aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(aws-ecs): ECS Exec Support

Open mbeacom opened this issue 5 years ago • 14 comments

As of March 16, ECS now supports remote execution similar to docker exec.

Use Case

As a user, I want the capability to execute commands against an AWS ECS Fargate / EC2 container (similar to docker exec) workloads managed by CDK.

Proposed Solution

CloudFormation's AWS::ECS::Service resource currently supports the boolean property: EnableExecuteCommand

Expose this to the aws-ecs's FargateService similar to enableEcsManagedTags

Other

  • https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html
  • https://aws.amazon.com/blogs/containers/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/
  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html#cfn-ecs-service-enableexecutecommand
  • [x] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

mbeacom avatar Mar 16 '21 16:03 mbeacom

We may also want to consider modifying the task role since "the task role will need to have IAM permissions to log the output to S3 and/or CloudWatch if the cluster is configured for these options. If these options are not configured then these IAM permissions are not required."

See: "Configuring the task role with the proper IAM policy"

SoManyHs avatar Mar 16 '21 23:03 SoManyHs

Is there any way one could use this functionality before it has higher-level support within CDK?

underbluewaters avatar Mar 17 '21 19:03 underbluewaters

@mbeacom Hi! Thanks so much for opening this issue -- was wondering if you were actively working on this? If so I can tag it as "in-progress"!

SoManyHs avatar Mar 18 '21 18:03 SoManyHs

@underbluewaters you can use escape hatches to set the EnableExecuteCommand property on the L1 ecs.CfnService construct. This property will be available on the next CDK release as it was just pulled from the new spec yesterday https://github.com/aws/aws-cdk/pull/13633.

You can access the underlying L1 resource of any construct or use the L1 classes directly until we add these properties to the corresponding higher level constructs. See the above docs for details.

MrArnoldPalmer avatar Mar 18 '21 18:03 MrArnoldPalmer

Note: CfnSpec with update on ECS Service for EnableExecuteCommand is now merged: https://github.com/aws/aws-cdk/pull/13633

SoManyHs avatar Mar 18 '21 19:03 SoManyHs

@MrArnoldPalmer Thanks! I was able to get it working with the following code:

// const cfnService = ecsService.node.defaultChild as CfnService;
// doesn't work, see
// https://github.com/aws/aws-cdk/issues/10666
const cfnService = ecsService.node.children[0] as CfnService;
cfnService.addOverride("Properties.EnableExecuteCommand", "True");

I had to be sure to add the following permissions to the task execution role:

const role = new iam.Role(this, "MaintenanceRole", {
  assumedBy: new ServicePrincipal("ecs-tasks.amazonaws.com"),
  inlinePolicies: {
    ecsExec: new PolicyDocument({
      statements: [
        new PolicyStatement({
          effect: iam.Effect.ALLOW,
          actions: [
            "ssmmessages:CreateControlChannel",
            "ssmmessages:CreateDataChannel",
            "ssmmessages:OpenControlChannel",
            "ssmmessages:OpenDataChannel",
          ],
          resources: ["*"],
        }),
      ],
    }),
  },
});
const taskDefinition = new ecs.FargateTaskDefinition(
  this,
  "SeaSketchMaintenanceFargateTaskDef",
  {
    cpu: 256,
    executionRole: role,
    memoryLimitMiB: 512,
  }
);

underbluewaters avatar Mar 18 '21 21:03 underbluewaters

@SoManyHs Yes, I'd be more than happy to (as long as an ETA of this weekend will suffice).

mbeacom avatar Mar 18 '21 21:03 mbeacom

@mbeacom awesome! I have some starter code pushed to my fork if you want to save some time and build off of that -- added the EnableExecuteCommand property, but if you could continue with adding the IAM policy to the taskRole, that'd be amazing!

SoManyHs avatar Mar 18 '21 21:03 SoManyHs

Hi @mbeacom, were you still planning on submitting a PR for this? Thanks!

SoManyHs avatar Mar 29 '21 18:03 SoManyHs

@SoManyHs Yes, apologies for the delay. I can push this out shortly!

mbeacom avatar Mar 31 '21 22:03 mbeacom

Hi @mbeacom, If you're not actively working on this anymore, mind if I move this back into the backlog? Thanks!

SoManyHs avatar Apr 21 '21 23:04 SoManyHs

Hi @mbeacom!

We're planning on prioritizing this in our next sprint -- since we haven't heard from you in awhile I'll go ahead and unassign you. Thanks for your help, and feel free to reach out if you have other input!

SoManyHs avatar Apr 28 '21 17:04 SoManyHs

Hi, just updating the issue with the current status on the feature. The implementation involves enabling support for the enableExecuteCommand field, adding IAM permissions for the AWS Systems Manager Agent (SSM) and further adding permissions for logging using CloudWatch or S3. For more information, please refer the documentation. I'm currently working on adding and testing the logging permissions.

upparekh avatar May 13 '21 00:05 upparekh

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Jun 03 '21 21:06 github-actions[bot]