CDK for ECS - Subnet and Architecture type

0

HI All

I have 2 questions on how to use CDK to deploy ECS using python.

  1. How do we set subnet on the cluster via CDK? The VPC is straightforward using the vpc parameter, however i could not find the param for subnet.
  2. How do we set the architecture(x64 or ARM), using the CDK ? While creating the task I need to set the architecture to a specific value as I am getting the following error: standard_init_linux.go:228: exec user process caused: exec format error

Thank you for your help.

Regards

asked 2 years ago1950 views
2 Answers
1
Accepted Answer

You can specify specific subnets for your EC2 container instances and/or your AWSVPC network configuration (if you are using Fargate or AWSVPC networking for your tasks/services).

For EC2 instances, the options you can pass to add_capacity (for EC2) are here - you want the vpc_subnets property.

For task networking, you can specify VPC subnets by specifying the vpc_subnets property to your Ec2Service or FargateService declarations.

If you want to run your task on Graviton2 (arm64 architecture), you don't need to declare the architecture of an ECS Task except on Fargate. At this writing, CDK does not yet have Level 2 construct support for architecture configurations in Task Definitions. You would have to use an Escape Hatch to modify the underlying L1 construct (CfnTaskDefinition in particular) to set the runtime_platform property.

If you're running your task on an EC2 instance, you just need to make sure that the architecture of your EC2 instance matches the architecture of your container image - or, better still, create a multi-architecture container image.

AWS
EXPERT
answered 2 years ago
  • HI Michael

    Thank you for the response.

    "You don't need to declare the architecture of an ECS Task except on Fargate. "

    Regarding the above mentioned line, what I understand is that instead of using the ecs.FargateTaskDefinition, I should be using the ecs.CfnTaskDefinition. Is that what I understand ?

    Here is what I am currently using: task_definition = _ecs.FargateTaskDefinition(self, "TaskDefinition", cpu=2048, memory_limit_mib=4096, execution_role=ecs_role, task_role = ecs_role, )

    Thanks

    Thanks

  • You can still use ecs.FargateTaskDefinition, but you'll need to use the escape hatch to get to the underlying ecs.CfnTaskDefinition and add the property. The documentation I linked above explains how. See "Modifying the AWS CloudFormation resource behind AWS constructs".

1

Hi Michael,

Thanks a bunch for your help. I was able to resolve this.

For everyone else:

runtime_platform_property = _ecs.CfnTaskDefinition.RuntimePlatformProperty(
            cpu_architecture="ARM64",
            operating_system_family="LINUX"
        )

cfn_task_defn = task_definition.node.default_child
cfn_task_defn.runtime_platform = runtime_platform_property

Thank you.

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions