Amazon S3 模板代码段 - AWS CloudFormation

Amazon S3 模板代码段

使用这些 Amazon S3 示例模板帮助在 AWS CloudFormation 中描述 Amazon S3 桶。有关更多示例,请参阅 AWS::S3::Bucket 资源中的示例部分。

使用默认值创建 Amazon S3 存储桶

此示例使用 AWS::S3::Bucket 创建带默认设置的桶。

JSON

"myS3Bucket" : { "Type" : "AWS::S3::Bucket" }

YAML

MyS3Bucket: Type: AWS::S3::Bucket

为虚拟主机创建一个带 DeletionPolicy 的 Amazon S3 存储桶

此示例将创建一个存储桶作为网站,并禁用阻止公有访问(为网站托管设置的存储桶需要公有读取权限)。然后,将公有存储桶策略添加到存储桶。由于此存储桶资源有一个设置为 RetainDeletionPolicy attribute,AWS CloudFormation 不会在其删除堆栈时删除此存储桶。Output 部分使用 Fn::GetAtt 检索 S3Bucket 资源的 WebsiteURL 属性和 DomainName 属性。

注意

以下示例假设 BlockPublicPolicyRestrictPublicBuckets 阻止公有访问设置已在账户级别禁用。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "S3Bucket": { "Type": "AWS::S3::Bucket", "Properties": { "PublicAccessBlockConfiguration": { "BlockPublicAcls": false, "BlockPublicPolicy": false, "IgnorePublicAcls": false, "RestrictPublicBuckets": false }, "WebsiteConfiguration": { "IndexDocument": "index.html", "ErrorDocument": "error.html" } }, "DeletionPolicy": "Retain", "UpdateReplacePolicy": "Retain" }, "BucketPolicy": { "Type": "AWS::S3::BucketPolicy", "Properties": { "PolicyDocument": { "Id": "MyPolicy", "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadForGetBucketObjects", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": { "Fn::Join": [ "", [ "arn:aws:s3:::", { "Ref": "S3Bucket" }, "/*" ] ] } } ] }, "Bucket": { "Ref": "S3Bucket" } } } }, "Outputs": { "WebsiteURL": { "Value": { "Fn::GetAtt": [ "S3Bucket", "WebsiteURL" ] }, "Description": "URL for website hosted on S3" }, "S3BucketSecureURL": { "Value": { "Fn::Join": [ "", [ "https://", { "Fn::GetAtt": [ "S3Bucket", "DomainName" ] } ] ] }, "Description": "Name of S3 bucket to hold website content" } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: S3Bucket: Type: 'AWS::S3::Bucket' Properties: PublicAccessBlockConfiguration: BlockPublicAcls: false BlockPublicPolicy: false IgnorePublicAcls: false RestrictPublicBuckets: false WebsiteConfiguration: IndexDocument: index.html ErrorDocument: error.html DeletionPolicy: Retain UpdateReplacePolicy: Retain BucketPolicy: Type: 'AWS::S3::BucketPolicy' Properties: PolicyDocument: Id: MyPolicy Version: 2012-10-17 Statement: - Sid: PublicReadForGetBucketObjects Effect: Allow Principal: '*' Action: 's3:GetObject' Resource: !Join - '' - - 'arn:aws:s3:::' - !Ref S3Bucket - /* Bucket: !Ref S3Bucket Outputs: WebsiteURL: Value: !GetAtt - S3Bucket - WebsiteURL Description: URL for website hosted on S3 S3BucketSecureURL: Value: !Join - '' - - 'https://' - !GetAtt - S3Bucket - DomainName Description: Name of S3 bucket to hold website content

使用自定义域创建静态网站

可以将 Route 53 和已注册的域一起使用。以下示例假设您已在 Route 53 中为您的域创建了托管区域。该示例创建两个用于网站托管的存储桶。根存储桶托管内容,另一个存储桶将 www.domainname.com 请求重定向到根存储桶。该记录集将您的域名映射到 Amazon S3 端点。

您还需要添加存储桶策略,如上面的示例所示。

有关使用自定义域的更多信息,请参阅《Amazon Simple Storage Service 用户指南》中的教程:使用注册到 Route 53 的自定义域配置静态网站

注意

以下示例假设 BlockPublicPolicyRestrictPublicBuckets 阻止公有访问设置已在账户级别禁用。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Mappings" : { "RegionMap" : { "us-east-1" : { "S3hostedzoneID" : "Z3AQBSTGFYJSTF", "websiteendpoint" : "s3-website-us-east-1.amazonaws.com" }, "us-west-1" : { "S3hostedzoneID" : "Z2F56UZL2M1ACD", "websiteendpoint" : "s3-website-us-west-1.amazonaws.com" }, "us-west-2" : { "S3hostedzoneID" : "Z3BJ6K6RIION7M", "websiteendpoint" : "s3-website-us-west-2.amazonaws.com" }, "eu-west-1" : { "S3hostedzoneID" : "Z1BKCTXD74EZPE", "websiteendpoint" : "s3-website-eu-west-1.amazonaws.com" }, "ap-southeast-1" : { "S3hostedzoneID" : "Z3O0J2DXBE1FTB", "websiteendpoint" : "s3-website-ap-southeast-1.amazonaws.com" }, "ap-southeast-2" : { "S3hostedzoneID" : "Z1WCIGYICN2BYD", "websiteendpoint" : "s3-website-ap-southeast-2.amazonaws.com" }, "ap-northeast-1" : { "S3hostedzoneID" : "Z2M4EHUR26P7ZW", "websiteendpoint" : "s3-website-ap-northeast-1.amazonaws.com" }, "sa-east-1" : { "S3hostedzoneID" : "Z31GFT0UA1I2HV", "websiteendpoint" : "s3-website-sa-east-1.amazonaws.com" } } }, "Parameters": { "RootDomainName": { "Description": "Domain name for your website (example.com)", "Type": "String" } }, "Resources": { "RootBucket": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName" : {"Ref":"RootDomainName"}, "PublicAccessBlockConfiguration": { "BlockPublicAcls": false, "BlockPublicPolicy": false, "IgnorePublicAcls": false, "RestrictPublicBuckets": false }, "WebsiteConfiguration": { "IndexDocument":"index.html", "ErrorDocument":"404.html" } } }, "WWWBucket": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": { "Fn::Join": ["", ["www.", {"Ref":"RootDomainName"}]] }, "AccessControl": "BucketOwnerFullControl", "WebsiteConfiguration": { "RedirectAllRequestsTo": { "HostName": {"Ref": "RootBucket"} } } } }, "myDNS": { "Type": "AWS::Route53::RecordSetGroup", "Properties": { "HostedZoneName": { "Fn::Join": ["", [{"Ref": "RootDomainName"}, "."]] }, "Comment": "Zone apex alias.", "RecordSets": [ { "Name": {"Ref": "RootDomainName"}, "Type": "A", "AliasTarget": { "HostedZoneId": {"Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "S3hostedzoneID"]}, "DNSName": {"Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "websiteendpoint"]} } }, { "Name": { "Fn::Join": ["", ["www.", {"Ref":"RootDomainName"}]] }, "Type": "CNAME", "TTL" : "900", "ResourceRecords" : [ {"Fn::GetAtt":["WWWBucket", "DomainName"]} ] } ] } } }, "Outputs": { "WebsiteURL": { "Value": {"Fn::GetAtt": ["RootBucket", "WebsiteURL"]}, "Description": "URL for website hosted on S3" } } }

YAML

Parameters: RootDomainName: Description: Domain name for your website (example.com) Type: String Mappings: RegionMap: us-east-1: S3hostedzoneID: Z3AQBSTGFYJSTF websiteendpoint: s3-website-us-east-1.amazonaws.com us-west-1: S3hostedzoneID: Z2F56UZL2M1ACD websiteendpoint: s3-website-us-west-1.amazonaws.com us-west-2: S3hostedzoneID: Z3BJ6K6RIION7M websiteendpoint: s3-website-us-west-2.amazonaws.com eu-west-1: S3hostedzoneID: Z1BKCTXD74EZPE websiteendpoint: s3-website-eu-west-1.amazonaws.com ap-southeast-1: S3hostedzoneID: Z3O0J2DXBE1FTB websiteendpoint: s3-website-ap-southeast-1.amazonaws.com ap-southeast-2: S3hostedzoneID: Z1WCIGYICN2BYD websiteendpoint: s3-website-ap-southeast-2.amazonaws.com ap-northeast-1: S3hostedzoneID: Z2M4EHUR26P7ZW websiteendpoint: s3-website-ap-northeast-1.amazonaws.com sa-east-1: S3hostedzoneID: Z31GFT0UA1I2HV websiteendpoint: s3-website-sa-east-1.amazonaws.com Resources: RootBucket: Type: AWS::S3::Bucket Properties: BucketName: !Ref RootDomainName PublicAccessBlockConfiguration: BlockPublicAcls: false BlockPublicPolicy: false IgnorePublicAcls: false RestrictPublicBuckets: false WebsiteConfiguration: IndexDocument: index.html ErrorDocument: 404.html WWWBucket: Type: AWS::S3::Bucket Properties: BucketName: !Sub - www.${Domain} - Domain: !Ref RootDomainName AccessControl: BucketOwnerFullControl WebsiteConfiguration: RedirectAllRequestsTo: HostName: !Ref RootBucket myDNS: Type: AWS::Route53::RecordSetGroup Properties: HostedZoneName: !Sub - ${Domain}. - Domain: !Ref RootDomainName Comment: Zone apex alias. RecordSets: - Name: !Ref RootDomainName Type: A AliasTarget: HostedZoneId: !FindInMap [ RegionMap, !Ref 'AWS::Region', S3hostedzoneID] DNSName: !FindInMap [ RegionMap, !Ref 'AWS::Region', websiteendpoint] - Name: !Sub - www.${Domain} - Domain: !Ref RootDomainName Type: CNAME TTL: 900 ResourceRecords: - !GetAtt WWWBucket.DomainName Outputs: WebsiteURL: Value: !GetAtt RootBucket.WebsiteURL Description: URL for website hosted on S3