将cloudformation文件分解为更小的脚本

时间:2017-03-16 15:00:49

标签: amazon-web-services amazon-cloudformation aws-cli

我有大的cloudformation json文件,我用它来创建新的vpc,子网,ec2实例。 基本上是将文件分成多个小文件以便于维护。

我如何创作 a)create_vpc.json和vpc_param.json文件。 b)如何从aws cli获取vpc列表并将vpc name / id传递给create_subnet.json并定义subnet_param.json。 c)使用aws cli获取vpc的子网列表,并将其作为参数传递给create_routetable.json和routetable_param.json

同样想要为ec2 / elb /其他东西创建其他脚本。

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

我当前的档案是:

 {
   "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 个答案:

答案 0 :(得分:0)

您可以使用Nested Stacks - (此链接说明它们是什么或何时使用)。如果您想查看示例模板或代码段,请访问this AWS页面以查看示例模板。

答案 1 :(得分:0)

您可以使用cfpack.js。它允许您使用多个较小的模板,这些模板将内置到一个较大的模板中并部署到您的CloudFormation堆栈中。

相关问题