Skip to content

Commit

Permalink
created r53-apigw construct
Browse files Browse the repository at this point in the history
  • Loading branch information
mickychetta committed Jan 5, 2022
1 parent b932c1b commit dbbac6a
Show file tree
Hide file tree
Showing 8 changed files with 839,962 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lib/*.js
test/*.js
*.d.ts
coverage
test/lambda/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
lib/*.js
test/*.js
*.js.map
*.d.ts
node_modules
*.generated.ts
dist
.jsii

.LAST_BUILD
.nyc_output
coverage
.nycrc
.LAST_PACKAGE
*.snk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Exclude typescript source and config
*.ts
tsconfig.json
coverage
.nyc_output
*.tgz
*.snk
*.tsbuildinfo

# Include javascript files and typescript declarations
!*.js
!*.d.ts

# Exclude jsii outdir
dist

# Include .jsii
!.jsii

# Include .jsii
!.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ This construct can create Private Hosted Zones. If you want a Private Hosted Zon

| **Name** | **Type** | **Description** |
|:-------------|:----------------|-----------------|
| publicApi | boolean | Whether the construct is deploying a private or public API. This has implications for the Hosted Zone and VPC. |
| 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. |
| 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.|
| publicApi | boolean | Whether the construct is deploying a private or public API. This has implications for the Hosted Zone and VPC. |
| 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.|
|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.*|
| 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.|

## Pattern Properties

Expand All @@ -84,6 +85,7 @@ This construct can create Private Hosted Zones. If you want a Private Hosted Zon
|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) |
| 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 provided by the client) |
|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.|
|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)

## Default settings
Out of the box implementation of the Construct without any override will set the following defaults:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

// Imports
import * as api from '@aws-cdk/aws-apigateway';
import * as route53 from "@aws-cdk/aws-route53";
import * as targets from '@aws-cdk/aws-route53-targets';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as defaults from '@aws-solutions-constructs/core';
import * as certificatemanager from '@aws-cdk/aws-certificatemanager';
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate
import { Construct } from '@aws-cdk/core';

/**
* The properties for the Route53ToApiGateway class.
*/
export interface Route53ToApiGatewayProps {
/**
* Whether to create a public or private API. This value has implications
* for the VPC, the type of Hosted Zone and the Application Load Balancer
*
* @default - None
*/
readonly publicApi: boolean
/**
* Custom properties for a new Private Hosted Zone. Cannot be specified for a
* public API. Cannot specify a VPC
*
* @default - None
*/
readonly privateHostedZoneProps?: route53.PrivateHostedZoneProps | any,
/**
* Existing Public or Private Hosted Zone. If a Private Hosted Zone, must
* exist in the same VPC specified in existingVpc
*
* @default - None
*/
readonly existingHostedZoneInterface?: route53.IHostedZone,
/**
* An existing VPC. Providing both this and vpcProps is an error. If an existingAlb or existing
* Private Hosted Zone is provided, this value must be the VPC associated with those resources.
*
* @default - None
*/
readonly existingVpc?: ec2.IVpc,
/**
* The existing API Gateway instance that will be protected with the WAF web ACL.
*
* @default - None
*/
readonly existingApiGatewayInterface: api.IRestApi,
/**
* An existing AWS Certificate Manager certificate for your custom domain name.
*
* @defualt - None
*/
readonly existingCertificateInterface?: certificatemanager.ICertificate;
}

/**
* @summary The Route53ToApiGateway class.
*/
export class Route53ToApiGateway extends Construct {
public readonly hostedZone: route53.IHostedZone;
public readonly vpc?: ec2.IVpc;
public readonly apiGateway: api.RestApi;
public readonly certificate: certificatemanager.ICertificate;
/**
* @summary Constructs a new instance of the Route53ToApiGateway class.
* @param {cdk.App} scope - represents the scope for all the resources.
* @param {string} id - this is a a scope-unique id.
* @param {Route53ToApiGatewayProps} props - user provided props for the construct
* @since 0.8.0
* @access public
*/
constructor(scope: Construct, id: string, props: Route53ToApiGatewayProps) {
super(scope, id);
defaults.CheckProps(props);

if (props.existingVpc) {
this.vpc = props.existingVpc;
}

// Existing Public or Private Hosted Zone
if (props.existingHostedZoneInterface) {
this.hostedZone = props.existingHostedZoneInterface;
} else { // Creating a Private Hosted Zone
if (props.publicApi) {
throw new Error('Public APIs require an existingHostedZone be passed in the Props object.');
} else {
if (!props.privateHostedZoneProps) {
throw new Error('Must supply privateHostedZoneProps to create a private API');
}
if (props.privateHostedZoneProps.vpc) {
throw new Error('All VPC specs must be provided at the Construct level in Route53ToApiGatewayProps');
}
const manufacturedProps: route53.PrivateHostedZoneProps = defaults.overrideProps(props.privateHostedZoneProps, { vpc: this.vpc });
this.hostedZone = new route53.PrivateHostedZone(this, `${id}-zone`, manufacturedProps);
}
}

// Create certificate and validate CNAME Record with DNS
if (props.existingCertificateInterface) { // Certificate must already be issued if user is bringing an existing one
this.certificate = props.existingCertificateInterface;
} else {
this.certificate = new certificatemanager.Certificate(this, 'Certificate', {
domainName: this.hostedZone.zoneName,
validation: certificatemanager.CertificateValidation.fromDns(this.hostedZone),
});
}

// Convert IRestApi to RestApi
this.apiGateway = props.existingApiGatewayInterface as api.RestApi;

// Add custom domain name in API Gateway
this.apiGateway.addDomainName('CustomDomainName', {
domainName: this.hostedZone.zoneName,
certificate: this.certificate
});

// Create A Record in custom domain to route traffic to API Gateway
new route53.ARecord(this, 'CustomDomainAliasRecord', {
zone: this.hostedZone,
target: route53.RecordTarget.fromAlias(new targets.ApiGateway(this.apiGateway))
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"name": "@aws-solutions-constructs/aws-route53-apigateway",
"version": "0.0.0",
"description": "CDK constructs for connecting an Amazon Route53 domain to an API Gateway.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/awslabs/aws-solutions-constructs.git",
"directory": "source/patterns/@aws-solutions-constructs/aws-route53-apigateway"
},
"author": {
"name": "Amazon Web Services",
"url": "https://aws.amazon.com",
"organization": true
},
"license": "Apache-2.0",
"scripts": {
"build": "tsc -b .",
"lint": "eslint -c ../eslintrc.yml --ext=.js,.ts . && tslint --project .",
"lint-fix": "eslint -c ../eslintrc.yml --ext=.js,.ts --fix .",
"test": "jest --coverage",
"clean": "tsc -b --clean",
"watch": "tsc -b -w",
"integ": "cdk-integ",
"integ-no-clean": "cdk-integ --no-clean",
"integ-assert": "cdk-integ-assert",
"jsii": "jsii",
"jsii-pacmak": "jsii-pacmak",
"build+lint+test": "npm run jsii && npm run lint && npm test && npm run integ-assert",
"snapshot-update": "npm run jsii && npm test -- -u && npm run integ-assert"
},
"jsii": {
"outdir": "dist",
"targets": {
"java": {
"package": "software.amazon.awsconstructs.services.route53apigateway",
"maven": {
"groupId": "software.amazon.awsconstructs",
"artifactId": "route53apigateway"
}
},
"dotnet": {
"namespace": "Amazon.SolutionsConstructs.AWS.Route53ApiGateway",
"packageId": "Amazon.SolutionsConstructs.AWS.Route53ApiGateway",
"signAssembly": true,
"iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png"
},
"python": {
"distName": "aws-solutions-constructs.aws-route53-apigateway",
"module": "aws_solutions_constructs.aws_route53_apigateway"
}
}
},
"dependencies": {
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-apigateway": "0.0.0",
"@aws-cdk/aws-certificatemanager": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-route53": "0.0.0",
"@aws-cdk/aws-route53-targets": "0.0.0",
"@aws-solutions-constructs/core": "0.0.0",
"constructs": "^3.2.0"
},
"devDependencies": {
"@aws-cdk/assert": "0.0.0",
"@types/jest": "^26.0.22",
"@types/node": "^10.3.0",
"prettier": "^2.5.1"
},
"jest": {
"moduleFileExtensions": [
"js"
],
"coverageReporters": [
"text",
[
"lcov",
{
"projectRoot": "../../../../"
}
]
]
},
"peerDependencies": {
"@aws-cdk/aws-apigateway": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-solutions-constructs/core": "0.0.0",
"constructs": "^3.2.0"
},
"keywords": [
"aws",
"cdk",
"awscdk",
"AWS Solutions Constructs",
"Amazon API Gateway",
"Amazon Route 53"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.handler = async (event, context) => {
console.log('Received event:', JSON.stringify(event, null, 2));
return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: `Hello from Lambda! You've hit ${event.path}\n`
};
};
Loading

0 comments on commit dbbac6a

Please sign in to comment.