簡體   English   中英

具有嵌套資源的 Cloudformation 堆棧無法創建

[英]Cloudformation stack with nested resources fails to create

我正在嘗試通過嵌套堆棧定義將在兩個環境之間共享的一些公共資源(特別是幾個 IAM 角色)。 使用嵌套堆棧資源的第一個環境創建正常,但第二個環境在嘗試運行嵌套堆棧時失敗。 我是不了解嵌套堆棧的工作原理,還是我做錯了什么?

我的嵌套堆棧定義為:

AWSTemplateFormatVersion: '2010-09-09'
Description: Defines common resources shared between environments.

Parameters:
    ParentStage:
    Type: String
    Description: The Stage or environment name.
    Default: ""
    ParentVpcId:
    Type: "AWS::EC2::VPC::Id"
    Description: VpcId of your existing Virtual Private Cloud (VPC)
    Default: ""

Resources:

    LambdaFunctionSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
        GroupDescription: Identifies Lamdba functions to VPC resources
        GroupName: BBA-KTDO-SG-LambdaFunction
        VpcId: !Ref ParentVpcId

    RdsAccessSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
        GroupDescription: Allows Lambda functions access to RDS instances
        GroupName: BBA-KTDO-SG-RDS
        SecurityGroupIngress:
        - IpProtocol: tcp
            FromPort: 3306
            ToPort: 3306
            SourceSecurityGroupId: !Ref LambdaFunctionSG
        VpcId: !Ref ParentVpcId

我已將該 YAML 文件上傳到 S3 存儲桶,然后嘗試在兩個單獨的堆棧文件中使用它(即app_dev.yamlapp_prod.yaml ):

Resources:

    CommonResources:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
        TemplateURL: "https://my-buildbucket.s3-eu-west-1.amazonaws.com/common/common.yaml"
        Parameters:
        ParentStage: !Ref Stage
        ParentVpcId: !Ref VpcId

並將其輸出稱為(例如):

VpcConfig:
    SecurityGroupIds:
        - !GetAtt [ CommonResources, Outputs.LambdaFunctionSGId ]

第一個環境創建良好,包括嵌套資源。 當我嘗試運行第二個環境時,它失敗並出現錯誤:

嵌入式堆棧 arn:aws:cloudformation:eu-west-1:238165151424:stack/my-prod-stack-CommonResources-L94ZCIP0UD9W/f9d06dd0-994d-11eb-9802-02554f144c21 未成功創建:以下資源未能成功創建創建:[LambdaExecuteRole,LambdaFunctionSG]。

像這樣在兩個單獨的堆棧之間共享單個資源定義是不可能的,或者我只是在實現中遺漏了一些東西?

正如@jasonwadsworth 提到的,堆棧的正確名稱總是在AWS::CloudFormation::Stack末尾用隨機字符串修改,檢查返回值。 使用GetAtt獲取堆棧的名稱並構造 output。 如何在 AWS CloudFormation 的同一父堆棧內的嵌套堆棧之間傳遞值?

加上使用aws cloudformation package命令打包嵌套堆棧,無需手動將它們上傳到 s3 存儲桶。

有點像

aws cloudformation package \
--template-file /path_to_template/template.json \
--s3-bucket bucket-name \
--output-template-file packaged-template.json

如果您好奇,請查看cloudformation output 導出以及Output 和導出之間的區別

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM