Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdk-rest-api): add RestApi construct #172

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions packages/cdk-api/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2022 Code d'Azur Interactive B.V.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions packages/cdk-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @codedazur/cdk-api
117 changes: 117 additions & 0 deletions packages/cdk-api/constructs/RestApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import {
RestApi,
RestApiProps,
EndpointType,
IResource,
LambdaIntegration,
SecurityPolicy,
} from "aws-cdk-lib/aws-apigateway";
import { DnsValidatedCertificate } from "aws-cdk-lib/aws-certificatemanager";
import { HttpMethod, IFunction } from "aws-cdk-lib/aws-lambda";
import {
ARecord,
HostedZone,
IHostedZone,
RecordTarget,
} from "aws-cdk-lib/aws-route53";
import { ApiGateway } from "aws-cdk-lib/aws-route53-targets";
import { Construct } from "constructs";

export interface ApiProps extends RestApiProps {
endpoints: Record<string, ResourceProps>;
domain?: DomainProps;
}

interface DomainProps {
name: string;
subdomain?: string;
}

interface ResourceProps {
method?: HttpMethod;
handler?: IFunction;
endpoints?: Record<string, ResourceProps>;
}

export class Api extends RestApi {
constructor(scope: Construct, id: string, { endpoints, ...props }: ApiProps) {
super(scope, id, props);

this.createEndpoints(this.root, endpoints);
this.createDomain(props.domain);
}

protected createEndpoints(
parent: IResource,
children: Record<string, ResourceProps>,
) {
Object.entries(children).forEach(([slug, child]) =>
this.createEndpoint(parent, slug, child),
);
}

protected createEndpoint(
parent: IResource,
slug: string,
{ method, handler, endpoints }: ResourceProps,
) {
const resource = parent.addResource(slug, {
defaultIntegration:
handler && !method ? new LambdaIntegration(handler) : undefined,
});

if (handler && method) {
resource.addMethod(method, new LambdaIntegration(handler));
}

if (endpoints) {
this.createEndpoints(resource, endpoints);
}
}

protected createDomain(domain?: DomainProps) {
if (!domain) {
return;
}

const zone = this.findHostedZone(domain);
const certificate = this.createCertificate(domain, zone);

this.addDomainName("Domain", {
domainName: this.fqdn(domain),
endpointType: EndpointType.EDGE,
securityPolicy: SecurityPolicy.TLS_1_2,
certificate,
});

this.createAlias(domain, zone);
}

protected findHostedZone(domain: DomainProps) {
return HostedZone.fromLookup(this, "HostedZone", {
domainName: domain.name,
});
}

protected createCertificate(domain: DomainProps, zone: IHostedZone) {
return new DnsValidatedCertificate(this, "Certificate", {
domainName: this.fqdn(domain),
hostedZone: zone,
region: "us-east-1",
});
}

protected createAlias(domain: DomainProps, zone: IHostedZone) {
return domain && zone
? new ARecord(this, "DomainAlias", {
recordName: this.fqdn(domain),
target: RecordTarget.fromAlias(new ApiGateway(this)),
zone: zone,
})
: undefined;
}

protected fqdn(domain: DomainProps) {
return [domain.subdomain, domain.name].filter(Boolean).join(".");
}
}
1 change: 1 addition & 0 deletions packages/cdk-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constructs/RestApi";
27 changes: 27 additions & 0 deletions packages/cdk-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@codedazur/cdk-rest-api",
"version": "0.0.0",
"main": "./index.ts",
"types": "./index.ts",
"publishConfig": {
"access": "public"
},
"license": "MIT",
"scripts": {
"lint": "TIMING=1 eslint \"**/*.ts*\"",
"test": "echo \"No tests configured.\""
},
"peerDependencies": {
"aws-cdk-lib": ">=2",
"constructs": ">=10"
},
"devDependencies": {
"@types/node": "^20.4.5",
"aws-cdk-lib": "^2.89.0",
"constructs": "^10.2.69",
"eslint": "^8.46.0",
"@codedazur/eslint-config": "*",
"@codedazur/tsconfig": "*",
"typescript": "^5.1.6"
}
}
5 changes: 5 additions & 0 deletions packages/cdk-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@codedazur/tsconfig/cdk.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
}
2 changes: 1 addition & 1 deletion packages/cdk-cache-invalidator/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# @codedazur/cdk-static-site
thijsdaniels marked this conversation as resolved.
Show resolved Hide resolved
# @codedazur/cdk-cache-invalidator