创建堆栈操作:模板格式错误:模板的资源块中未解析的资源依赖项 [VpcId]



我正在使用多个 CF 模板,并打算跨模板导出和导入值。我导出/导入了大多数值,但是由于某种原因没有导入VpcId,遇到了这个问题。

第一个带导出的模板

Parameters:
StackPrefix:
Type: String
Default: "app-name"
Outputs:
VpcId:
Value: !Ref VPC
Export:
Name: !Join [ ":", [ !Ref StackPrefix, VPC ] ]

第二个带导入的模板

Parameters:
StackPrefix:
Type: String
Default: "app-name"
Resources:
SecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: !Sub ${AWS::StackName}-alb
SecurityGroupIngress:
- CidrIp: "0.0.0.0/0"
IpProtocol: "TCP"
FromPort: 80
ToPort: 80
VpcId:
Fn::ImportValue: !Sub "${StackPrefix}:VPC"

使用aws cli部署模板会导致"模板验证错误">

aws --profile shiny-app cloudformation create-stack --stack-name app-elb --template-body file://02-load-balancer.yaml

An error occurred (ValidationError) when calling the CreateStack operation: Template format error: Unresolved resource dependencies [VpcId] in the Resources block of the template

任何帮助不胜感激

在 AWS 控制台中检查第一个堆栈的输出是否正确,否则请尝试:

!Sub '${AWS::StackName}:VPC'

您无需在第一个堆栈中添加前缀参数,只需使用该名称,然后在第 53 行的第二个堆栈中尝试类似操作,并使用第一个堆栈的名称添加参数 ParentVPCStack。

VpcId: {'Fn::ImportValue': !Sub '${ParentVPCStack}:VPC'}

对我来说,这有效。

经过一个不眠之夜,跑过一个同事,我想回答我自己的问题

@Seth E感谢您的输入,您是对的。

我在第 53-54 行有这个

53       VpcId:
54         Fn::ImportValue: !Sub "${StackPrefix}:VPC"

。在第 117 行中,这出来了

117       VpcId: !Ref VpcId

我认为CloudFormation在很大程度上有助于识别模板中的哪一行有问题的代码,对于这个错误,CloudFormation对我来说还不够清楚。在 CF 中调试(输出值不是那么简单(,我认为这是我需要习惯调试 CF 模板的东西。

An error occurred (ValidationError) when calling the CreateStack operation: Template format error: Unresolved resource dependencies [VpcId] in the Resources block of the template

最新更新