微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cloudformation Split & ImportValue

如何解决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'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。