Search code examples
amazon-web-servicesaws-cloudformationamazon-ecs

I am trying to create an ECS service in cloudformation, and I want to use the default VPC that it creates, and choose any of the subnets


Below is the yaml template. In the NetworkConfiguration, the subnet property is required. How should I set it to be any subnet of the default VPC that was created?

Resources:
    ECSService:
        Type: AWS::ECS::Service
        Properties:
            TaskDefinition: !Ref ECSTaskDefinition
            LaunchType: FARGATE
            Cluster: !Ref ECSCluster
            ServiceName: !Join
                - '-'
                - 
                    - !Ref Message
                    - !Ref Stage
                    - service
            DesiredCount: 1
            DeploymentConfiguration:
                MaximumPercent: 200
                MinimumHealthyPercent: 100
            NetworkConfiguration:
                AwsvpcConfiguration:
                    AssignPublicIp: ENABLED
                    Subnets: 
                        - ?????

Solution

  • There isn’t a value for “any subnet in this vpc”, you’d have to set the subnets in the template or as a parameter.

    Alternatively you can create the vpc and subnets in the template and reference them when describing your ECS service.

    Lastly you could use a custom resource to call a Lambda function that looks up the subnets but it’s more complicated than a native reference. See an AWS blog post on it here https://aws.amazon.com/blogs/mt/looking-up-information-on-aws-cloudformation-stack-parameters-using-aws-lambda/