AWS Cloudformation !Ref SecurityGroup returns 无效 ID

AWS Cloudformation !Ref SecurityGroup returns an invalid ID

我想通过 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 时会发生该错误。请注意,我已将堆栈名称更改为 "Stackname".

Invalid Id: \"Stackname-Security-N12M8127812\" (expecting \"sg-\")

所以我猜 !Ref 不是 return 安全组的 ID,而是 return 的名称。 有办法获取id吗?

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

SourceSecurityGroupId: !GetAtt Security.GroupId