UNPKG

@boostercloud/rocket-static-sites-aws-infrastructure

Version:

Booster rocket to deploy static sites to Booster applications in AWS.

60 lines (59 loc) 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StaticWebsiteStack = void 0; const core_1 = require("@aws-cdk/core"); const aws_s3_1 = require("@aws-cdk/aws-s3"); const aws_s3_deployment_1 = require("@aws-cdk/aws-s3-deployment"); const aws_cloudfront_1 = require("@aws-cdk/aws-cloudfront"); const fs_1 = require("fs"); class StaticWebsiteStack { static mountStack(params, stack) { var _a, _b, _c; const rootPath = (_a = params.rootPath) !== null && _a !== void 0 ? _a : './public'; const indexFile = (_b = params.indexFile) !== null && _b !== void 0 ? _b : 'index.html'; const errorFile = (_c = params.errorFile) !== null && _c !== void 0 ? _c : '404.html'; if (fs_1.existsSync(rootPath)) { const staticWebsiteOIA = new aws_cloudfront_1.OriginAccessIdentity(stack, 'staticWebsiteOIA', { comment: 'Allows static site to be reached only via CloudFront', }); const staticSiteBucket = new aws_s3_1.Bucket(stack, 'staticWebsiteBucket', { websiteIndexDocument: indexFile, websiteErrorDocument: errorFile, bucketName: params.bucketName, removalPolicy: core_1.RemovalPolicy.DESTROY, }); staticSiteBucket.grantRead(staticWebsiteOIA); const cloudFrontDistribution = new aws_cloudfront_1.CloudFrontWebDistribution(stack, 'staticWebsiteDistribution', { defaultRootObject: indexFile, viewerProtocolPolicy: aws_cloudfront_1.ViewerProtocolPolicy.REDIRECT_TO_HTTPS, viewerCertificate: aws_cloudfront_1.ViewerCertificate.fromCloudFrontDefaultCertificate(), originConfigs: [ { s3OriginSource: { s3BucketSource: staticSiteBucket, originAccessIdentity: staticWebsiteOIA, }, behaviors: [{ isDefaultBehavior: true }], }, ], }); new aws_s3_deployment_1.BucketDeployment(stack, 'staticWebsiteDeployment', { sources: [aws_s3_deployment_1.Source.asset(rootPath)], destinationBucket: staticSiteBucket, distribution: cloudFrontDistribution, }); new core_1.CfnOutput(stack, 'staticWebsiteURL', { value: `https://${cloudFrontDistribution.distributionDomainName}`, description: `The URL for the static website generated from ${rootPath} directory.`, }); } else { throw new Error(`The rocket '${require('package.json').name}' tried to deploy a static site from local folder ${rootPath}, but couldn't find it. Please review the configuration and parameters for this rocket.`); } } static async unmountStack(params, utils) { // The bucket must be empty for the stack deletion to succeed await utils.s3.emptyBucket(params.bucketName); } } exports.StaticWebsiteStack = StaticWebsiteStack;