【问题标题】:CloudFormation Template (JSON) for EC2 with VPC, Subnet & Security Group Choices具有 VPC、子网和安全组选择的 EC2 的 CloudFormation 模板 (JSON)
【发布时间】:2019-10-27 09:43:08
【问题描述】:

我正在尝试以 JSON 格式设置一个支持 EC2 实例的 CloudFormation 模板,刚刚开始,但在选择 VPC 和子网时遇到了问题。最后,这将是一个跨多个帐户使用的模板,每个帐户具有多个 VCP 和子网。任何账户中都没有默认 VPC。

我想要 VPC 的模板提示,然后根据 VPC 迭代有效子网。我一直在处理这篇亚马逊博客文章:Looking up information on AWS CloudFormation stack parameters using AWS Lambda | AWS Management Tools Blog

但是,我似乎无法让它工作。如文章中所述,我使用正确的角色设置了 Lambda 函数,但我收到错误“此用户没有默认 VPC”。我也愿意采用更简单的方法来实现这一点。

{
   "AWSTemplateFormatVersion":"2010-09-09",
   "Description":"EC2 CloudFormation Template - Version 1.0",
   "Metadata":{},
   "Parameters":{
      "InstanceType":{
         "Description":"EC2 instance type",
         "Type":"String",
         "Default":"t2.small",
         "AllowedValues":[
            "t1.micro",
            "t2.nano",
            "t2.micro",
            "t2.small",
            "t2.medium",
            "t2.large"
         ],
         "ConstraintDescription":"must be a valid EC2 instance type."
      },
      "VpcName" : {
        "Type" : "AWS::EC2::VPC::Id",
        "Description" : "Select the VPC for this EC2 Instances"
      },
      "SubnetName" : {
        "Type" : "AWS::EC2::Subnet::Id",
        "Description" : "The list of SubnetIds"
      }
    },
   "Mappings":{},
   "Conditions":{},
   "Resources":{
     "VcpInfo" : {
       "Type" : "Custom::VcpInfo",
       "Properties" : {
         "ServiceToken" : "arn:aws:lambda:us-east-1:206765214992:function:Test_GetAtt",
         "NameFilter" : { "Ref": "VpcName" }
       }
     },
     "SubnetInfo" : {
       "Type" : "Custom::SubnetInfo",
       "Properties" : {
         "ServiceToken" : "arn:aws:lambda:us-east-1:206765214992:function:Test_GetAtt",
         "NameFilter" : { "Ref": "SubnetName" }
       }
      },
      "EOTSSEC2":{
         "Type":"AWS::EC2::Instance",
         "Properties":{
            "DisableApiTermination":"false",
            "ImageId":"ami-06bee8e1000e44ca4",
            "InstanceType":{ "Ref":"InstanceType"  },
            "Monitoring":"true"
         }
      }
    },
    "Outputs":{
     "VCPCidrBlock" : {
       "Description" : "VCP CidrBlock",
       "Value" : "!GetAtt VcpInfo.CidrBlock"
     },
     "SubnetAvailabilityZon" : {
       "Description" : "Subnet AvailabilityZone",
       "Value" : "!GetAtt SubnetInfo.AvailabilityZone"
     },
     "SubnetCidrBlock" : {
       "Description" : "Subnet CidrBlock",
       "Value" : "!GetAtt SubnetInfo.CidrBlock"
     },
     "SubnetVpcId" : {
       "Description" : "Subnet VpcId",
       "Value" : "!GetAtt SubnetInfo.VpcId"
     }
   }
}

我希望系统提示我输入 VPC,然后显示有效的子网列表。

【问题讨论】:

    标签: json amazon-web-services amazon-ec2 amazon-cloudformation


    【解决方案1】:

    那篇博文展示了如何获取有关特定资源的属性信息(例如子网),然后在模板的其他地方使用这些属性,例如在输出部分显示有关这些资源的更多信息选择的资源。

    但是,不可能调用自定义资源来操作Parameters 部分。这是因为参数是在构建堆栈之前收集的。

    因此,如果您希望提示输入 VPC,然后仅使用属于该 VPC 的子网列表填充 Subnet 参数,那么抱歉 - 这是不可能的。

    欢迎您创建自己的“前端”,向用户询问信息,包括上述能力,然后调用 CloudFormation 以创建所需参数的堆栈,但它不是可以在 CloudFormation 管理控制台中向参数添加自定义逻辑。

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 2016-05-12
      • 2019-12-22
      • 2015-08-01
      • 2020-08-07
      • 2019-06-24
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      相关资源
      最近更新 更多