阅读背景:

是否可以使用AWS Cloud Formation获得VPC ID?

来源:互联网 

Is it possible to obtain a VPC Id using Cloud Formation JSON?

是否可以使用Cloud Formation JSON获取VPC ID?

Something like:

就像是:

{ "Fn::GetAtt" : [ "MyVPCName", "VPCId" ] }

3 个解决方案

#1


7  

You can use the "Ref" built-in function to get the VPC's resource ID iff you created the VPC in the same template, see the Return Values section of https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html

如果您在同一模板中创建了VPC,则可以使用“Ref”内置函数来获取VPC的资源ID,请参阅https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide的“返回值”部分。 /aws-resource-ec2-vpc.html

If you are wanting to reference an existing VPC (like your default VPC), then the above won't work. You could pass the VPC's Id as a parameter to the template instead and use the "Ref" built-in to use it where you need it.

如果您想要引用现有VPC(如默认VPC),则上述操作无效。您可以将VPC的Id作为参数传递给模板,并使用内置的“Ref”在您需要的地方使用它。

#2


5  

For a pre-existing VPC, start with the Parameter:

对于预先存在的VPC,请从参数开始:

    "VpcId": { "Type": "String" },

Then you can use something like this with your launch script:

然后你可以在你的启动脚本中使用这样的东西:

aws --profile profile_name --region us-east-1 ec2 describe-vpcs --filters 'Name=tag:Name,Values=MY_VPC_NAME'

This way you find the VPC before you launch the stack. In this way you can launch to different regions or accounts without having to always look up that information by hand.

这样,您可以在启动堆栈之前找到VPC。通过这种方式,您可以启动到不同的区域或帐户,而无需始终手动查找该信息。

#3


1  

Bradley's answer is right. I'd also add that you might want to look into nested stacks if you find you need to "sew together" resources that are created in separate templates. Another option is to add some code layer that generates the template (e.g. cfn-pyplates) and use the AWS API to do those lookups across stacks.

布拉德利的回答是正确的。我还要补充一点,如果您发现需要“缝合”在单独模板中创建的资源,您可能需要查看嵌套堆栈。另一个选择是添加一些生成模板的代码层(例如cfn-pyplates),并使用AWS API跨堆栈执行这些查找。


分享到: