AWS Cloudformation主模板创建

时间:2019-02-19 03:00:42

标签: amazon-web-services amazon-cloudformation

我正在尝试创建一个调用其他模板的主模板。我的第一个模板是VPC和子网创建,第二个模板是创建堡垒主机。我面临的问题是我无法参考。由于第二个模板失败而在我的第二个模板中创建了VPC。我的主模板如下所示:-

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

有人可以帮我吗,我是否必须修改VPC和堡垒主机模板以在堡垒模板中引用我的VPC。

2 个答案:

答案 0 :(得分:0)

基于主模板,我相信它会失败,因为CFN开始并行创建两个模板,而Bastion需要在您的VPC资源之后创建。只需为您的DependsOn: VPC资源添加Bastion,就可以在创建VPC之后创建它。

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

这是AWS saas-identity-cognito-master.template中的一个有效示例。

答案 1 :(得分:0)

我能够通过使用“导出和导入功能”修改子模板并在主模板中调用子模板来解决该问题。下面是我用的:-

输出:

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

并导入

参数:-   NetworkStackName:     说明:>-       包含网络的活动CloudFormation堆栈的名称       将用于以下资源的资源,例如子网和安全组       这个堆栈。     类型:字符串     最小长度:1     最大长度:255     AllowedPattern:'^ [a-zA-Z] [-a-zA-Z0-9] * $'     默认值:VPC-PROD

以及在如下所示的资源中:       VpcId:!ImportValue VPC-PROD

否,我无法成功调用master中的子模板。

相关问题