UNPKG

limgen

Version:

Infrastructure as Code generator

166 lines (165 loc) 6.67 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CdnCloudFront = void 0; const pulumi = __importStar(require("@pulumi/pulumi")); const aws = __importStar(require("@pulumi/aws")); const prefixed_1 = require("../utils/prefixed"); const deep_merge_1 = require("../utils/deep-merge"); class CdnCloudFront extends pulumi.ComponentResource { constructor(name = 'CDN', args, opts) { super("limgen:CloudFrontComponent", name, {}, opts); this.originAccessIdentity = null; this.originBucketPolicy = null; this._args = args; if (this._args.storage) { this.originAccessIdentity = this.getOriginAccessIdentity(); this.originBucketPolicy = this.getOriginBucketPolicy(); } this.distribution = this.getDistribution(); this.registerOutputs({}); } getDistribution() { let orderedCacheBehaviors = []; const origins = []; if (!this._args.lb && !this._args.storage) { throw new Error("At least one of lb or storage is required to create a CloudFront distribution"); } let defaultCacheBehavior; if (this._args.lb) { origins.push({ domainName: this._args.lb.loadBalancer.dnsName, originId: (0, prefixed_1.prefixed)("lb-origin"), customOriginConfig: { originProtocolPolicy: "http-only", httpPort: 80, httpsPort: 443, originSslProtocols: ["TLSv1.2"], }, }); defaultCacheBehavior = { targetOriginId: (0, prefixed_1.prefixed)("lb-origin"), viewerProtocolPolicy: "redirect-to-https", allowedMethods: ["GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH", "DELETE"], cachedMethods: ["GET", "HEAD"], compress: true, forwardedValues: { queryString: true, headers: ["*"], cookies: { forward: "all" } }, }; } if (this._args.storage) { origins.push({ domainName: this._args.storage.bucketRegionalDomainName, originId: (0, prefixed_1.prefixed)("storage-origin"), s3OriginConfig: { originAccessIdentity: this.originAccessIdentity.cloudfrontAccessIdentityPath, }, }); orderedCacheBehaviors.push({ pathPattern: this._args.storagePathPattern || "/media/*", targetOriginId: (0, prefixed_1.prefixed)("storage-origin"), allowedMethods: ["GET", "HEAD", "OPTIONS"], cachedMethods: ["GET", "HEAD"], compress: true, viewerProtocolPolicy: "redirect-to-https", forwardedValues: { headers: [], queryStringCacheKeys: [], queryString: false, cookies: { forward: "none" }, }, }); if (!defaultCacheBehavior) { defaultCacheBehavior = { targetOriginId: (0, prefixed_1.prefixed)("storage-origin"), viewerProtocolPolicy: "redirect-to-https", allowedMethods: ["GET", "HEAD", "OPTIONS"], cachedMethods: ["GET", "HEAD"], forwardedValues: { headers: [], queryStringCacheKeys: [], queryString: false, cookies: { forward: "none" }, }, }; } } return new aws.cloudfront.Distribution("CloudFrontDistribution", (0, deep_merge_1.deepMerge)({ enabled: true, origins, orderedCacheBehaviors, defaultCacheBehavior, priceClass: "PriceClass_100", defaultRootObject: "index.html", viewerCertificate: { cloudfrontDefaultCertificate: true, }, restrictions: { geoRestriction: { restrictionType: "none", }, }, }, this._args.cloudFrontDistributionArgs)); } getOriginBucketPolicy() { if (!this._args.storage) { throw new Error("Media bucket is required to create an origin bucket policy"); } return new aws.s3.BucketPolicy("OriginBucketPolicy", { bucket: this._args.storage.id, policy: pulumi.all([ this._args.storage.arn, this.originAccessIdentity.iamArn ]).apply(([storageArn, iamArn]) => JSON.stringify({ Version: "2012-10-17", Statement: [ { Effect: "Allow", Principal: { AWS: iamArn, }, Action: ["s3:GetObject"], Resource: `${storageArn}/*`, }, ], })), }); } getOriginAccessIdentity() { return new aws.cloudfront.OriginAccessIdentity("OriginAccessIdentity", { comment: "OAI for CloudFront distribution to access S3 bucket", }); } } exports.CdnCloudFront = CdnCloudFront;