带有VPC,子网&的EC2的CloudFormation模板(JSON).安全组选择 [英] CloudFormation Template (JSON) for EC2 with VPC, Subnet & Security Group Choices

查看:157
本文介绍了带有VPC,子网&的EC2的CloudFormation模板(JSON).安全组选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用JSON设置一个CloudFormation模板,该模板代表一个EC2实例,虽然刚刚开始,但是在选择VPC和子网时遇到了问题.最后,它将是跨多个帐户使用的模板,每个帐户具有多个VCP和子网.任何帐户中都没有默认的VPC.

I'm trying to setup a CloudFormation template in JSON that stands up an EC2 instance, just getting started but having issues with selecting VPC and subnet. In the end this will be a template used across multiple accounts each with multiple VCPs and subnets. There is no default VPC in any of the accounts.

我想让VPC出现模板提示,然后基于VPC迭代有效的子网.我一直在处理此亚马逊博客文章:

I'd like to have the template prompt for VPC then iterate valid subnets based on the VPC. I've been working with this Amazon blog post: Looking up information on AWS CloudFormation stack parameters using AWS Lambda | AWS Management Tools Blog

但是,我似乎无法使它正常工作.我有Lambda函数设置,具有本文中概述的正确角色,但出现错误此用户无默认VPC".我也愿意采用一种更简单的方法来使其工作.

However, I can't seem to get this to work. I have the Lambda function setup with the correct role as outlined in the article but I'm getting the error "No default VPC for this user". I'm also open to an easier way to get this to work.

{
   "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,然后显示一个有效的子网列表.

I'd like to be prompted for a VPC then be presented with a valid list of subnets.

推荐答案

该博客文章显示了如何获取有关特定资源的属性信息(例如子网),然后如何在模板,例如在输出"部分中,以显示有关所选资源的更多信息.

That blog post shows how to get attribute information about a particular resource (eg a Subnet) and then use those attribute elsewhere in the template, such as in the Outputs section to show more information about those chosen resource.

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

However, it is not possible to call a Custom Resource to manipulate the Parameters section. This is because the Parameters are collected before the stack is built.

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

So, if your desire is to prompt for a VPC, and then populate the Subnet parameter only with a list of subnets that belong to that VPC, then sorry — this is not possible.

欢迎您创建自己的前端" ,该消息会要求用户提供信息,包括上述功能,然后调用CloudFormation创建具有所需参数的堆栈,但并非如此可以在CloudFormation管理控制台内的参数中添加自定义逻辑.

You are welcome to create your own "front-end" that asks users for information, including the above ability, and then call CloudFormation to create the stack with the desired parameters, but it is not possible to add custom logic to the Parameters within the CloudFormation management console.

这篇关于带有VPC,子网&的EC2的CloudFormation模板(JSON).安全组选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆