|
| 1 | +#!/usr/bin/env node |
| 2 | +import * as s3 from "aws-cdk-lib/aws-s3"; |
| 3 | +import * as s3deploy from "aws-cdk-lib/aws-s3-deployment"; |
| 4 | +import { CfnOutput, RemovalPolicy, Stack } from "aws-cdk-lib"; |
| 5 | +import { Construct } from "constructs"; |
| 6 | +import path = require("path"); |
| 7 | + |
| 8 | +export interface StaticSiteBasicProps { |
| 9 | + staticContentPrefix: string; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Static site infrastructure, which deploys site content to an S3 bucket. |
| 14 | + * |
| 15 | + * The site redirects from HTTP to HTTPS, using a CloudFront distribution, |
| 16 | + * Route53 alias record, and ACM certificate. |
| 17 | + */ |
| 18 | +export class StaticSiteBasic extends Construct { |
| 19 | + constructor(parent: Stack, name: string, props: StaticSiteBasicProps) { |
| 20 | + super(parent, name); |
| 21 | + |
| 22 | + // new CfnOutput(this, "Site", { value: "https://" + siteDomain }); |
| 23 | + |
| 24 | + // Content bucket |
| 25 | + const indexDocument = "index.html"; |
| 26 | + |
| 27 | + const websiteBucket = new s3.Bucket(this, "WebsiteBucket", { |
| 28 | + websiteIndexDocument: indexDocument, |
| 29 | + publicReadAccess: true, |
| 30 | + blockPublicAccess: new s3.BlockPublicAccess({ |
| 31 | + blockPublicAcls: false, |
| 32 | + blockPublicPolicy: false, |
| 33 | + ignorePublicAcls: false, |
| 34 | + restrictPublicBuckets: false, |
| 35 | + }), |
| 36 | + /** |
| 37 | + * The default removal policy is RETAIN, which means that cdk destroy will not attempt to delete |
| 38 | + * the new bucket, and it will remain in your account until manually deleted. By setting the policy to |
| 39 | + * DESTROY, cdk destroy will attempt to delete the bucket, but will error if the bucket is not empty. |
| 40 | + */ |
| 41 | + removalPolicy: RemovalPolicy.DESTROY, // NOT recommended for production code |
| 42 | + |
| 43 | + /** |
| 44 | + * For sample purposes only, if you create an S3 bucket then populate it, stack destruction fails. This |
| 45 | + * setting will enable full cleanup of the demo. |
| 46 | + */ |
| 47 | + autoDeleteObjects: true, // NOT recommended for production code |
| 48 | + }); |
| 49 | + |
| 50 | + new CfnOutput(this, "Bucket", { value: websiteBucket.bucketName }); |
| 51 | + new CfnOutput(this, "StaticSiteUrl", { |
| 52 | + value: [ |
| 53 | + websiteBucket.bucketWebsiteUrl, |
| 54 | + props.staticContentPrefix, |
| 55 | + indexDocument, |
| 56 | + ].join("/"), |
| 57 | + }); |
| 58 | + |
| 59 | + // Deploy site contents to S3 bucket |
| 60 | + new s3deploy.BucketDeployment(this, "DeployWebsite", { |
| 61 | + //sources: [s3deploy.Source.asset(path.join(__dirname, './site-contents'))], |
| 62 | + sources: [s3deploy.Source.asset(path.join(__dirname, "./site-contents"))], |
| 63 | + destinationBucket: websiteBucket, |
| 64 | + destinationKeyPrefix: props.staticContentPrefix, // optional prefix in destination bucket |
| 65 | + }); |
| 66 | + } |
| 67 | +} |
0 commit comments