@studion/infra-code-blocks
Version:
Studion common infra components
102 lines (101 loc) • 3.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LbCacheStrategy = void 0;
const aws = require("@pulumi/aws");
const pulumi = require("@pulumi/pulumi");
class LbCacheStrategy extends pulumi.ComponentResource {
name;
pathPattern;
config;
cachePolicy;
responseHeadersPolicy;
constructor(name, args, opts = {}) {
super('studion:cloudfront:LbCacheStrategy', name, {}, opts);
this.name = name;
const { pathPattern, loadBalancer } = args;
this.pathPattern = pathPattern;
this.cachePolicy = this.createCachePolicy();
this.responseHeadersPolicy = this.createResponseHeadersPolicy();
this.config = {
targetOriginId: pulumi.output(loadBalancer).apply(lb => lb.arn),
viewerProtocolPolicy: 'redirect-to-https',
allowedMethods: [
'GET',
'HEAD',
'OPTIONS',
'PUT',
'POST',
'PATCH',
'DELETE',
],
cachedMethods: ['GET', 'HEAD', 'OPTIONS'],
compress: true,
cachePolicyId: this.cachePolicy.id,
originRequestPolicyId: aws.cloudfront
.getOriginRequestPolicyOutput({ name: 'Managed-AllViewer' })
.apply(policy => policy.id),
responseHeadersPolicyId: this.responseHeadersPolicy.id,
};
this.registerOutputs();
}
createCachePolicy() {
return new aws.cloudfront.CachePolicy(`${this.name}-cache-policy`, {
defaultTtl: 0,
minTtl: 0,
maxTtl: 3600, // 1 hour
parametersInCacheKeyAndForwardedToOrigin: {
cookiesConfig: {
cookieBehavior: 'none',
},
headersConfig: {
headerBehavior: 'none',
},
queryStringsConfig: {
queryStringBehavior: 'all',
},
enableAcceptEncodingGzip: true,
enableAcceptEncodingBrotli: true,
},
}, { parent: this });
}
createResponseHeadersPolicy() {
return new aws.cloudfront.ResponseHeadersPolicy(`${this.name}-res-headers-policy`, {
customHeadersConfig: {
items: [
{
header: 'Cache-Control',
value: 'no-store',
override: false,
},
],
},
securityHeadersConfig: {
contentTypeOptions: {
override: true,
},
frameOptions: {
frameOption: 'SAMEORIGIN',
override: false,
},
referrerPolicy: {
referrerPolicy: 'strict-origin-when-cross-origin',
override: false,
},
// instruct browsers to only use HTTPS
strictTransportSecurity: {
accessControlMaxAgeSec: 31536000, // 1 year
includeSubdomains: true,
preload: true,
override: true,
},
},
}, { parent: this });
}
getPathConfig() {
return {
pathPattern: this.pathPattern,
...this.config,
};
}
}
exports.LbCacheStrategy = LbCacheStrategy;