简体   繁体   English

如何在 cloudformation 中引用 ImportValue 中的变量?

[英]How can I ref a variable in ImportValue in cloudformation?

I have a cloudformation template and need to import a value based on passed in parameter.我有一个 cloudformation 模板,需要根据传入的参数导入一个值。 Below is the code.下面是代码。 But I can't combine !ImportValue and !Ref .但我不能结合!ImportValue!Ref How can I use the EnvironmentName in ImportValue function?如何在ImportValue function 中使用EnvironmentName

Parameters:

    EnvironmentName:
        Description: An environment name 
        Type: String

...

VpcConfig:
        SecurityGroupIds:
          - !ImportValue # how can I reference EnvironmentName
...

As shown in the AWS docs you can use ImportValue with Sub to achieve what you want:AWS 文档中所示,您可以将ImportValueSub一起使用来实现您想要的:

VpcConfig:
        SecurityGroupIds:
          - Fn::ImportValue: 
              !Sub "${EnvironmentName}"

You may need to adjust indentations to match your template.您可能需要调整缩进以匹配您的模板。

Lets say Environment is a Parameter in both templates可以说Environment是两个模板中的参数

Security Group in Template 1:模板 1 中的安全组:

MySecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
        GroupName: !Sub 'My Group - ${Environment}'
        GroupDescription: 'my group'
        SecurityGroupIngress:
            - IpProtocol: 'icmp'
              FromPort: '-1'
              ToPort: '-1'
              CidrIp: '0.0.0.0/0'

Exported from Template 1:从模板 1 导出:

MySecurityGroup:
    Description: 'Security Group Test'
    Value: !Ref 'MySecurityGroup'
    Export:
        Name: !Sub 'MySecurityGroup-${Environment}'

Imported as导入为

VpcConfig:
    SecurityGroupIds:
                - Fn::ImportValue: !Sub MySecurityGroup-${Environment}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM