CreateStack操作:模板格式错误:模板的“资源”块中未解决的资源依赖性[VpcId]

时间:2019-10-21 18:02:49

标签: amazon-web-services yaml amazon-cloudformation

我正在使用多个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

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

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

@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

答案 1 :(得分:0)

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

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

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

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

对我来说这可行。