Skip to content

Commit 81129dd

Browse files
authored
feat(aws-route53-apigateway): New Construct (#511)
* created README for r53-apigw construct * updated code in README * updated code in README * updated code in README * updated README by editing architecture and removing vpcProps * created r53-apigw construct * fixed esline error * updated unit tests * added empty integ test * added one resource to integ test * fixed eslint * removed certificate creation and validation * updated license header to 2022 * changed error message and integ test
1 parent d744e12 commit 81129dd

File tree

10 files changed

+1047
-0
lines changed

10 files changed

+1047
-0
lines changed
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,112 @@
1+
# aws-route53-apigateway 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_apigateway`|
22+
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@aws-solutions-constructs/aws-route53-apigateway`|
23+
|![Java Logo](https://docs.aws.amazon.com/cdk/api/latest/img/java32.png) Java|`software.amazon.awsconstructs.services.route53apigateway`|
24+
25+
## Overview
26+
27+
This AWS Solutions Construct implements an Amazon Route 53 connected to a configured Amazon API Gateway REST API.
28+
29+
Here is a minimal deployable pattern definition in Typescript:
30+
31+
``` typescript
32+
import * as api from '@aws-cdk/aws-apigateway';
33+
import * as lambda from "@aws-cdk/aws-lambda";
34+
import * as route53 from "@aws-cdk/aws-route53";
35+
import { Route53ToApigateway } from '@aws-solutions-constructs/aws-route53-apigateway';
36+
37+
// The construct requires an existing REST API, this can be created in raw CDK or extracted
38+
// from a previously instantiated construct that created an API Gateway REST API
39+
const existingRestApi = previouslyCreatedApigatewayToLambdaConstruct.apiGateway;
40+
41+
const ourHostedZone = route53.HostedZone.fromLookup(this, 'HostedZone', {
42+
domainName: "example.com",
43+
});
44+
45+
const certificate = acm.Certificate.fromCertificateArn(
46+
stack,
47+
"fake-cert",
48+
"arn:aws:acm:us-east-1:123456789012:certificate/11112222-3333-1234-1234-123456789012"
49+
);
50+
51+
// This construct can only be attached to a configured API Gateway.
52+
new Route53ToApigateway(this, 'Route53ToApigatewayPattern', {
53+
existingApiGatewayObj: existingRestApi,
54+
existingHostedZoneInterface: ourHostedZone,
55+
publicApi: true,
56+
existingCertificateInterface: certificate
57+
});
58+
59+
```
60+
61+
## Initializer
62+
63+
``` text
64+
new Route53ToApigateway(scope: Construct, id: string, props: Route53ToApigatewayProps);
65+
```
66+
67+
_Parameters_
68+
69+
* scope [`Construct`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html)
70+
* id `string`
71+
* props [`Route53ToApigatewayProps`](#pattern-construct-props)
72+
73+
## Pattern Construct Props
74+
75+
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.
76+
77+
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. If you are using privateHostedZoneProps, an existing wildcard certificate (*.example.com) must be issued from a previous domain to be used in the newly created Private Hosted Zone. New certificate creation and validation do not take place in this construct. A private Rest API already exists in a VPC, so that VPC must be provided in the existingVpc prop. There is no scenario where this construct can create a new VPC (since it can't create a new API), so the vpcProps property is not supported on this construct.
78+
79+
| **Name** | **Type** | **Description** |
80+
|:-------------|:----------------|-----------------|
81+
| publicApi | boolean | Whether the construct is deploying a private or public API. This has implications for the Hosted Zone and VPC. |
82+
| 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 existingHostedZoneInterface is an error. |
83+
| 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.|
84+
| 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.|
85+
|existingApiGatewayInterface|[api.IRestApi](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.IRestApi.html)|The existing API Gateway instance that will be connected to the Route 53 hosted zone. *Note that Route 53 can only be connected to a configured API Gateway, so this construct only accepts an existing IRestApi and does not accept apiGatewayProps.*|
86+
| existingCertificateInterface |[certificatemanager.ICertificate](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-certificatemanager.ICertificate.html)| An existing AWS Certificate Manager certificate for your custom domain name.|
87+
88+
## Pattern Properties
89+
90+
| **Name** | **Type** | **Description** |
91+
|:-------------|:----------------|-----------------|
92+
|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 provided by the client) |
93+
| vpc? | [ec2.IVpc](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.IVpc.html) | The VPC used by the construct. |
94+
|apiGateway|[api.RestApi](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.RestApi.html)|Returns an instance of the API Gateway REST API created by the pattern.|
95+
|certificate|[certificatemanager.ICertificate](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-certificatemanager.ICertificate.html)| THe certificate used by the construct (whether create by the construct or provided by the client)
96+
97+
## Default settings
98+
Out of the box implementation of the Construct without any override will set the following defaults:
99+
100+
### Amazon Route53
101+
* Adds an ALIAS record to the new or provided Hosted Zone that routes to the construct's API Gateway
102+
103+
### Amazon API Gateway
104+
* User provided API Gateway object is used as-is
105+
* Sets up custom domain name mapping to API
106+
107+
## Architecture
108+
109+
![Architecture Diagram](architecture.png)
110+
111+
***
112+
&copy; Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* Copyright 2022 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 api from '@aws-cdk/aws-apigateway';
16+
import * as route53 from "@aws-cdk/aws-route53";
17+
import * as targets from '@aws-cdk/aws-route53-targets';
18+
import * as ec2 from '@aws-cdk/aws-ec2';
19+
import * as defaults from '@aws-solutions-constructs/core';
20+
import * as certificatemanager from '@aws-cdk/aws-certificatemanager';
21+
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate
22+
import { Construct } from '@aws-cdk/core';
23+
24+
/**
25+
* The properties for the Route53ToApiGateway class.
26+
*/
27+
export interface Route53ToApiGatewayProps {
28+
/**
29+
* Whether to create a public or private API. This value has implications
30+
* for the VPC, the type of Hosted Zone and the Application Load Balancer
31+
*
32+
* @default - None
33+
*/
34+
readonly publicApi: boolean
35+
/**
36+
* Optional custom properties for a new Private Hosted Zone. Cannot be specified for a
37+
* public API. Cannot specify a VPC, it will use the VPC in existingVpc or the VPC created by the construct.
38+
* Providing both this and existingHostedZoneInterface is an error.
39+
*
40+
* @default - None
41+
*/
42+
readonly privateHostedZoneProps?: route53.PrivateHostedZoneProps | any,
43+
/**
44+
* Existing Public or Private Hosted Zone. If a Private Hosted Zone, must
45+
* exist in the same VPC specified in existingVpc
46+
*
47+
* @default - None
48+
*/
49+
readonly existingHostedZoneInterface?: route53.IHostedZone,
50+
/**
51+
* An existing VPC. If an existing Private Hosted Zone is provided,
52+
* this value must be the VPC associated with those resources.
53+
*
54+
* @default - None
55+
*/
56+
readonly existingVpc?: ec2.IVpc,
57+
/**
58+
* The existing API Gateway instance that will be protected with the Route 53 hosted zone.
59+
*
60+
* @default - None
61+
*/
62+
readonly existingApiGatewayInterface: api.IRestApi,
63+
/**
64+
* An existing AWS Certificate Manager certificate for your custom domain name.
65+
*
66+
* @defualt - None
67+
*/
68+
readonly existingCertificateInterface: certificatemanager.ICertificate;
69+
}
70+
71+
/**
72+
* @summary The Route53ToApiGateway class.
73+
*/
74+
export class Route53ToApiGateway extends Construct {
75+
public readonly hostedZone: route53.IHostedZone;
76+
public readonly vpc?: ec2.IVpc;
77+
public readonly apiGateway: api.RestApi;
78+
public readonly certificate: certificatemanager.ICertificate;
79+
/**
80+
* @summary Constructs a new instance of the Route53ToApiGateway class.
81+
* @param {cdk.App} scope - represents the scope for all the resources.
82+
* @param {string} id - this is a a scope-unique id.
83+
* @param {Route53ToApiGatewayProps} props - user provided props for the construct
84+
* @since 0.8.0
85+
* @access public
86+
*/
87+
constructor(scope: Construct, id: string, props: Route53ToApiGatewayProps) {
88+
super(scope, id);
89+
defaults.CheckProps(props);
90+
91+
this.certificate = props.existingCertificateInterface;
92+
93+
if (props.existingVpc) {
94+
this.vpc = props.existingVpc;
95+
}
96+
97+
// Existing Public or Private Hosted Zone
98+
if (props.existingHostedZoneInterface) {
99+
this.hostedZone = props.existingHostedZoneInterface;
100+
101+
if (props.existingVpc) {
102+
throw new Error('Cannot provide an existing VPC to an existing Private Hosted Zone.');
103+
}
104+
if (props.privateHostedZoneProps) {
105+
throw new Error('Must provide either existingHostedZoneInterface or privateHostedZoneProps, but not both.');
106+
}
107+
} else { // Creating a Private Hosted Zone
108+
if (props.publicApi) {
109+
throw new Error('Public APIs require an existingHostedZone be passed in the Props object.');
110+
} else {
111+
if (!props.privateHostedZoneProps) {
112+
throw new Error('Must provide either existingHostedZoneInterface or privateHostedZoneProps.');
113+
}
114+
if (props.privateHostedZoneProps.vpc) {
115+
throw new Error('All VPC specs must be provided at the Construct level in Route53ToApiGatewayProps.');
116+
}
117+
if (!props.privateHostedZoneProps.zoneName) {
118+
throw new Error('Must supply zoneName for Private Hosted Zone Props.');
119+
}
120+
if ( !this.vpc ) {
121+
throw new Error('Must specify an existingVPC for the Private Hosted Zone in the construct props.');
122+
}
123+
const manufacturedProps: route53.PrivateHostedZoneProps = defaults.overrideProps(props.privateHostedZoneProps, { vpc: this.vpc });
124+
this.hostedZone = new route53.PrivateHostedZone(this, `${id}-zone`, manufacturedProps);
125+
}
126+
}
127+
128+
// Convert IRestApi to RestApi
129+
this.apiGateway = props.existingApiGatewayInterface as api.RestApi;
130+
131+
// Add custom domain name in API Gateway
132+
this.apiGateway.addDomainName('CustomDomainName', {
133+
domainName: this.hostedZone.zoneName,
134+
certificate: this.certificate
135+
});
136+
137+
// Create A Record in custom domain to route traffic to API Gateway
138+
new route53.ARecord(this, 'CustomDomainAliasRecord', {
139+
zone: this.hostedZone,
140+
target: route53.RecordTarget.fromAlias(new targets.ApiGateway(this.apiGateway))
141+
});
142+
}
143+
}

0 commit comments

Comments
 (0)