Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export type WebServerServiceOptions = {
serviceName: string;
image: pulumi.Input<string>;
port: pulumi.Input<number>;
domain: pulumi.Input<string>;
hostedZoneId: pulumi.Input<string>;
domain?: pulumi.Input<string>;
hostedZoneId?: pulumi.Input<string>;
environment?:
| aws.ecs.KeyValuePair[]
| ((services: Services) => aws.ecs.KeyValuePair[]);
Expand Down Expand Up @@ -479,12 +479,12 @@ new WebServer(name: string, args: WebServerArgs, opts?: pulumi.ComponentResource
export type WebServerArgs = {
image: pulumi.Input<string>;
port: pulumi.Input<number>;
domain: pulumi.Input<string>;
hostedZoneId: pulumi.Input<string>;
cluster: aws.ecs.Cluster;
vpcId: pulumi.Input<string>;
vpcCidrBlock: pulumi.Input<string>;
publicSubnetIds: pulumi.Input<pulumi.Input<string>[]>;
domain?: pulumi.Input<string>;
hostedZoneId?: pulumi.Input<string>;
desiredCount?: pulumi.Input<number>;
autoscaling?: pulumi.Input<{
enabled: pulumi.Input<boolean>;
Expand Down
73 changes: 41 additions & 32 deletions src/components/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export type WebServerArgs = Pick<
* The domain which will be used to access the service.
* The domain or subdomain must belong to the provided hostedZone.
*/
domain: pulumi.Input<string>;
domain?: pulumi.Input<string>;
/**
* The ID of the hosted zone.
*/
hostedZoneId: pulumi.Input<string>;
hostedZoneId?: pulumi.Input<string>;
/**
* Path for the health check request. Defaults to "/healthcheck".
*/
Expand All @@ -43,13 +43,13 @@ const defaults = {
export class WebServer extends pulumi.ComponentResource {
name: string;
service: EcsService;
certificate: AcmCertificate;
lbSecurityGroup: aws.ec2.SecurityGroup;
serviceSecurityGroup: aws.ec2.SecurityGroup;
lb: aws.lb.LoadBalancer;
lbTargetGroup: aws.lb.TargetGroup;
lbHttpListener: aws.lb.Listener;
lbTlsListener: aws.lb.Listener;
certificate?: AcmCertificate;
lbTlsListener?: aws.lb.Listener;

constructor(
name: string,
Expand All @@ -60,8 +60,17 @@ export class WebServer extends pulumi.ComponentResource {

const { vpcId, domain, hostedZoneId } = args;

const hasCustomDomain = !!domain && !!hostedZoneId;
if (domain && !hostedZoneId) {
throw new Error(
'WebServer:hostedZoneId must be provided when the domain is specified',
);
}

this.name = name;
this.certificate = this.createTlsCertificate({ domain, hostedZoneId });
if (hasCustomDomain) {
this.certificate = this.createTlsCertificate({ domain, hostedZoneId });
}
const {
lb,
lbTargetGroup,
Expand All @@ -77,15 +86,17 @@ export class WebServer extends pulumi.ComponentResource {
this.serviceSecurityGroup = this.createSecurityGroup(vpcId);
this.service = this.createEcsService(args);

this.createDnsRecord({ domain, hostedZoneId });
if (hasCustomDomain) {
this.createDnsRecord({ domain, hostedZoneId });
}

this.registerOutputs();
}

private createTlsCertificate({
domain,
hostedZoneId,
}: Pick<WebServerArgs, 'domain' | 'hostedZoneId'>) {
}: Pick<Required<WebServerArgs>, 'domain' | 'hostedZoneId'>) {
const certificate = new AcmCertificate(
`${domain}-acm-certificate`,
{
Expand Down Expand Up @@ -191,24 +202,27 @@ export class WebServer extends pulumi.ComponentResource {
{ parent: this },
);

const lbTlsListener = new aws.lb.Listener(
`${this.name}-lb-listener-443`,
{
loadBalancerArn: lb.arn,
port: 443,
protocol: 'HTTPS',
sslPolicy: 'ELBSecurityPolicy-2016-08',
certificateArn: this.certificate.certificate.arn,
defaultActions: [
{
type: 'forward',
targetGroupArn: lbTargetGroup.arn,
},
],
tags: commonTags,
},
{ parent: this },
);
let lbTlsListener = undefined;
if (this.certificate) {
lbTlsListener = new aws.lb.Listener(
`${this.name}-lb-listener-443`,
{
loadBalancerArn: lb.arn,
port: 443,
protocol: 'HTTPS',
sslPolicy: 'ELBSecurityPolicy-2016-08',
certificateArn: this.certificate.certificate.arn,
defaultActions: [
{
type: 'forward',
targetGroupArn: lbTargetGroup.arn,
},
],
tags: commonTags,
},
{ parent: this },
);
}

return {
lb,
Expand Down Expand Up @@ -260,12 +274,7 @@ export class WebServer extends pulumi.ComponentResource {
},
{
parent: this,
dependsOn: [
this.lb,
this.lbTargetGroup,
this.lbHttpListener,
this.lbTlsListener,
],
dependsOn: [this.lb, this.lbTargetGroup],
},
);
return service;
Expand All @@ -274,7 +283,7 @@ export class WebServer extends pulumi.ComponentResource {
private createDnsRecord({
domain,
hostedZoneId,
}: Pick<WebServerArgs, 'domain' | 'hostedZoneId'>) {
}: Pick<Required<WebServerArgs>, 'domain' | 'hostedZoneId'>) {
const albAliasRecord = new aws.route53.Record(
`${this.name}-route53-record`,
{
Expand Down