Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AWS ECS Task Role in Node AWS SDK code

Code that uses the AWS Node SDK doesn't seem to be able to gain the role permissions of the ECS task.

If I run the code on an EC2 ECS instance, the code seems to inherit the role on the instance, not of the task.

If I run the code on Fargate, the code doesn't get any permission.

By contrast, any bash scripts that run within the instance seem to have the proper permissions.

Indeed, the documentation doesn't mention this as an option for the node sdk, just:

  1. Loaded from IAM roles for Amazon EC2 (if running on EC2),
  2. Loaded from the shared credentials file (~/.aws/credentials),
  3. Loaded from environment variables,
  4. Loaded from a JSON file on disk,
  5. Hardcoded in your application

Is there any way to have your node code gain the permissions of the ECS task?

This seems to be the logical way to pass permissions to your code. It works beautifully with code running on an instance.

The only workaround I can think of is to create one IAM user per ECS service and pass the API Key/Secret as environmental variables in the task definition. However, that doesn't seem very secure since it would be visible in plain text to anyone with access to the task definition.

like image 896
Cristian Vrabie Avatar asked Sep 18 '18 17:09

Cristian Vrabie


People also ask

How do I connect to an ECS task?

To connect to your container instanceOpen the Amazon ECS console at https://console.aws.amazon.com/ecs/ . Select the cluster that hosts your container instance. On the Cluster page, choose ECS Instances. On the Container Instance column, select the container instance to connect to.

How do I create an Amazon Elastic container service role?

Open the IAM console at https://console.aws.amazon.com/iam/ . In the navigation pane, choose Roles, Create role. For Select trusted entity section, choose AWS service. For Use case, using the drop down menu, select Elastic Container Service and then the Elastic Container Service Task use case and then choose Next.

Can you ssh into ECS task?

Furthermore, ECS users deploying tasks on Fargate did not even have this option because with Fargate there are no EC2 instances you can ssh into. With ECS on Fargate, it was simply not possible to exec into a container(s).

How do I run a task in ECS cluster?

Open the Amazon ECS console. In the navigation menu, choose Clusters, and then select the cluster that contains your stopped task. On your cluster's page, choose the Tasks tab. In the Desired task status table header, choose Stopped, and then select the stopped task to inspect.


1 Answers

Your question is missing a lot of details on how you setup your ECS Cluster plus I am not sure if the question is for ECS or for Fargate specifically.

Make sure that you are using the latest version of the SDK. Javascript supports ECS and Fargate task credentials.

Often there is confusion about credentials on ECS. There is the IAM role that is assigned to the Cluster EC2 instances and the IAM role that is assigned to ECS tasks.

The most common problem is the "Trust Relationship" has not been setup on the ECS Task Role. Select your IAM role and then the "Trust Relationships" tab and make sure that it looks like this:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "ecs-tasks.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

In addition to the standard Amazon ECS permissions required to run tasks and services, IAM users also require iam:PassRole permissions to use IAM roles for tasks.

Next verify that you are using the IAM role in the task definition. Specify the correct IAM role ARN in the Task Role field. Note that this different than Task Execution Role (which allows containers to pull images and publish logs).

Next make sure that your ECS Instances are using the latest version of the ECS Agent. The agent version is listed on the "ECS Instances" tab under the right hand side column "Agent version". The current version is 1.20.3.

Are you using an ECS optimized AMI? If not, add --net=host to your docker run command that starts the agent. Review this link for more information.

like image 118
John Hanley Avatar answered Oct 22 '22 16:10

John Hanley