简体   繁体   English

云形成:如何从子网ID获取VPC

[英]Cloud Formation: How to get VPC from Subnet Id

I have a Cloud Formation to set up an EC2 instance. 我有一个Cloud Formation来设置EC2实例。 I'm currently using the Parameters to specify the Subnet Id for the EC2 instance as well as the VPC Id for the Security Group (to be used in turn by the EC2 instance). 我目前正在使用参数来指定EC2实例的子网ID以及安全组的VPC ID(由EC2实例依次使用)。

In my situation the Subnet Id specified is required to be part of the VPC and I'd like to only have to specify the Subnet Id in the Parameters. 在我的情况下,指定的子网ID必须是VPC的一部分,我只想在参数中指定子网ID。 But I can't find a way to derive the VPC from the Subnet Id ( http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html ) 但是我找不到从子网ID派生VPC的方法( http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html

I see the GetAtt function can be used to return resource attributes. 我看到GetAtt函数可用于返回资源属性。 Is there something equivalent to return resource properties? 是否有等同于返回资源属性的东西?

From the documentation Fn:GetAtt , you can only retrieve AvailabilityZone and Ipv6CidrBlocks details of the Subnet. 从文档Fn:GetAtt中 ,您只能检索子网的AvailabilityZone和Ipv6CidrBlocks详细信息。 There is no inbuilt support to get VpcId of the given subnet in CFn Templates. CFn模板中没有内置支持来获取给定子网的VpcId。

There is a work-around though. 不过,有一种解决方法。 If you are using the aws-cli documentation , you can use the describe-subnets method to fetch the VpcId of the required subnet and pass it as input to the Cloudformation template create_stack call. 如果使用aws-cli 文档 ,则可以使用describe-subnets subnets方法获取所需子网的VpcId并将其作为输入传递给Cloudformation模板create_stack调用。

This method works even if you are using any SDK. 即使使用任何SDK,此方法也可以使用。 for example, in Java. 例如,在Java中。

//pseudo code only! 
DescribeSubnetsRequest request = new DescribeSubnetsRequest();
request.withSubnetIds("subnet-abcdefgh");

DescribeSubnetsResult result = awsClient.describeSubnets(request);
String myVpc = result.getSubnets().get(0).getVpcId();

// add the above VPC Id to the parameters of your Cloud formation template create stack request.

Hope this helps. 希望这可以帮助。

I created a small project called cli2cloudformation. 我创建了一个名为cli2cloudformation的小项目。 Using that you're able to execute cli commands inside your cloudformation stack and use the results of the commands. 使用它,您可以在cloudformation堆栈内执行cli命令并使用命令的结果。

Just check it here . 只是在这里检查。 I hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM