【问题标题】:Is it possible to get a VPC Id using AWS Cloud Formation?是否可以使用 AWS Cloud Formation 获取 VPC ID?
【发布时间】:2015-01-03 08:57:31
【问题描述】:

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

类似:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          我建议将此类重要值放入 CFN 导出中。然后用这样的东西轻松引用。

          YAML 代码示例

          VpcId: !ImportValue VpcId
          

          JSON 代码示例

          "VpcId" : {"Fn::ImportValue" : "VpcId"}
          

          Link to AWS documentation on how to import CloudFormation exports

          Link to AWS documentation on creating CloudFormation Outputs and Exports

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-08-18
            • 2021-06-29
            • 1970-01-01
            • 2019-06-02
            • 1970-01-01
            • 2018-12-18
            • 2021-04-20
            • 2012-10-02
            相关资源
            最近更新 更多