Skip to content

Commit e39f37d

Browse files
authored
HostedZoneId and domain are optional for WebServer (#13)
1 parent 3a00a75 commit e39f37d

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ export type WebServerServiceOptions = {
147147
serviceName: string;
148148
image: pulumi.Input<string>;
149149
port: pulumi.Input<number>;
150-
domain: pulumi.Input<string>;
151-
hostedZoneId: pulumi.Input<string>;
150+
domain?: pulumi.Input<string>;
151+
hostedZoneId?: pulumi.Input<string>;
152152
environment?:
153153
| aws.ecs.KeyValuePair[]
154154
| ((services: Services) => aws.ecs.KeyValuePair[]);
@@ -479,12 +479,12 @@ new WebServer(name: string, args: WebServerArgs, opts?: pulumi.ComponentResource
479479
export type WebServerArgs = {
480480
image: pulumi.Input<string>;
481481
port: pulumi.Input<number>;
482-
domain: pulumi.Input<string>;
483-
hostedZoneId: pulumi.Input<string>;
484482
cluster: aws.ecs.Cluster;
485483
vpcId: pulumi.Input<string>;
486484
vpcCidrBlock: pulumi.Input<string>;
487485
publicSubnetIds: pulumi.Input<pulumi.Input<string>[]>;
486+
domain?: pulumi.Input<string>;
487+
hostedZoneId?: pulumi.Input<string>;
488488
desiredCount?: pulumi.Input<number>;
489489
autoscaling?: pulumi.Input<{
490490
enabled: pulumi.Input<boolean>;

src/components/web-server.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ export type WebServerArgs = Pick<
2525
* The domain which will be used to access the service.
2626
* The domain or subdomain must belong to the provided hostedZone.
2727
*/
28-
domain: pulumi.Input<string>;
28+
domain?: pulumi.Input<string>;
2929
/**
3030
* The ID of the hosted zone.
3131
*/
32-
hostedZoneId: pulumi.Input<string>;
32+
hostedZoneId?: pulumi.Input<string>;
3333
/**
3434
* Path for the health check request. Defaults to "/healthcheck".
3535
*/
@@ -43,13 +43,13 @@ const defaults = {
4343
export class WebServer extends pulumi.ComponentResource {
4444
name: string;
4545
service: EcsService;
46-
certificate: AcmCertificate;
4746
lbSecurityGroup: aws.ec2.SecurityGroup;
4847
serviceSecurityGroup: aws.ec2.SecurityGroup;
4948
lb: aws.lb.LoadBalancer;
5049
lbTargetGroup: aws.lb.TargetGroup;
5150
lbHttpListener: aws.lb.Listener;
52-
lbTlsListener: aws.lb.Listener;
51+
certificate?: AcmCertificate;
52+
lbTlsListener?: aws.lb.Listener;
5353

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

6161
const { vpcId, domain, hostedZoneId } = args;
6262

63+
const hasCustomDomain = !!domain && !!hostedZoneId;
64+
if (domain && !hostedZoneId) {
65+
throw new Error(
66+
'WebServer:hostedZoneId must be provided when the domain is specified',
67+
);
68+
}
69+
6370
this.name = name;
64-
this.certificate = this.createTlsCertificate({ domain, hostedZoneId });
71+
if (hasCustomDomain) {
72+
this.certificate = this.createTlsCertificate({ domain, hostedZoneId });
73+
}
6574
const {
6675
lb,
6776
lbTargetGroup,
@@ -77,15 +86,17 @@ export class WebServer extends pulumi.ComponentResource {
7786
this.serviceSecurityGroup = this.createSecurityGroup(vpcId);
7887
this.service = this.createEcsService(args);
7988

80-
this.createDnsRecord({ domain, hostedZoneId });
89+
if (hasCustomDomain) {
90+
this.createDnsRecord({ domain, hostedZoneId });
91+
}
8192

8293
this.registerOutputs();
8394
}
8495

8596
private createTlsCertificate({
8697
domain,
8798
hostedZoneId,
88-
}: Pick<WebServerArgs, 'domain' | 'hostedZoneId'>) {
99+
}: Pick<Required<WebServerArgs>, 'domain' | 'hostedZoneId'>) {
89100
const certificate = new AcmCertificate(
90101
`${domain}-acm-certificate`,
91102
{
@@ -191,24 +202,27 @@ export class WebServer extends pulumi.ComponentResource {
191202
{ parent: this },
192203
);
193204

194-
const lbTlsListener = new aws.lb.Listener(
195-
`${this.name}-lb-listener-443`,
196-
{
197-
loadBalancerArn: lb.arn,
198-
port: 443,
199-
protocol: 'HTTPS',
200-
sslPolicy: 'ELBSecurityPolicy-2016-08',
201-
certificateArn: this.certificate.certificate.arn,
202-
defaultActions: [
203-
{
204-
type: 'forward',
205-
targetGroupArn: lbTargetGroup.arn,
206-
},
207-
],
208-
tags: commonTags,
209-
},
210-
{ parent: this },
211-
);
205+
let lbTlsListener = undefined;
206+
if (this.certificate) {
207+
lbTlsListener = new aws.lb.Listener(
208+
`${this.name}-lb-listener-443`,
209+
{
210+
loadBalancerArn: lb.arn,
211+
port: 443,
212+
protocol: 'HTTPS',
213+
sslPolicy: 'ELBSecurityPolicy-2016-08',
214+
certificateArn: this.certificate.certificate.arn,
215+
defaultActions: [
216+
{
217+
type: 'forward',
218+
targetGroupArn: lbTargetGroup.arn,
219+
},
220+
],
221+
tags: commonTags,
222+
},
223+
{ parent: this },
224+
);
225+
}
212226

213227
return {
214228
lb,
@@ -260,12 +274,7 @@ export class WebServer extends pulumi.ComponentResource {
260274
},
261275
{
262276
parent: this,
263-
dependsOn: [
264-
this.lb,
265-
this.lbTargetGroup,
266-
this.lbHttpListener,
267-
this.lbTlsListener,
268-
],
277+
dependsOn: [this.lb, this.lbTargetGroup],
269278
},
270279
);
271280
return service;
@@ -274,7 +283,7 @@ export class WebServer extends pulumi.ComponentResource {
274283
private createDnsRecord({
275284
domain,
276285
hostedZoneId,
277-
}: Pick<WebServerArgs, 'domain' | 'hostedZoneId'>) {
286+
}: Pick<Required<WebServerArgs>, 'domain' | 'hostedZoneId'>) {
278287
const albAliasRecord = new aws.route53.Record(
279288
`${this.name}-route53-record`,
280289
{

0 commit comments

Comments
 (0)