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

How to provide instance identity document for registering the container instance to AWS ECS Cluster using AWS Javascript SDK?

我正在使用 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?

请多指教!!!

我已经解决了这个问题。使用 EC2 SDK 启动带有 ECS AMI Image 的实例,并在用户数据中添加以下脚本。

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

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