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: using a single HTTP API Gateway for lambda function deployments #30

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions examples/simple/functions/helloAgain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";

export interface ProxyResponse {
data: string;
}

export const handler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
console.log(JSON.stringify(event, null, 2));
const response: ProxyResponse = {
data: "Hello World Again!",
};
return {
statusCode: 200,
body: JSON.stringify(response),
};
};
3 changes: 2 additions & 1 deletion packages/construct/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"prepack": "yarn rimraf && yarn build"
},
"dependencies": {
"@aws-cdk/aws-apigateway": "1.92.0",
"@aws-cdk/aws-apigatewayv2": "^1.92.0",
"@aws-cdk/aws-apigatewayv2-integrations": "^1.92.0",
"@aws-cdk/aws-certificatemanager": "1.92.0",
"@aws-cdk/aws-cloudfront": "1.92.0",
"@aws-cdk/aws-cloudfront-origins": "1.92.0",
Expand Down
43 changes: 22 additions & 21 deletions packages/construct/src/serverless-ui.construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import {
IHostedZone,
} from "@aws-cdk/aws-route53";
import { CloudFrontTarget } from "@aws-cdk/aws-route53-targets";
import { IRestApi, LambdaRestApi } from "@aws-cdk/aws-apigateway";
import {
IDistribution,
Distribution,
ViewerProtocolPolicy,
BehaviorOptions,
AllowedMethods,
CachePolicy,
} from "@aws-cdk/aws-cloudfront";
Expand All @@ -22,6 +20,8 @@ import { CfnOutput, Construct, RemovalPolicy, Stack } from "@aws-cdk/core";
import { Bucket, IBucket } from "@aws-cdk/aws-s3";
import * as path from "path";
import { HttpOrigin, S3Origin } from "@aws-cdk/aws-cloudfront-origins";
import { LambdaProxyIntegration } from "@aws-cdk/aws-apigatewayv2-integrations";
import { HttpApi, IHttpApi } from "@aws-cdk/aws-apigatewayv2";

interface Domain {
/**
Expand Down Expand Up @@ -76,9 +76,9 @@ export class ServerlessUI extends Construct {
*/
readonly websiteBucket: IBucket;
/**
* The API Gateway APIs for the function deployments
* The API Gateway API for the function deployments
*/
readonly restApis: IRestApi[];
readonly httpApi: IHttpApi;
/**
* The Node.js Lambda Functions deployed
*/
Expand Down Expand Up @@ -119,32 +119,33 @@ export class ServerlessUI extends Construct {
});
});

const restApis = lambdas.map((lambda, i) => {
return new LambdaRestApi(this, `LambdaRestApi-${functionFiles[i].name}`, {
const httpApi = new HttpApi(this, "HttpApi");

lambdas.forEach((lambda, i) => {
const lambdaFileName = functionFiles[i].name;
const lambdaProxyIntegration = new LambdaProxyIntegration({
handler: lambda,
});

httpApi.addRoutes({
path: `/api/${lambdaFileName}`,
integration: lambdaProxyIntegration,
});
});

/**
* Build a Cloudfront behavior for each api function that allows all HTTP Methods and has caching disabled.
*/
const additionalBehaviors: Record<
string,
BehaviorOptions
> = restApis.reduce((previous, current, i) => {
const functionName = functionFiles[i].name;
const restApiOrigin = `${current.restApiId}.execute-api.${
Stack.of(this).region
}.amazonaws.com`;
const newAdditionalBehaviors = { ...previous };
newAdditionalBehaviors[`/api/${functionName}`] = {
origin: new HttpOrigin(restApiOrigin, { originPath: "/prod" }),
const additionalBehaviors = {
"/api/*": {
origin: new HttpOrigin(
`${httpApi.apiId}.execute-api.${Stack.of(this).region}.amazonaws.com`
),
cachePolicy: CachePolicy.CACHING_DISABLED,
allowedMethods: AllowedMethods.ALLOW_ALL,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
};
return newAdditionalBehaviors;
}, {} as Record<string, BehaviorOptions>);
},
};

/**
* Creating a Cloudfront distribution for the website bucket with an aggressive caching policy
Expand Down Expand Up @@ -217,7 +218,7 @@ export class ServerlessUI extends Construct {
});

this.websiteBucket = websiteBucket;
this.restApis = restApis;
this.httpApi = httpApi;
this.functions = lambdas;
this.distribution = distribution;
}
Expand Down
29 changes: 27 additions & 2 deletions packages/construct/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@
"@aws-cdk/cx-api" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]":
"@aws-cdk/aws-apigatewayv2-integrations@^1.92.0":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-apigatewayv2-integrations/-/aws-apigatewayv2-integrations-1.92.0.tgz#987d206bc518ff5a61475ab3755be5e64c80a5f5"
integrity sha512-d0u2w5sP0zwnjesgmip5AntsjSPnDFYcnevKzSTKIe0MDnIOVg1HAZyVXeJGX/WBBC2eA5DS0GBndDqzr+VqMw==
dependencies:
"@aws-cdk/aws-apigatewayv2" "1.92.0"
"@aws-cdk/aws-ec2" "1.92.0"
"@aws-cdk/aws-elasticloadbalancingv2" "1.92.0"
"@aws-cdk/aws-iam" "1.92.0"
"@aws-cdk/aws-lambda" "1.92.0"
"@aws-cdk/aws-servicediscovery" "1.92.0"
"@aws-cdk/core" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]", "@aws-cdk/aws-apigatewayv2@^1.92.0":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-apigatewayv2/-/aws-apigatewayv2-1.92.0.tgz#cc9c7caf160e5bee99397de78297f499d3842acc"
integrity sha512-BKM8x7SMDfefmdeUpnHhEx4ncTxBdL4MoZGaMkAibAcUk3Ok2vHEy2eWrl4jK8WTFYfUQivjsfnRFQk6755kZg==
Expand Down Expand Up @@ -86,7 +100,7 @@
"@aws-cdk/cx-api" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/aws-cloudfront-origins@^1.92.0":
"@aws-cdk/[email protected]":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-cloudfront-origins/-/aws-cloudfront-origins-1.92.0.tgz#5d94b35de191234b31d7a53ac37ffd6a163f52ed"
integrity sha512-UmgJxdcDsfdl3emPCggO7js90Hdnzb6iinFPu7ehiCSAprSGfyQ325VepwCe2vg1VGjZ4sIjZykrzIuSc7N84Q==
Expand Down Expand Up @@ -381,6 +395,17 @@
"@aws-cdk/cx-api" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-servicediscovery/-/aws-servicediscovery-1.92.0.tgz#0a2ba39f9898dd60c7ecb8eede48c523a95797db"
integrity sha512-i99S157y0Y7lABd3xUXnh/o8f29q5V9wNI0RiWiDF3gVspt6GUlHgOodIhvZ+yO5kKYhzU8LNB76VCcrW7jSdQ==
dependencies:
"@aws-cdk/aws-ec2" "1.92.0"
"@aws-cdk/aws-elasticloadbalancingv2" "1.92.0"
"@aws-cdk/aws-route53" "1.92.0"
"@aws-cdk/core" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-signer/-/aws-signer-1.92.0.tgz#c39e749079a5a965935a3e2ebfda4d8e055a3c5e"
Expand Down
27 changes: 26 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@
"@aws-cdk/cx-api" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]":
"@aws-cdk/aws-apigatewayv2-integrations@^1.92.0":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-apigatewayv2-integrations/-/aws-apigatewayv2-integrations-1.92.0.tgz#987d206bc518ff5a61475ab3755be5e64c80a5f5"
integrity sha512-d0u2w5sP0zwnjesgmip5AntsjSPnDFYcnevKzSTKIe0MDnIOVg1HAZyVXeJGX/WBBC2eA5DS0GBndDqzr+VqMw==
dependencies:
"@aws-cdk/aws-apigatewayv2" "1.92.0"
"@aws-cdk/aws-ec2" "1.92.0"
"@aws-cdk/aws-elasticloadbalancingv2" "1.92.0"
"@aws-cdk/aws-iam" "1.92.0"
"@aws-cdk/aws-lambda" "1.92.0"
"@aws-cdk/aws-servicediscovery" "1.92.0"
"@aws-cdk/core" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]", "@aws-cdk/aws-apigatewayv2@^1.92.0":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-apigatewayv2/-/aws-apigatewayv2-1.92.0.tgz#cc9c7caf160e5bee99397de78297f499d3842acc"
integrity sha512-BKM8x7SMDfefmdeUpnHhEx4ncTxBdL4MoZGaMkAibAcUk3Ok2vHEy2eWrl4jK8WTFYfUQivjsfnRFQk6755kZg==
Expand Down Expand Up @@ -381,6 +395,17 @@
"@aws-cdk/cx-api" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-servicediscovery/-/aws-servicediscovery-1.92.0.tgz#0a2ba39f9898dd60c7ecb8eede48c523a95797db"
integrity sha512-i99S157y0Y7lABd3xUXnh/o8f29q5V9wNI0RiWiDF3gVspt6GUlHgOodIhvZ+yO5kKYhzU8LNB76VCcrW7jSdQ==
dependencies:
"@aws-cdk/aws-ec2" "1.92.0"
"@aws-cdk/aws-elasticloadbalancingv2" "1.92.0"
"@aws-cdk/aws-route53" "1.92.0"
"@aws-cdk/core" "1.92.0"
constructs "^3.2.0"

"@aws-cdk/[email protected]":
version "1.92.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-signer/-/aws-signer-1.92.0.tgz#c39e749079a5a965935a3e2ebfda4d8e055a3c5e"
Expand Down