Наконец, я создал стек vpc с облачной информацией, он получает входные параметры из aws cli и выводит vpc id, subnet id и т. Д.
Этот шаблон облачной информации имеет только один нулевой ресурс (потому что облачная информация сообщит об ошибке, если в его шаблоне нет ресурса).
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 или любые другие языки для сбора этих данных), чтобы передать их выше облачной информации в качестве параметров.
#!/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"