如何提供实例身份文档以使用AWS Javascript SDK将容器实例注册到AWS ECS集群?

时间:2020-07-12 16:32:15

标签: amazon-web-services amazon-ec2 amazon-ecs aws-sdk-js aws-sdk-nodejs

我正在使用AWS Javascript SDK创建集群并注册ECS任务的容器实例。我为请求参数提供实例身份文档值而感到震惊。我正在尝试将运行中的EC2实例注册为容器实例。我通过执行以下命令curl命令来获得实例身份文档。

curl http://169.254.169.254/latest/dynamic/instance-identity/document/

输出:

{
  "accountId" : "01234567891",
  "architecture" : "x86_64",
  "availabilityZone" : "us-east-11",
  "billingProducts" : null,
  "devpayProductCodes" : null,
  "marketplaceProductCodes" : null,
  "imageId" : "ami-123456789012d",
  "instanceId" : "i-12345678901",
  "instanceType" : "t2.micro",
  "kernelId" : null,
  "pendingTime" : "2020-07-12T15:57:19Z",
  "privateIp" : "172.31.33.40",
  "ramdiskId" : null,
  "region" : "us-east-1",
  "version" : "2017-09-30"
}

我已将输出转换为字符串,但是NodeJS代码出现错误。

{ InvalidParameterException: An identity document was provided, but not valid.

message:
'The identity document and identity document signature were not valid.',
code: 'InvalidParameterException',
time: 2020-07-14T07:30:59.782Z,
requestId: '7bca816f-8c8f-4e6e-8e77-04c93b45c17e',
statusCode: 400,
retryable: false,
retryDelay: 68.57527460306602 } 'InvalidParameterException: The identity document and identity document signature were not valid.\n  

我们是否必须从EC2实例执行registerContainerInstances API?

请指导!

1 个答案:

答案 0 :(得分:1)

我已解决问题。利用EC2 SDK通过ECS AMI Image启动实例,并在用户数据中添加以下脚本。

#!/bin/bash
echo "ECS_CLUSTER=YOUR_CLUSTER_NAME" >> /etc/ecs/ecs.config

以上代码将注册到指定的ECS集群中。不需要使用registerContainerInstances API。如果AWS从API参考中删除此API,那就太好了。