部署到 CloudFront 的最新静态网站示例
Up to date example of static website deployed to CloudFront
有谁知道是否有使用 AWS CDK 将静态网站部署到 CloudFront 的最新示例或教程?
我也有兴趣使用 Lambda@Edge 进行一些路径重写,如 here 所述。
上次我看这个时,似乎没有办法为 S3 资产指定静态目标存储桶,所以我开始跟踪 this issue,这似乎已经解决。
我将不胜感激有关最佳实践的任何建议或文档。
这是在 Typescript 中使用 AWS CDK 的官方示例:https://github.com/aws-samples/aws-cdk-examples/blob/master/typescript/static-site/static-site.ts
相关部分在static-site.ts
:
const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: props.domainName });
const siteDomain = props.siteSubDomain + '.' + props.domainName;
new cdk.CfnOutput(this, 'Site', { value: 'https://' + siteDomain });
// Content bucket
const siteBucket = new s3.Bucket(this, 'SiteBucket', {
bucketName: siteDomain,
websiteIndexDocument: 'index.html',
websiteErrorDocument: 'error.html',
publicReadAccess: true,
// The default removal policy is RETAIN, which means that cdk destroy will not attempt to delete
// the new bucket, and it will remain in your account until manually deleted. By setting the policy to
// DESTROY, cdk destroy will attempt to delete the bucket, but will error if the bucket is not empty.
removalPolicy: cdk.RemovalPolicy.DESTROY, // NOT recommended for production code
});
new cdk.CfnOutput(this, 'Bucket', { value: siteBucket.bucketName });
// TLS certificate
const certificateArn = new acm.DnsValidatedCertificate(this, 'SiteCertificate', {
domainName: siteDomain,
hostedZone: zone
}).certificateArn;
new cdk.CfnOutput(this, 'Certificate', { value: certificateArn });
// CloudFront distribution that provides HTTPS
const distribution = new cloudfront.CloudFrontWebDistribution(this, 'SiteDistribution', {
aliasConfiguration: {
acmCertRef: certificateArn,
names: [ siteDomain ],
sslMethod: cloudfront.SSLMethod.SNI,
securityPolicy: cloudfront.SecurityPolicyProtocol.TLS_V1_1_2016,
},
originConfigs: [
{
s3OriginSource: {
s3BucketSource: siteBucket
},
behaviors : [ {isDefaultBehavior: true}],
}
]
});
new cdk.CfnOutput(this, 'DistributionId', { value: distribution.distributionId });
// Route53 alias record for the CloudFront distribution
new route53.ARecord(this, 'SiteAliasRecord', {
recordName: siteDomain,
target: route53.AddressRecordTarget.fromAlias(new targets.CloudFrontTarget(distribution)),
zone
});
// Deploy site contents to S3 bucket
new s3deploy.BucketDeployment(this, 'DeployWithInvalidation', {
sources: [ s3deploy.Source.asset('./site-contents') ],
destinationBucket: siteBucket,
distribution,
distributionPaths: ['/*'],
});
有谁知道是否有使用 AWS CDK 将静态网站部署到 CloudFront 的最新示例或教程?
我也有兴趣使用 Lambda@Edge 进行一些路径重写,如 here 所述。
上次我看这个时,似乎没有办法为 S3 资产指定静态目标存储桶,所以我开始跟踪 this issue,这似乎已经解决。
我将不胜感激有关最佳实践的任何建议或文档。
这是在 Typescript 中使用 AWS CDK 的官方示例:https://github.com/aws-samples/aws-cdk-examples/blob/master/typescript/static-site/static-site.ts
相关部分在static-site.ts
:
const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: props.domainName });
const siteDomain = props.siteSubDomain + '.' + props.domainName;
new cdk.CfnOutput(this, 'Site', { value: 'https://' + siteDomain });
// Content bucket
const siteBucket = new s3.Bucket(this, 'SiteBucket', {
bucketName: siteDomain,
websiteIndexDocument: 'index.html',
websiteErrorDocument: 'error.html',
publicReadAccess: true,
// The default removal policy is RETAIN, which means that cdk destroy will not attempt to delete
// the new bucket, and it will remain in your account until manually deleted. By setting the policy to
// DESTROY, cdk destroy will attempt to delete the bucket, but will error if the bucket is not empty.
removalPolicy: cdk.RemovalPolicy.DESTROY, // NOT recommended for production code
});
new cdk.CfnOutput(this, 'Bucket', { value: siteBucket.bucketName });
// TLS certificate
const certificateArn = new acm.DnsValidatedCertificate(this, 'SiteCertificate', {
domainName: siteDomain,
hostedZone: zone
}).certificateArn;
new cdk.CfnOutput(this, 'Certificate', { value: certificateArn });
// CloudFront distribution that provides HTTPS
const distribution = new cloudfront.CloudFrontWebDistribution(this, 'SiteDistribution', {
aliasConfiguration: {
acmCertRef: certificateArn,
names: [ siteDomain ],
sslMethod: cloudfront.SSLMethod.SNI,
securityPolicy: cloudfront.SecurityPolicyProtocol.TLS_V1_1_2016,
},
originConfigs: [
{
s3OriginSource: {
s3BucketSource: siteBucket
},
behaviors : [ {isDefaultBehavior: true}],
}
]
});
new cdk.CfnOutput(this, 'DistributionId', { value: distribution.distributionId });
// Route53 alias record for the CloudFront distribution
new route53.ARecord(this, 'SiteAliasRecord', {
recordName: siteDomain,
target: route53.AddressRecordTarget.fromAlias(new targets.CloudFrontTarget(distribution)),
zone
});
// Deploy site contents to S3 bucket
new s3deploy.BucketDeployment(this, 'DeployWithInvalidation', {
sources: [ s3deploy.Source.asset('./site-contents') ],
destinationBucket: siteBucket,
distribution,
distributionPaths: ['/*'],
});