我正在尝试定义一些公共(public)资源(特别是几个 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.yaml
和 app_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 ]
第一个环境创建良好,包括嵌套资源。当我尝试运行第二个环境时,它失败并出现错误:
Embedded stack arn:aws:cloudformation:eu-west-1:238165151424:stack/my-prod-stack-CommonResources-L94ZCIP0UD9W/f9d06dd0-994d-11eb-9802-02554f144c21 was not successfully created: The following resource(s) failed to create: [LambdaExecuteRole, LambdaFunctionSG].
是否不可能像这样在两个单独的堆栈之间共享单个资源定义,或者我在实现中错过了一些东西?
最佳答案
正如 @jasonwadsworth 提到的,堆栈的正确名称总是在末尾用随机字符串修改 AWS::CloudFormation::Stack检查返回值。使用 GetAtt 获取堆栈的名称并构造输出。 How do I pass values between nested stacks within the same parent stack in 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 exports如果您好奇的话 Difference between an Output & an Export
关于amazon-web-services - 无法创建具有嵌套资源的 Cloudformation 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67047707/