CloudFormation nested stacks

What are nested stacks? For example, we have three cloudformation:

  1. One template that is creating the VPC.
  2. One template that is creating the security group.
  3. One template that is creating one EC2 instance.
  4. One template that is creating the RDS cluster.

In stead of having one single big template, we can create multiple templates, called nested stacks. And yes, this templates have to run in order:

  1. First VPC template.
  2. Second security group template.
  3. Third EC2 template.
  4. Fifth RDS cluster template.

Small description

Bellow I’ll simplify and represent just the dependencies between this templates. It’s not usefull to explain all the resources that are created within all this templates!

We need to run the VPC template first, because this template is going to create the VPC and the subnets. Than the security group template will wait untill VPC template will finish, because the security group template will generate the security groups for EC2 instance and RDS cluster. After security group template is creating the resources, EC2 template will follow and EC2 template needs the security group and VPC subnets. Than, RDS template needs the subnets from the VPC template and the security groups.

Stack template

{
  "Description": "Nested stack template",
 
  "Resources": {
    "VPCstack": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "https://s3-us-west-2.amazonaws.com/VPC.template"

      }
    },
    "SecurityGroupStack": {
      "Type": "AWS::CloudFormation::Stack",
      "DependsOn": [
        "VPCstack"
      ],
      "Properties": {
        "TemplateURL": "https://s3-us-west-2.amazonaws.com/security_group.template"

      }
    },
    "EC2Stack": {
      "Type": "AWS::CloudFormation::Stack",
      "DependsOn": ["SecurityGroupStack", "VPCstack" ],
      "Properties": {
        "TemplateURL": "https://s3-us-west-2.amazonaws.com/ec2.template"

      }
    },
    "RDSStack": {
      "Type": "AWS::CloudFormation::Stack",
      "DependsOn": ["VPCstack", "SecurityGroupStack", "EC2Stack" ],
      "Properties": {
        "TemplateURL": "https://s3-us-west-2.amazonaws.com/RDS.template"

      }
    }
  },
  "Outputs": {
    "VPCStack": {
      "Description": "VPC stack",
      "Value": {
        "Ref": "VPCstack"
      },
      "Export": {
        "Name": {
          "Fn::Sub": "${AWS::StackName}-VPC"
        }
      }
    },
    "SecurityGroupStack": {
      "Description": "Security group stack",
      "Value": {
        "Ref": "SecurityGroupStack"
      },
      "Export": {
        "Name": {
          "Fn::Sub": "${AWS::StackName}-SecurityGroup"
        }
      }
    },
    "EC2Stack": {
      "Description": "EC2 stack",
      "Value": {
        "Ref": "EC2Stack"
      },
      "Export": {
        "Name": {
          "Fn::Sub": "${AWS::StackName}-EC2"
        }
      }
    },
    "RDSStack": {
      "Description": "RDS stack",
      "Value": {
        "Ref": "RDSStack"
      },
      "Export": {
        "Name": {
          "Fn::Sub": "${AWS::StackName}-RDS"
        }
      }
    }
  }
}



This is just an example on how to use nested stacks.