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

您需要的資源特定屬性的名稱。有關該資源類型可用屬性的詳細資訊,請參閱該資源的參考頁面。

傳回值

屬性值。

範例

傳回字串

此範例程式碼片段會傳回一個字串,其中包含負載平衡器的 DNS 名稱,此負載平衡器的邏輯名稱為 myELB

JSON

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

YAML

!GetAtt myELB.DNSName

傳回多個字串

以下範例範本傳回負載平衡器的 SourceSecurityGroup.OwnerAliasSourceSecurityGroup.GroupName,此負載平衡器的邏輯名稱為 myELB

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 的多項資源使用標準識別碼

下列範例顯示如何在範本的 輸出 中搭配使用 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 函數。

當使用 AWS::LanguageExtensions 轉換程序 轉換時,在 Fn::GetAtt 函數內,您可以使用以下函數。對於 Fn::GetAtt 邏輯資源名稱或 Fn::GetAtt 屬性名稱都是如此。