@studion/infra-code-blocks
Version:
Studion common infra components
133 lines (132 loc) • 4.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebServerLoadBalancer = void 0;
const pulumi = require("@pulumi/pulumi");
const aws = require("@pulumi/aws");
const common_tags_1 = require("../../shared/common-tags");
const merge_with_defaults_1 = require("../../shared/merge-with-defaults");
const webServerLoadBalancerNetworkConfig = {
ingress: [
{
protocol: 'tcp',
fromPort: 80,
toPort: 80,
cidrBlocks: ['0.0.0.0/0'],
},
{
protocol: 'tcp',
fromPort: 443,
toPort: 443,
cidrBlocks: ['0.0.0.0/0'],
},
],
egress: [
{
fromPort: 0,
toPort: 0,
protocol: '-1',
cidrBlocks: ['0.0.0.0/0'],
},
],
};
const defaults = {
healthCheckPath: '/healthcheck',
healthCheckConfig: {
healthyThreshold: 3,
unhealthyThreshold: 2,
interval: 60,
timeout: 5,
},
};
class WebServerLoadBalancer extends pulumi.ComponentResource {
name;
lb;
targetGroup;
httpListener;
tlsListener;
securityGroup;
constructor(name, args, opts = {}) {
super('studion:web-server:WebServerLoadBalancer', name, {}, opts);
this.name = name;
const vpc = pulumi.output(args.vpc);
const argsWithDefaults = (0, merge_with_defaults_1.mergeWithDefaults)(defaults, args);
const { port, certificate, healthCheckPath, healthCheckConfig, loadBalancingAlgorithmType, } = argsWithDefaults;
this.securityGroup = this.createLbSecurityGroup(vpc.vpcId);
this.lb = new aws.lb.LoadBalancer(this.name, {
namePrefix: 'lb-',
loadBalancerType: 'application',
subnets: vpc.publicSubnetIds,
securityGroups: [this.securityGroup.id],
internal: false,
ipAddressType: 'ipv4',
tags: { ...common_tags_1.commonTags, Name: name },
}, { parent: this });
this.targetGroup = this.createLbTargetGroup(port, vpc.vpcId, healthCheckPath, healthCheckConfig, loadBalancingAlgorithmType);
this.httpListener = this.createLbHttpListener(this.lb, this.targetGroup, !!certificate);
this.tlsListener =
certificate &&
this.createLbTlsListener(this.lb, this.targetGroup, pulumi.output(certificate));
this.registerOutputs();
}
createLbTlsListener(lb, lbTargetGroup, certificate) {
return new aws.lb.Listener(`${this.name}-listener-443`, {
loadBalancerArn: lb.arn,
port: 443,
protocol: 'HTTPS',
sslPolicy: 'ELBSecurityPolicy-TLS13-1-2-2021-06',
certificateArn: certificate.arn,
defaultActions: [
{
type: 'forward',
targetGroupArn: lbTargetGroup.arn,
},
],
tags: common_tags_1.commonTags,
}, { parent: this, dependsOn: [certificate] });
}
createLbHttpListener(lb, lbTargetGroup, redirectToHttps) {
const httpsRedirectAction = {
type: 'redirect',
redirect: {
port: '443',
protocol: 'HTTPS',
statusCode: 'HTTP_301',
},
};
const defaultAction = redirectToHttps
? httpsRedirectAction
: {
type: 'forward',
targetGroupArn: lbTargetGroup.arn,
};
return new aws.lb.Listener(`${this.name}-listener-80`, {
loadBalancerArn: lb.arn,
port: 80,
defaultActions: [defaultAction],
tags: common_tags_1.commonTags,
}, { parent: this });
}
createLbTargetGroup(port, vpcId, healthCheckPath, healthCheckConfig, loadBalancingAlgorithmType) {
return new aws.lb.TargetGroup(`${this.name}-tg`, {
namePrefix: 'lb-tg-',
port,
protocol: 'HTTP',
targetType: 'ip',
vpcId,
loadBalancingAlgorithmType,
healthCheck: {
...healthCheckConfig,
path: healthCheckPath,
},
tags: { ...common_tags_1.commonTags, Name: `${this.name}-target-group` },
}, { parent: this, dependsOn: [this.lb] });
}
createLbSecurityGroup(vpcId) {
return new aws.ec2.SecurityGroup(`${this.name}-security-group`, {
...webServerLoadBalancerNetworkConfig,
vpcId,
tags: common_tags_1.commonTags,
}, { parent: this });
}
}
exports.WebServerLoadBalancer = WebServerLoadBalancer;