Do I need to create a seperate network stack for this cloudformation yaml file to work?

so I am tring to create Fargate instances into subnets using cloudformation.
I would like the user to be able to choose which vpc id, subnet ids to launch their Fargate instances into which I use as a parameter like this:

Parameters:
  VPCSubnets:
    Type: List<AWS::EC2::Subnet::Id>
    Description: Provide the subnets you wish to deploy into.
  VPCInformation:
    Type: AWS::EC2::VPC::Id
    Description: Provide the VPC ID that resources will be deployed into.

this information is used for the network settings for ECS and task definitions.
If I create network resouces just below parameters like this:

for example:

MyVpc:
    Type: AWS::EC2::VPC
    Description: VPC for the cluster and fargate instances
    Properties:
      CidrBlock: 10.0.0.0/26
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
      - Key: interviewchallenge-vpc
        Value: !Join ['', [!Ref "AWS::Region", "conversion-challenge-VPC" ]]

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: myVPC
      CidrBlock: 10.0.0.0/28
      AvailabilityZone: "us-east-1a"
      Tags:
      - Key: interviewchallenge-vpc-subnet1
        Value: !Join ['', [!Ref "AWS::Region", "conversion-challenge-az1" ]]

At this point in the template, these network resouces havent been created right?
Can this be done in a single stack??

>Solution :

Can this be done in a single stack??

No. You need two templates and the corresponding stacks. The first template creates VPC, subnets and the remaining network resources. Then in the second stack, you use them in for ECS deployment.

Only this way your user will be able to choose the VPC and subnets when creating the ECS stack.

Leave a Reply