如何在 Cloudformation 模板中使用变量作为地形"Data source"的特征



在terraform中,我们可以使用数据源来获取现有的资源详细信息。

但是在 cloudformation 中,如果资源不是由 cloudformation 模板创建的,我找不到任何引用它的方法,除非我硬编码值,例如真正的 vpc id。

有什么建议吗?

一种方法

是使用参数(例如,AWS::EC2::VPC::Id for vpc ID(。创建堆栈时,这将列出您要在其中创建堆栈的区域中的所有现有 VPC。目前,此类参数仅限于卷,子网,sg等少数参数,但将来可能会有更多参数。尽管目前在云形成中没有任何与地形数据源完全相同的数据源。您也可以使用云形成宏来设计一些东西,但这会有点混乱。

最后,

我用 cloudformation 制作了一个 vpc 堆栈,它从 aws cli 获取参数输入并输出 vpc id、子网 id 等。

此 cloudformation 模板只有一个空资源(因为如果其模板中没有资源,cloudformation 将报告错误(。

Description: >
  This template deploys a VPC, with a pair of public and private subnets spread
  across two Availabilty Zones. It deploys an Internet Gateway, with a default
  route on the public subnets. It deploys a pair of NAT Gateways (one in each AZ),
  and default routes for them in the private subnets.
Parameters:
  EnvironmentName:
    Description: An environment name that will be prefixed to resource names
    Type: String
  VPC:
    Description: Please enter the VPC ID
    Type: String
  VpcCIDR:
    Description: Please enter the IP range (CIDR notation) for this VPC
    Type: String
  PublicSubnet1:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
    Type: String
  PublicSubnet2:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
    Type: String
  PrivateSubnet1:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone
    Type: String
  PrivateSubnet2:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
    Type: String
Conditions:
  HasNot: !Equals [ 'true', 'false' ]
Resources:
  NullResource:
    Type: 'Custom::NullResource'
    Condition: HasNot
Outputs:
  VPC:
    Description: A reference to the created VPC
    Value: !Ref VPC
    Export:
      Name: !Sub "${EnvironmentName}:VPC"
  PublicSubnet1:
    Description: A reference to the public subnet in the 1st Availability Zone
    Value: !Ref PublicSubnet1
    Export:
      Name: !Sub "${EnvironmentName}:PublicSubnet1"
  PublicSubnet2:
    Description: A reference to the public subnet in the 2nd Availability Zone
    Value: !Ref PublicSubnet2
    Export:
      Name: !Sub "${EnvironmentName}:PublicSubnet2"
  PrivateSubnet1:
    Description: A reference to the private subnet in the 1st Availability Zone
    Value: !Ref PrivateSubnet1
    Export:
      Name: !Sub "${EnvironmentName}:PrivateSubnet1"
  PrivateSubnet2:
    Description: A reference to the private subnet in the 2nd Availability Zone
    Value: !Ref PrivateSubnet2
    Export:
      Name: !Sub "${EnvironmentName}:PrivateSubnet2"
  VpcCIDR:
    Description: VPC CIDR
    Value: !Ref VpcCIDR
    Export:
      Name: !Sub "${EnvironmentName}:VpcCIDR"

我运行一个bash脚本来收集这些数据(你可以编写javascript,pytyon或任何其他语言来收集这些数据(,将它们作为参数提供给上面的cloudformation。

#!/bin/bash
set -ex
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
# collect the vpc details.
# you can run aws cli or any aws sdk to collect them.
source ../common_function.sh
echo ${VPC_ID}
aws --profile "${AWS_PROFILE}" --region "${AWS_DEFAULT_REGION}" 
    cloudformation deploy 
    --stack-name "${ENVIRONMENT_NAME}-vpc" 
    --capabilities CAPABILITY_IAM 
    --template-file "${DIR}/vpc.yaml" 
    --parameter-overrides 
    EnvironmentName="${ENVIRONMENT_NAME}" 
    VPC="${VPC_ID}" 
    VpcCIDR="${VPC_CIDR}" 
    PublicSubnet1="${PUBLIC_SUBNET_ID_1}" 
    PublicSubnet2="${PUBLIC_SUBNET_ID_2}" 
    PrivateSubnet1="${PRIVATE_SUBNET_ID_1}" 
    PrivateSubnet2="${PRIVATE_SUBNET_ID_2}"

在 cfn 堆栈上部署后,您可以在其他 cfn 堆栈中引用这些输出变量。

VpcId:
    'Fn::ImportValue': !Sub "${EnvironmentName}:VPC"

相关内容

  • 没有找到相关文章

最新更新