2
votes

I have large cloudformation json file which I used to create new vpc, subnets, ec2 instance. Basically looking into breaking file into multiple small file for easy maintenance.

How do I create a) create_vpc.json and vpc_param.json file. b) how do I get list of vpc from aws cli and pass vpc name/id to create_subnet.json and define subnet_param.json. c) get list of subnet for a vpc using aws cli and pass that as arguments to create_routetable.json and routetable_param.json

Similarly want to create other scripts for ec2/elb/other stuffs.

aws cloudformation create-stack --stackname startmyinstance  --template-body file:///some/local/path/templates/startmyinstance.json --parameters file:///some/local/path/params/startmyinstance-parameters.json

my current file is :

 {
   "AWSTemplateFormatVersion": "2010-09-09",
   "Description": " Tempalte to launch Custom VPC with two availablilty zones. **WARNING** This template might create one or more Amazon EC2 instances. You will be billed for the AWS resources used if you create a stack from this template.",
   "Parameters": {
      "KeyName": {
         "Description": "Name of an existing EC2 KeyPair to enable SSH access to the instances",
         "Type": "String",
         "MinLength": "1",
         "MaxLength": "64",
         "AllowedPattern": "[-_ a-zA-Z0-9]*",
         "Default": "Helix-PROD",
         "ConstraintDescription": "can contain only alphanumeric characters, spaces, dashes and underscores."
      },
      "VpcCidr": {
         "Description": "CIDR address for the VPC to be created.",
         "Type": "String",
         "Default": "10.206.36.0/22"
      },
      "SUB1": {
         "Description": "subnet1.",
         "Type": "String",
         "Default": "10.206.36.0/27"
      },
      "SUB2": {
         "Description": "subnet2",
         "Type": "String",
         "Default": "10.206.36.32/27"
      },

      "AvailabilityZone1": {
         "Description": "First AZ to use for Public1/private1 subnets.",
         "Type": "AWS::EC2::AvailabilityZone::Name",
         "Default": "eu-west-1a"
      },
      "AvailabilityZone2": {
         "Description": "First AZ to use for Public2/private2 subnets.",
         "Type": "AWS::EC2::AvailabilityZone::Name",
         "Default": "eu-west-1b"
      },
   },
   "Mappings": {
      "RegionMap": {

         "eu-west-1": {
            "64": "ami-70edb016"
         }
      }

   },

   "Resources": {
      "VPC": {
         "Type": "AWS::EC2::VPC",
         "Properties": {
            "CidrBlock": {
               "Ref": "VpcCidr"
            },
            "Tags": [{
               "Key": "Network",
               "Value": "Public"
            }]
         }
      },
      "Sub1": {
         "Type": "AWS::EC2::Subnet",
         "Properties": {
            "VpcId": {
               "Ref": "VPC"
            },
            "AvailabilityZone": {
               "Ref": "AvailabilityZone1"
            },
            "CidrBlock": {
               "Ref": "subnet1"
            },
            "Tags": [{
               "Key": "Network",
               "Value": "Private"
            }, {
               "Key": "Name",
               "Value": "Sub1"
            }]
         }
      },
      "Sub2": {
         "Type": "AWS::EC2::Subnet",
         "Properties": {
            "VpcId": {
               "Ref": "VPC"
            },
            "AvailabilityZone": {
               "Ref": "AvailabilityZone2"
            },
            "CidrBlock": {
               "Ref": "subnet2"
            },
            "Tags": [{
               "Key": "Network",
               "Value": "Private"
            }, {
               "Key": "Name",
               "Value": "Sub2"
            }]
         }
      },
   }
}
2

2 Answers

1
votes

You can use cfpack.js for this. It allows you to use multiple smaller templates that will be built into one bigger template and deployed to your CloudFormation stack.

0
votes

You could make use of Nested Stacks - (This link explains what they are or when to use). If you want to see sample templates or snippets visit this AWS page to see sample templates.