Search code examples
amazon-ecsaws-cdk

How to specify VPC ID in cdk FargateService


I'm trying to create multiple ECS services each service as a different stack so I can update/delete/recreate them individually. I have 2 more stacks one for VPC and another one for the ECS cluster.

Structure:

.
|-- README.md
|-- stacks
|   |-- __init__.py
|   |-- ecs.py
|   |-- iam.py
|   |-- tasks_services.py
|   |-- test.py
|   `-- vpc.py
|-- app.py
|-- cdk.context.json

In my app.py I'm fusing thig together

vpc = vpcstack(app, "vpcstack", config,
         env=cdk_env
               )
cluster = ecsstack(app, "ecsstack",vpc,config,
               env =cdk_env
               )
ExecutionRole = executionRole (app, "role" , environment,
                            env=cdk_env)

service = mySvc (app,"initial",environment , config,cluster,vpc,
                         env =cdk_env)

I'm not sure how to pass the VPC values into the ecs.FargateService

    ecs.FargateService(self, "Service",
                       cluster=cluster,
                       task_definition=task_definition,
                       vpc_subnets = ec2.SubnetSelection(                             
                            subnet_type=ec2.SubnetType.PRIVATE_WITH_NAT
                                    ),

It gives the error TypeError: Cannot read properties of undefined (reading 'selectSubnets') makes sense because it was not able to read the VPC properties. I don't see any option to specify the VPC values here.

For example ec2.securitygroups has the VPC paramerter self.security_group = ec2.SecurityGroup(self, "SG", vpc=vpc) How to do the same for ecs.FargateService

Passing the VPC construct to the ECS cluster, (i have combined the VPC and clustre togather for ease of use ).

Creating VPC/cluster: in ecs.py

            vpc = ec2.Vpc(self, "VPC",
                               max_azs=3,

            ecs.Cluster(self, "Cluster",
                              vpc=vpc

Solution

  • Thanks to @gshpychka for pointing out the exact issue. Took some time to understand.

    Extract VPC for the stack and pass it to the next stack

    vpc = vpcstack(app, "vpcstack", config,
             env=cdk_env
                   )
    cluster = ecsstack(app, "ecsstack",vpc.vpc,config,
                   env =cdk_env
                   )
    
    service = mySvc (app,"initial",environment , config,cluster.ecs_cluster,vpc,
                             env =cdk_env)
    

    In VPC.py

    class vpcstack(Stack):
    
        def __init__(self, scope: Construct, construct_id: str, config,**kwargs) -> None:
            super().__init__(scope, construct_id, **kwargs)
    
            # VPC constructs
               self.vpc = ec2.Vpc(self, "VPC",
    

    in ecs.py

    class ecsstack(Stack):
    
        def __init__(self, scope: Construct, construct_id: str, config,vpc,**kwargs) -> None:
            super().__init__(scope, construct_id, **kwargs)
            # Cluster 
            self.ecs_cluster = ecs.Cluster(self, "Cluster"