将云形成文件分解为较小的脚本



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

如何创建a( create_vpc.json 和 vpc_param.json 文件。b( 如何从 AWS CLI 获取 VPC 列表并将 VPC 名称/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"
            }]
         }
      },
   }
}

您可以使用嵌套堆栈 - (此链接解释了它们是什么或何时使用(。如果您想查看示例模板或代码段,请访问此 AWS 页面以查看示例模板。

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

最新更新