AWS Cloudformation !Ref SecurityGroup 返回无效 ID

Jun*_*nge 4 amazon-web-services aws-cloudformation aws-security-group

我想通过 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
Run Code Online (Sandbox Code Playgroud)

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

无效 ID:“Stackname-Security-N12M8127812”(需要“sg-”)

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

小智 13

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

SourceSecurityGroupId: !GetAtt Security.GroupId
Run Code Online (Sandbox Code Playgroud)

  • 文档中不是很清楚。doco 告诉您,有时您会得到姓名,有时会得到 ID。AWS 代码片段有时假设其中之一,有时则假设另一个。所以这是一个好问题。 (8认同)