Fn::GetAtt - AWS CloudFormation

Fn::GetAtt

Fn::GetAtt 内部函数返回模板中的资源的属性值。使用 AWS::LanguageExtensions 转换 转换时,您可以使用内置函数作为 Fn::GetAtt 的参数。有关特定资源的 GetAtt 返回值的更多信息,请参阅 资源和属性参考 中该资源的文档。

声明

JSON

{ "Fn::GetAtt" : [ "logicalNameOfResource", "attributeName" ] }

YAML

完整函数名称的语法:

Fn::GetAtt: [ logicalNameOfResource, attributeName ]

短格式的语法:

!GetAtt logicalNameOfResource.attributeName

参数

logicalNameOfResource

包含所需属性的资源的逻辑名称 (也称为逻辑 ID)。

attributeName

您想要获得其值的资源特定属性名称。有关该资源类型之可用属性的详细信息,请参阅资源的引用页面。

返回值

属性值。

示例

返回字符串

此示例代码段返回的字符串包含逻辑名称为 myELB 的负载均衡器的 DNS 名称。

JSON

"Fn::GetAtt" : [ "myELB" , "DNSName" ]

YAML

!GetAtt myELB.DNSName

返回多个字符串

以下示例模板将返回逻辑名称为 SourceSecurityGroup.OwnerAlias 的负载均衡器 SourceSecurityGroup.GroupNamemyELB

JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "myELB": { "Type": "AWS::ElasticLoadBalancing::LoadBalancer", "Properties": { "AvailabilityZones": [ "eu-west-1a" ], "Listeners": [ { "LoadBalancerPort": "80", "InstancePort": "80", "Protocol": "HTTP" } ] } }, "myELBIngressGroup": { "Type": "AWS::EC2::SecurityGroup", "Properties": { "GroupDescription": "ELB ingress group", "SecurityGroupIngress": [ { "IpProtocol": "tcp", "FromPort": 80, "ToPort": 80, "SourceSecurityGroupOwnerId": { "Fn::GetAtt": [ "myELB", "SourceSecurityGroup.OwnerAlias" ] }, "SourceSecurityGroupName": { "Fn::GetAtt": [ "myELB", "SourceSecurityGroup.GroupName" ] } } ] } } } }
YAML
AWSTemplateFormatVersion: 2010-09-09 Resources: myELB: Type: AWS::ElasticLoadBalancing::LoadBalancer Properties: AvailabilityZones: - eu-west-1a Listeners: - LoadBalancerPort: '80' InstancePort: '80' Protocol: HTTP myELBIngressGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: ELB ingress group SecurityGroupIngress: - IpProtocol: tcp FromPort: 80 ToPort: 80 SourceSecurityGroupOwnerId: !GetAtt myELB.SourceSecurityGroup.OwnerAlias SourceSecurityGroupName: !GetAtt myELB.SourceSecurityGroup.GroupName

示例:使用 Fn::GetAttRefFn::ForEach 中的 Fn::Sub,为多个资源使用标准标识符

以下示例说明如何在模板的 Outputs 中将 Fn::GetAttFn::SubFn::ForEach 结合使用,以减少模板的长度和详细程度。在 Fn::GetAtt 中使用 Fn::Sub 能使模板包含一个内置函数,该函数可以在每次迭代 Fn::ForEach 调用时引用不同的存储桶。

JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Mappings": { "Buckets": { "Properties": { "Identifiers": ["A", "B", "C"] } } }, "Resources": { "Fn::ForEach::Buckets": [ "Identifier", {"Fn::FindInMap": ["Buckets", "Properties", "Identifiers"]}, { "S3Bucket${Identifier}": { "Type": "AWS::S3::Bucket", "Properties": { "AccessControl": "PublicRead", "MetricsConfigurations": [ { "Id": {"Fn::Sub": "EntireBucket${Identifier}"} } ], "WebsiteConfiguration": { "IndexDocument": "index.html", "ErrorDocument": "error.html", "RoutingRules": [ { "RoutingRuleCondition": { "HttpErrorCodeReturnedEquals": "404", "KeyPrefixEquals": "out1/" }, "RedirectRule": { "HostName": "ec2-11-22-333-44.compute-1.amazonaws.com", "ReplaceKeyPrefixWith": "report-404/" } } ] } }, "DeletionPolicy": "Retain", "UpdateReplacePolicy": "Retain" } } ] }, "Outputs": { "Fn::ForEach::BucketOutputs": [ "Identifier", {"Fn::FindInMap": ["Buckets", "Properties", "Identifiers"]}, { "Fn::ForEach::GetAttLoop": [ "Property", ["Arn", "DomainName", "WebsiteURL"], { "S3Bucket${Identifier}${Property}": { "Value": { "Fn::GetAtt": [{"Fn::Sub": "S3Bucket${Identifier}"}, {"Ref": "Property"}] } } } ] } ] } }
YAML
AWSTemplateFormatVersion: 2010-09-09 Transform: 'AWS::LanguageExtensions' Mappings: Buckets: Properties: Identifiers: - A - B - C Resources: 'Fn::ForEach::Buckets': - Identifier - Fn::FindInMap: - Buckets - Properties - Identifiers - 'S3Bucket${Identifier}': Type: 'AWS::S3::Bucket' Properties: AccessControl: PublicRead MetricsConfigurations: - Id: Fn::Sub: 'EntireBucket${Identifier}' WebsiteConfiguration: IndexDocument: index.html ErrorDocument: error.html RoutingRules: - RoutingRuleCondition: HttpErrorCodeReturnedEquals: '404' KeyPrefixEquals: out1/ RedirectRule: HostName: ec2-11-22-333-44.compute-1.amazonaws.com ReplaceKeyPrefixWith: report-404/ DeletionPolicy: Retain UpdateReplacePolicy: Retain Outputs: 'Fn::ForEach::BucketOutputs': - Identifier - Fn::FindInMap: - Buckets - Properties - Identifiers - 'Fn::ForEach::GetAttLoop': - Property - - Arn - DomainName - WebsiteURL - 'S3Bucket${Identifier}${Property}': Value: !GetAtt - !Sub 'S3Bucket${Identifier}' - !Ref Property

支持的函数

不使用 AWS::LanguageExtensions 转换 转换时:

  • Fn::GetAtt 逻辑资源名称不能使用函数。必须指定作为资源逻辑 ID 的字符串。对于 Fn::GetAtt 属性名称,可以使用 Ref 函数。

  • Fn::GetAtt 属性名称可以使用 Ref 函数。

Fn::GetAtt 函数中使用 AWS::LanguageExtensions 转换 转换时,您可以使用以下函数。对于 Fn::GetAtt 逻辑资源名称或 Fn::GetAtt 属性名称都是如此。