【问题标题】:YAML code to create multiple VPC using count in AWS cloudformation使用 AWS cloudformation 中的计数创建多个 VPC 的 YAML 代码
【发布时间】:2022-07-19 17:32:39
【问题描述】:

我是 CloudFormation 的新手,想使用 YAML 创建一个模板。我需要弄清楚是否有任何方法可以使用 UserInput 创建多个 VPC。 到目前为止,我已经使用了以下代码:

  Parameters:
      EnvironmentName:
        Description: An environment name that is prefixed to resource names
        Type: String
    
    vpcCIDR1:
      Description: Please enter the IP range (CIDR notation) for this VPC
      Type: String
      Default: 10.3.0.0/16
    vpcCIDR2:
      Description: Please enter the IP range (CIDR notation) for this VPC
      Type: String
      Default: 10.4.0.0/16
Resources:
  VPC1:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref vpcCIDR1
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName
  VPC2:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref vpcCIDR2
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

我不需要再次编写相同的代码,而是需要代码来获取用户输入的 VPC 计数并根据用户输入创建 VPC。

【问题讨论】:

    标签: amazon-web-services templates yaml amazon-cloudformation aws-cloudformation-custom-resource


    【解决方案1】:

    是的,如果您开发自己的macro,您就可以做到这一点。 CloudFormation 中没有循环,也没有任何复杂的用户输入处理。但是使用宏,您可以编写任何您想要的逻辑。或者,如果您不需要宏,也可以使用 custom resources

    【讨论】:

      【解决方案2】:

      如何使用这样的基本 bash 脚本(我尚未完全测试部署,您需要修改文件名等),但这样的事情将是一个起点:

      #!/bin/bash
      
      while read -n1 -r -p "Would you like to create another VPC: [y]es|[n]o"; do
        case $REPLY in
          y)  echo
              echo "Enter a VPC name: "
              read vpcName
              echo
              echo "Enter a VPC CIDR: "
              read vpcCIDR
              echo
              echo "Enter a VPC Environment: "
              read Environment
              echo
              echo "Creating VPC with:
              VPC Name: $vpcName
              VPC CIDR: $vpcCIDR
              VPC Environment: $Environment"
              echo
              aws cloudformation create-stack --stack-name myteststack --template-body file://sampletemplate.json \
              --parameters ParameterKey=vpcCIDR,ParameterValue=$vpcCIDR \
              ParameterKey=vpcName,ParameterValue=$vpcName \
              ParameterKey=Environment,ParameterValue=$Environment
              ;;
      
          n)  echo
              echo "Nothing further to do, good bye!"
              exit;;
        esac
      done
      

      【讨论】:

        猜你喜欢
        • 2021-01-05
        • 2012-04-23
        • 2013-03-21
        • 2023-03-14
        • 2020-11-15
        • 1970-01-01
        • 2019-07-16
        • 2020-02-17
        • 2019-04-27
        相关资源
        最近更新 更多