Cloudformation Split & ImportValue

问题描述

我在 CloudFormation 中有 2 个堆栈。一个是创建一个带有几个子网的 vpc,这些子网被导出以便在其他堆栈中使用。这个想法是让这些子网在其他堆栈中使用。 vpc 堆栈正确导出值,但我无法将它们导入到第二个堆栈中,因为它使用字符串列表。

堆栈 1:

  Privatesubnets:
    Description: "A list of the public subnets"
    Value: !Join [ ",",[ !Ref Privatesubnet1,!Ref Privatesubnet2 ]]
    Export:
      Name: !Sub "${StagingArea}-Privatesubnets"
  
  Publicsubnet1:
    Description: "Reference to the publi subnet in the 1st Availability Zone"
    Value: !Ref Publicsubnet1

  Publicsubnet2: 
    Description: "A reference to the public subnet in the 2nd Availability Zone"
    Value: !Ref Publicsubnet2

  Privatesubnet1:
    Description: "A reference to the private subnet in the 1st Availability Zone"
    Value: !Ref Privatesubnet1
    Export:
      Name: !Sub "${StagingArea}-Privatesubnet1"
  Privatesubnet2: 
    Description: "A reference to the private subnet in the 2nd Availability Zone"
    Value: !Ref Privatesubnet2
    Export:
      Name: !Sub "${StagingArea}-Privatesubnet2"

当我尝试将 ImportValue 导入我的第二个堆栈时,它不起作用。下面的堆栈 2:

  DbsubnetGroup:
    Type: AWS::RDS::DBsubnetGroup
    Properties:
      DBsubnetGroupDescription: !Sub "${StagingArea} RDS DB subnet Group"
      subnetIds: 
        - !ImportValue 'Fn::Sub': '{$StagingArea}-Privatesubnet1'
        - !ImportValue 'Fn::Sub': '{$StagingArea}-Privatesubnet2'

是否可以将第一个堆栈中导出的值导入到第二个堆栈中?我尝试了各种选择,但似乎没有一个有效。

解决方法

Sub不正确(错误使用 $),最好遵循 Ec2 pricing 并将语句分成两行:

        - Fn::ImportValue:
            !Sub '${StagingArea}-PrivateSubnet1'
        - Fn::ImportValue:
            !Sub '${StagingArea}--PrivateSubnet2'