Skip to content

Commit afd0811

Browse files
authored
feat(Implement aws-route53-alb): Implement new construct (#421)
* Implement new construct * Adjust viperlight * Fix first few cfn_nag issues * Fix more cfn_nag issues * cfn_nag changes * Fix more cfn_nag issues * Fix more cfn_nag issues * Viperlight adjustments * Fix more cfn_nag issues * Viperlight adjustments * Lint trivia * Address review comments * Address review comments * viperlight edits
1 parent ffd8d17 commit afd0811

28 files changed

+5412
-1
lines changed

.viperlightignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,8 @@ source/patterns/@aws-solutions-constructs/aws-lambda-eventbridge/test/integ.depl
140140
source/patterns/@aws-solutions-constructs/aws-lambda-eventbridge/test/integ.existingEventBus.expected.json:108
141141
source/patterns/@aws-solutions-constructs/aws-lambda-eventbridge/test/integ.existingFunction.expected.json:122
142142
source/patterns/@aws-solutions-constructs/aws-lambda-eventbridge/test/aws-lambda-eventbridge.test.ts:28
143-
source/patterns/@aws-solutions-constructs/aws-lambda-eventbridge/test/aws-lambda-eventbridge.test.ts:339
143+
source/patterns/@aws-solutions-constructs/aws-lambda-eventbridge/test/aws-lambda-eventbridge.test.ts:339
144+
# These are references to the us-east-1 ELBV2 account (publicly known)
145+
source/patterns/@aws-solutions-constructs/aws-route53-alb/test/integ.deployPrivateApi.expected.json:193
146+
source/patterns/@aws-solutions-constructs/aws-route53-alb/test/integ.deployPrivateApiExistingZone.expected.json:844
147+
source/patterns/@aws-solutions-constructs/aws-route53-alb/test/integ.deployPublicApiNewAlb.expected.json:188

package-lock.json

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lib/*.js
2+
test/*.js
3+
*.d.ts
4+
coverage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
lib/*.js
2+
test/*.js
3+
*.js.map
4+
*.d.ts
5+
node_modules
6+
*.generated.ts
7+
dist
8+
.jsii
9+
10+
.LAST_BUILD
11+
.nyc_output
12+
coverage
13+
.nycrc
14+
.LAST_PACKAGE
15+
*.snk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Exclude typescript source and config
2+
*.ts
3+
tsconfig.json
4+
coverage
5+
.nyc_output
6+
*.tgz
7+
*.snk
8+
*.tsbuildinfo
9+
10+
# Include javascript files and typescript declarations
11+
!*.js
12+
!*.d.ts
13+
14+
# Exclude jsii outdir
15+
dist
16+
17+
# Include .jsii
18+
!.jsii
19+
20+
# Include .jsii
21+
!.jsii
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# aws-route53-alb module
2+
<!--BEGIN STABILITY BANNER-->
3+
4+
---
5+
6+
![Stability: Experimental](https://img.shields.io/badge/stability-Experimental-important.svg?style=for-the-badge)
7+
8+
> All classes are under active development and subject to non-backward compatible changes or removal in any
9+
> future version. These are not subject to the [Semantic Versioning](https://semver.org/) model.
10+
> This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
11+
12+
---
13+
<!--END STABILITY BANNER-->
14+
15+
| **Reference Documentation**:| <span style="font-weight: normal">https://docs.aws.amazon.com/solutions/latest/constructs/</span>|
16+
|:-------------|:-------------|
17+
<div style="height:8px"></div>
18+
19+
| **Language** | **Package** |
20+
|:-------------|-----------------|
21+
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`aws_solutions_constructs.aws_route53_alb`|
22+
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@aws-solutions-constructs/aws-route53-alb`|
23+
|![Java Logo](https://docs.aws.amazon.com/cdk/api/latest/img/java32.png) Java|`software.amazon.awsconstructs.services.route53alb`|
24+
25+
This AWS Solutions Construct implements an Amazon Route53 Hosted Zone routing to an Application Load Balancer
26+
27+
Here is a minimal deployable pattern definition in Typescript:
28+
29+
``` typescript
30+
import { Route53ToAlb } from '@aws-solutions-constructs/aws-route53-alb';
31+
32+
new Route53ToAlb(this, 'Route53ToAlbPattern', {
33+
privateHostedZoneProps: {
34+
zoneName: 'www.example.com',
35+
}
36+
publicApi: false,
37+
});
38+
39+
```
40+
41+
## Initializer
42+
43+
``` text
44+
new Route53ToAlb(scope: Construct, id: string, props: Route53ToAlbProps);
45+
```
46+
47+
_Parameters_
48+
49+
* scope [`Construct`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html)
50+
* id `string`
51+
* props [`Route53ToAlbProps`](#pattern-construct-props)
52+
53+
## Pattern Construct Props
54+
55+
This construct cannot create a new Public Hosted Zone, if you are creating a public API you must supply an existing Public Hosted Zone that will be reconfigured with a new Alias record. Public Hosted Zones are configured with public domain names and are not well suited to be launched and torn down dynamically, so this construct will only reconfigure existing Public Hosted Zones.
56+
57+
This construct can create Private Hosted Zones. If you want a Private Hosted Zone, then you can either provide an existing Private Hosted Zone or a privateHostedZoneProps value with at least the Domain Name defined.
58+
59+
| **Name** | **Type** | **Description** |
60+
|:-------------|:----------------|-----------------|
61+
| privateHostedZoneProps? | [route53.PrivateHostedZoneProps](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.PrivateHostedZoneProps.html) | Optional custom properties for a new Private Hosted Zone. Cannot be specified for a public API. Cannot specify a VPC, it will use the VPC in existingVpc or the VPC created by the construct. Providing both this and existingHostedZoneInterfaceis an error. |
62+
| existingHostedZoneInterface? | [route53.IHostedZone](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.IHostedZone.html) | Existing Public or Private Hosted Zone (type must match publicApi setting). Specifying both this and privateHostedZoneProps is an error. If this is a Private Hosted Zone, the associated VPC must be provided as the existingVpc property |
63+
| loadBalancerProps? | [elasticloadbalancingv2.ApplicationLoadBalancerProps](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticloadbalancingv2.ApplicationLoadBalancerProps.html) | Optional custom properties for a new loadBalancer. Providing both this and existingLoadBalancer is an error. This cannot specify a VPC, it will use the VPC in existingVpc or the VPC created by the construct. |
64+
| existingLoadBalancerObj? | [elasticloadbalancingv2.ApplicationLoadBalancer](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticloadbalancingv2.ApplicationLoadBalancer.html) | Existing Application Load Balancer to incorporate into the construct architecture. Providing both this and loadBalancerProps is an error. The VPC containing this loadBalancer must match the VPC provided in existingVpc. |
65+
| vpcProps? | [ec2.VpcProps](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.VpcProps.html) | Optional custom properties for a VPC the construct will create. This VPC will be used by the new ALB and any Private Hosted Zone the construct creates (that's why loadBalancerProps and privateHostedZoneProps can't include a VPC). Providing both this and existingVpc is an error. |
66+
| existingVpc? | [ec2.IVpc](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.IVpc.html) | An existing VPC in which to deploy the construct. Providing both this and vpcProps is an error. If the client provides an existing load balancer and/or existing Private Hosted Zone, those constructs must exist in this VPC. |
67+
| logAccessLogs? | boolean| Whether to turn on Access Logs for the Application Load Balancer. Uses an S3 bucket with associated storage costs.Enabling Access Logging is a best practice. default - true |
68+
| loggingBucketProps? | [s3.BucketProps](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.BucketProps.html) | Optional properties to customize the bucket used to store the ALB Access Logs. Supplying this and setting logAccessLogs to false is an error. @default - none |
69+
70+
| publicApi | boolean | Whether the construct is deploying a private or public API. This has implications for the Hosted Zone, VPC and ALB. |
71+
72+
73+
## Pattern Properties
74+
75+
| **Name** | **Type** | **Description** |
76+
|:-------------|:----------------|-----------------|
77+
| hostedZone | [route53.IHostedZone](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.IHostedZone.html) | The hosted zone used by the construct (whether created by the construct or providedb by the client) |
78+
| vpc | [ec2.IVpc](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.IVpc.html) | The VPC used by the construct (whether created by the construct or providedb by the client) |
79+
| loadBalancer | [elasticloadbalancingv2.ApplicationLoadBalancer](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticloadbalancingv2.ApplicationLoadBalancer.html) | The Load Balancer used by the construct (whether created by the construct or providedb by the client) |
80+
81+
## Default settings
82+
83+
Out of the box implementation of the Construct without any override will set the following defaults:
84+
85+
### Amazon Route53
86+
* Adds an ALIAS record to the new or provided Hosted Zone that routes to the construct's ALB
87+
88+
### Application Load Balancer
89+
* Creates an Application Load Balancer with no Listener or target. The consruct can incorporate an existing, fully configured ALB if provided.
90+
91+
## Architecture
92+
![Architecture Diagram](architecture.png)
93+
94+
***
95+
&copy; Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/**
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
14+
// Imports
15+
import * as defaults from "@aws-solutions-constructs/core";
16+
import * as elb from "@aws-cdk/aws-elasticloadbalancingv2";
17+
import * as s3 from "@aws-cdk/aws-s3";
18+
import * as r53 from "@aws-cdk/aws-route53";
19+
import * as r53t from '@aws-cdk/aws-route53-targets';
20+
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate
21+
import { Construct } from '@aws-cdk/core';
22+
import * as ec2 from '@aws-cdk/aws-ec2';
23+
24+
export interface Route53ToAlbProps {
25+
/**
26+
* Custom properties for a new Private Hosted Zone. Cannot be specified for a
27+
* public API. Cannot specify a VPC
28+
*
29+
* @default - None
30+
*/
31+
readonly privateHostedZoneProps?: r53.PrivateHostedZoneProps | any,
32+
/**
33+
* Existing Public or Private Hosted Zone. If a Private Hosted Zone, must
34+
* exist in the same VPC specified in existingVpc
35+
*
36+
* @default - None
37+
*/
38+
readonly existingHostedZoneInterface?: r53.IHostedZone,
39+
/**
40+
* Custom properties for a new ALB. Providing both this and existingLoadBalancerObj
41+
* is an error. These properties cannot include a VPC.
42+
*
43+
* @default - None
44+
*/
45+
readonly loadBalancerProps?: elb.ApplicationLoadBalancerProps | any,
46+
/**
47+
* An existing Application Load Balancer. Providing both this and loadBalancerProps
48+
* is an error. This ALB must exist in the same VPC specified in existingVPC
49+
*
50+
* @default - None
51+
*/
52+
readonly existingLoadBalancerObj?: elb.ApplicationLoadBalancer,
53+
/**
54+
* Whether to turn on Access Logs for the Application Load Balancer. Uses an S3 bucket
55+
* with associated storage costs. Enabling Access Logging is a best practice.
56+
*
57+
* @default - true
58+
*/
59+
readonly logAccessLogs?: boolean,
60+
/**
61+
* Optional properties to customize the bucket used to store the ALB Access
62+
* Logs. Supplying this and setting logAccessLogs to false is an error.
63+
*
64+
* @default - none
65+
*/
66+
readonly loggingBucketProps?: s3.BucketProps,
67+
/**
68+
* Custom properties for a new VPC. Providing both this and existingVpc is
69+
* an error. If an existingAlb or existing Private Hosted Zone is provided, those
70+
* already exist in a VPC so this value cannot be provided.
71+
*
72+
* @default - None
73+
*/
74+
readonly vpcProps?: ec2.VpcProps,
75+
/**
76+
* An existing VPC. Providing both this and vpcProps is an error. If an existingAlb or existing
77+
* Private Hosted Zone is provided, this value must be the VPC associated with those resources.
78+
*
79+
* @default - None
80+
*/
81+
readonly existingVpc?: ec2.IVpc,
82+
/**
83+
* Whether to create a public or private API. This value has implications
84+
* for the VPC, the type of Hosted Zone and the Application Load Balancer
85+
*
86+
* @default - None
87+
*/
88+
readonly publicApi: boolean
89+
}
90+
91+
/**
92+
* @summary Configures a Route53 Hosted Zone to route to an Application Load Balancer
93+
*/
94+
export class Route53ToAlb extends Construct {
95+
public readonly hostedZone: r53.IHostedZone;
96+
public readonly vpc: ec2.IVpc;
97+
public readonly loadBalancer: elb.ApplicationLoadBalancer;
98+
99+
/**
100+
* @summary Constructs a new instance of the Route53ToAlb class.
101+
* @param {cdk.App} scope - represents the scope for all the resources.
102+
* @param {string} id - this is a a scope-unique id.
103+
* @param {Route53ToAlbProps} props - user provided props for the construct.
104+
* @access public
105+
*/
106+
constructor(scope: Construct, id: string, props: Route53ToAlbProps) {
107+
super(scope, id);
108+
defaults.CheckProps(props);
109+
110+
if ((props?.logAccessLogs === false) && (props.loggingBucketProps)) {
111+
throw new Error('If logAccessLogs is false, supplying loggingBucketProps is invalid.');
112+
}
113+
114+
if (props?.loadBalancerProps?.vpc) {
115+
throw new Error('Specify any existing VPC at the construct level, not within loadBalancerProps.');
116+
}
117+
118+
if (props.existingLoadBalancerObj && !props.existingVpc) {
119+
throw new Error('An existing ALB already exists in a VPC, so that VPC must be passed to the construct in props.existingVpc');
120+
}
121+
122+
if (props.existingHostedZoneInterface && !props.publicApi && !props.existingVpc) {
123+
throw new Error('An existing Private Hosted Zone already exists in a VPC, so that VPC must be passed to the construct in props.existingVpc');
124+
}
125+
126+
if (props.existingVpc) {
127+
this.vpc = props.existingVpc;
128+
} else {
129+
this.vpc = defaults.buildVpc(scope, {
130+
defaultVpcProps: props.publicApi ?
131+
defaults.DefaultPublicPrivateVpcProps() :
132+
// If this is an internal app, we're going to turn on DNS
133+
// by default to allow gateway and interface service endpoints
134+
defaults.overrideProps(defaults.DefaultIsolatedVpcProps(), { enableDnsHostnames: true, enableDnsSupport: true, }),
135+
userVpcProps: props.vpcProps,
136+
});
137+
}
138+
139+
if (props.existingHostedZoneInterface) {
140+
this.hostedZone = props.existingHostedZoneInterface;
141+
} else {
142+
if (props.publicApi) {
143+
throw new Error('Public APIs require an existingHostedZone be passed in the Props object.');
144+
} else {
145+
if (!props.privateHostedZoneProps) {
146+
throw new Error('Must supply privateHostedZoneProps to create a private API');
147+
}
148+
if (props.privateHostedZoneProps.vpc) {
149+
throw new Error('All VPC specs must be provided at the Construct level in Route53ToAlbProps');
150+
}
151+
const manufacturedProps: r53.PrivateHostedZoneProps = defaults.overrideProps(props.privateHostedZoneProps, { vpc: this.vpc });
152+
this.hostedZone = new r53.PrivateHostedZone(this, `${id}-zone`, manufacturedProps);
153+
}
154+
}
155+
156+
this.loadBalancer = defaults.ObtainAlb(
157+
this,
158+
id,
159+
this.vpc,
160+
props.publicApi,
161+
props.existingLoadBalancerObj,
162+
props.loadBalancerProps,
163+
props.logAccessLogs,
164+
props.loggingBucketProps
165+
);
166+
167+
// Add the ALB to the HostedZone as a target
168+
const hostedZoneTarget = new r53t.LoadBalancerTarget(this.loadBalancer);
169+
170+
new r53.ARecord(this, `${id}-alias`, {
171+
target: { aliasTarget: hostedZoneTarget },
172+
zone: this.hostedZone
173+
});
174+
}
175+
}

0 commit comments

Comments
 (0)