Jason
02/10/2022, 8:57 PMFrank
Frank
Frank
Jason
02/10/2022, 9:58 PMDavid Martin
02/10/2022, 10:33 PMimport * as sst from "@serverless-stack/resources";
import * as s3 from "aws-cdk-lib/aws-s3";
import { Certificate } from "aws-cdk-lib/aws-certificatemanager";
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
import * as route53 from "aws-cdk-lib/aws-route53";
import * as targets from "aws-cdk-lib/aws-route53-targets";
import * as origins from "aws-cdk-lib/aws-cloudfront-origins";
export default class BucketStack extends sst.Stack {
public bucket: any;
constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
super(scope, id, props);
this.bucket = new sst.Bucket(this, process.env.S3_PUBLIC_BUCKET_NAME!, {
s3Bucket: {
bucketName: process.env.S3_PUBLIC_BUCKET_NAME!,
publicReadAccess: false,
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
},
});
const certificate = Certificate.fromCertificateArn(
this,
"cert",
"arn:aws:acm:us-east-1:-XXXXX"
);
const distribution = new cloudfront.Distribution(
this,
``${process.env.CDN_DOMAIN!}-xxxxx-cdn`,`
{
defaultBehavior: {
origin: new origins.S3Origin(this.bucket.s3Bucket),
viewerProtocolPolicy:
cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
},
domainNames: [process.env.CDN_DOMAIN!],
certificate,
}
);
const zone = route53.HostedZone.fromHostedZoneAttributes(
this,
"shopcatsHostedZone",
{
hostedZoneId: "xxxx",
zoneName: "xxxx",
}
);
new route53.ARecord(this, "CDNARecord", {
zone,
target: route53.RecordTarget.fromAlias(
new targets.CloudFrontTarget(distribution)
),
});
new route53.AaaaRecord(this, "AliasRecord", {
zone,
target: route53.RecordTarget.fromAlias(
new targets.CloudFrontTarget(distribution)
),
});
new route53.ARecord(this, "SiteAliasRecord", {
recordName: process.env.CDN_DOMAIN!,
target: route53.RecordTarget.fromAlias(
new targets.CloudFrontTarget(distribution)
),
zone,
});
}
}
David Martin
02/10/2022, 10:33 PMresponseHeadersPolicy
David Martin
02/10/2022, 10:34 PMDavid Martin
02/10/2022, 10:34 PMDavid Martin
02/10/2022, 10:38 PM