本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Outputs
區段中的 Fn::ForEach
範例
這些範例會示範如何使用 Outputs CloudFormation 範本的 區段語法參考 區段中的 Fn::ForEach
內部函數。
參考複寫的 AWS::S3::Bucket
資源
此範例會使用 Outputs CloudFormation 範本的 區段語法參考 區段中的巢狀 Fn::ForEach
迴圈,來縮短範本長度。
JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Resources": { "Fn::ForEach::Buckets": [ "Identifier", [ "A", "B", "C" ], { "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", [ "A", "B", "C" ], { "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' Resources: 'Fn::ForEach::Buckets': - Identifier - [A, B, C] - 'S3Bucket${Identifier}': Type: 'AWS::S3::Bucket' Properties: AccessControl: PublicRead MetricsConfigurations: - Id: !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 - [A, B, C] - 'Fn::ForEach::GetAttLoop': - Property - [Arn, DomainName, WebsiteURL] - 'S3Bucket${Identifier}${Property}': Value: !GetAtt [!Sub 'S3Bucket${Identifier}', !Ref Property]
轉換後範本將等同於下列範本:
AWSTemplateFormatVersion: 2010-09-09 Transform: 'AWS::LanguageExtensions' Resources: S3BucketA: Type: 'AWS::S3::Bucket' Properties: AccessControl: PublicRead MetricsConfigurations: - Id: EntireBucketA 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 S3BucketB: Type: 'AWS::S3::Bucket' Properties: AccessControl: PublicRead MetricsConfigurations: - Id: EntireBucketB 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 S3BucketC: Type: 'AWS::S3::Bucket' Properties: AccessControl: PublicRead MetricsConfigurations: - Id: EntireBucketC 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: S3BucketAArn: Value: !GetAtt [S3BucketA, Arn] S3BucketADomainName: Value: !GetAtt [S3BucketA, DomainName] S3BucketAWebsiteURL: Value: !GetAtt [S3BucketA, WebsiteURL] S3BucketBArn: Value: !GetAtt [S3BucketB, Arn] S3BucketBDomainName: Value: !GetAtt [S3BucketB, DomainName] S3BucketBWebsiteURL: Value: !GetAtt [S3BucketB, WebsiteURL] S3BucketCArn: Value: !GetAtt [S3BucketC, Arn] S3BucketCDomainName: Value: !GetAtt [S3BucketC, DomainName] S3BucketCWebsiteURL: Value: !GetAtt [S3BucketC, WebsiteURL]
參考複寫的 AWS::EC2::Instance
資源
此範例使用產生的邏輯 ID 在 Resources CloudFormation 範本的 區段語法參考 中參考複寫的資源。
JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Mappings": { "Instances": { "InstanceType": { "B": "m5.4xlarge", "C": "c5.2xlarge" }, "ImageId": {"A": "ami-id1"} } }, "Resources": { "Fn::ForEach::Instances": [ "Identifier", [ "A", "B", "C" ], { "Instance${Identifier}": { "Type": "AWS::EC2::Instance", "Properties": { "InstanceType": { "Fn::FindInMap": [ "Instances", "InstanceType", {"Ref": "Identifier"}, {"DefaultValue": "m5.xlarge"} ] }, "ImageId": { "Fn::FindInMap": [ "Instances", "ImageId", {"Ref": "Identifier"}, {"DefaultValue": "ami-id-default"} ] } } } } ] }, "Outputs": { "SecondInstanceId": { "Description": "Instance Id for InstanceB", "Value": {"Ref": "InstanceB"} }, "SecondPrivateIp": { "Description": "Private IP for InstanceB", "Value": { "Fn::GetAtt": [ "InstanceB", "PrivateIp" ] } } } }
YAML
AWSTemplateFormatVersion: 2010-09-09 Transform: 'AWS::LanguageExtensions' Mappings: Instances: InstanceType: B: m5.4xlarge C: c5.2xlarge ImageId: A: ami-id1 Resources: 'Fn::ForEach::Instances': - Identifier - [A, B, C] - 'Instance${Identifier}': Type: 'AWS::EC2::Instance' Properties: InstanceType: !FindInMap [Instances, InstanceType, !Ref Identifier, DefaultValue: m5.xlarge] ImageId: !FindInMap [Instances, ImageId, !Ref Identifier, DefaultValue: ami-id-default] Outputs: SecondInstanceId: Description: Instance Id for InstanceB Value: !Ref InstanceB SecondPrivateIp: Description: Private IP for InstanceB Value: !GetAtt [InstanceB, PrivateIp]
轉換後範本將等同於下列範本:
AWSTemplateFormatVersion: 2010-09-09 Transform: 'AWS::LanguageExtensions' Resources: InstanceA: Type: 'AWS::EC2::Instance' Properties: InstanceType: m5.xlarge ImageId: ami-id1 InstanceB: Type: 'AWS::EC2::Instance' Properties: InstanceType: m5.4xlarge ImageId: ami-id-default InstanceC: Type: 'AWS::EC2::Instance' Properties: InstanceType: c5.2xlarge ImageId: ami-id-default Outputs: SecondInstanceId: Description: Instance Id for InstanceB Value: !Ref InstanceB SecondPrivateIp: Description: Private IP for InstanceB Value: !GetAtt [InstanceB, PrivateIp]