【问题标题】:AWS Cloudformation !Ref SecurityGroup returns an invalid IDAWS Cloudformation !Ref SecurityGroup 返回无效 ID
【发布时间】:2019-12-02 06:00:40
【问题描述】:

我想通过 cloudformation 部署一个带有 SecurityGroup 入口规则的 SecurityGroup。

我目前在 yaml 文件中使用这个:

Security
  Type: AWS::EC2::SecurityGroup
  Properties:
    GroupDescription: Securitygroup with access to itself
SecurityIngress:
  Type: AWS::EC2::SecurityGroupIngress
  Properties:
    GroupId: !Ref Security
    SourceSecurityGroupId: !Ref Security
    IpProtocol: tcp
    FromPort: -1

这会给我一个错误,指出 SucurityGroupId 格式错误。创建 SecurityIngress 时会发生该错误。请注意,我已将堆栈名称更改为“堆栈名称”。

ID 无效:\"Stackname-Security-N12M8127812\"(应为 \"sg-\")

所以我猜 !Ref 不会返回 SecurityGroup 的 ID,而是返回名称。 有没有办法获取id?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-security-group


    【解决方案1】:

    使用!Ref 将返回资源名称。 documentation 中明确提到了这一点。您需要使用!GetAtt 来获取资源属性之一,包括安全组ID。

    SourceSecurityGroupId: !GetAtt Security.GroupId
    

    【讨论】:

    • 文档中不是很清楚。 doco 告诉你,有时你会得到名字,有时会得到 ID。 AWS sn-ps 有时假设一个,有时假设另一个。所以这是一个很好的问题。
    猜你喜欢
    • 2018-04-08
    • 2020-11-26
    • 2019-08-31
    • 2020-12-20
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多