【问题标题】:Break cloudformation file into smaller scripts将 cloudformation 文件分解为更小的脚本
【发布时间】:2017-08-07 20:10:37
【问题描述】:

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

我如何创建 a) create_vpc.json 和 vpc_param.json 文件。 b) 如何从 aws cli 获取 vpc 列表并将 vpc 名称/id 传递给 create_subnet.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"
            }]
         }
      },
   }
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-11
        • 2013-08-05
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 2014-10-04
        相关资源
        最近更新 更多