Search code examples
amazon-web-servicesamazon-ec2amazon-ecsaws-sdk-jsaws-sdk-nodejs

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


I'm using AWS Javascript SDK to create a cluster and register the container instance for ECS task. I got struck by providing the instance identity document value for the request parameters. I'm trying to register my running EC2 instance as container instance. I got the instance identity document by executing the following command curl command.

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

Output:

{
  "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"
}

I have converted the output to string but I'm getting an error in NodeJS code.

{ 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  

Do we have to execute registerContainerInstances API from an EC2 instance?

Please guide!!!


Solution

  • I have fixed the problem. Make use of EC2 SDK to launch an instance with ECS AMI Image and added the following script in the user data.

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

    The above code will register to the specified ECS cluster. It is not required to use registerContainerInstances API. It would be great, if AWS removes this API from the API reference.