简体   繁体   中英

AWS Cloudformation Master Template creation

I am trying to create a master template which calls other templates.My first template is VPC and subnet creation and second one is creating bastion host. Problem i am facing is i am not able to ref. the created VPC in my second template due to which its failing. My master template looks like below:-

Description: >

    This template deploys the full agyle time stack as follows, which consists of:

    A VPC with with public and private subnets spread across two Availabilty Zones.
    It deploys an Internet Gateway and a pair of NAT Gateways, with the relevant routes in each of the subnets.

    It then deploys the API ECS cluster distributed across multiple Availability Zones.

    Finally, it deploys the API ECS services deployed as containers within the ECS repository
Parameters:
    S3TemplateKeyPrefix:
        Description: >
            An S3 key prefix which will be used to resolve referenced templates
        Type: String

Resources:

    VPC:
        Type: AWS::CloudFormation::Stack
        Properties:
            TemplateURL: !Sub ${S3TemplateKeyPrefix}/infrastructure/vpc.yaml

    Bastion:
        Type: AWS::CloudFormation::Stack
        Properties:
            TemplateURL: !Sub ${S3TemplateKeyPrefix}/infrastructure/bastion.yaml
            Parameters: 
                EnvironmentName: !Ref AWS::StackName
                VPC: !GetAtt VPC.Outputs.VPC

Can someone help me here do i have to modify VPC and Bastion host template to reference my VPC in bastion template.

Based on your master template, I believe it fails because CFN starts creating both of them in parallel, whereas Bastion needs to be created after your VPC resource. Just add the DependsOn: VPC for your Bastion resource to have it created only after your VPC has been created.

Bastion:
  Type: AWS::CloudFormation::Stack
  DependsOn: VPCStack
  Properties:

Here's a working example from AWS saas-identity-cognito-master.template .

I was able to resolve the issue with modifying the child templates with Export and Import Function and calling it in master template. below is what I used:-

Outputs:

PubPrivateVPC: 
    Description: A reference to the created VPC
    Value: !Ref PubPrivateVPC
    Export:
      Name: VPC-PROD

and import

Parameter:- NetworkStackName: Description: >- Name of an active CloudFormation stack that contains the networking resources, such as the subnet and security group, that will be used in this stack. Type: String MinLength: 1 MaxLength: 255 AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$' Default: VPC-PROD

and in resources called like below:- VpcId: !ImportValue VPC-PROD

No i am able to call child templates in master successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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