首页 > 解决方案 > CreateStack 操作:模板格式错误:模板的 Resources 块中未解析的资源依赖项 [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

任何帮助表示赞赏

标签: amazon-web-servicesyamlamazon-cloudformation

解决方案


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

@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

推荐阅读