diff --git a/examples/simple/functions/helloAgain.ts b/examples/simple/functions/helloAgain.ts new file mode 100644 index 0000000..375df74 --- /dev/null +++ b/examples/simple/functions/helloAgain.ts @@ -0,0 +1,18 @@ +import type { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"; + +export interface ProxyResponse { + data: string; +} + +export const handler = async ( + event: APIGatewayProxyEvent +): Promise => { + console.log(JSON.stringify(event, null, 2)); + const response: ProxyResponse = { + data: "Hello World Again!", + }; + return { + statusCode: 200, + body: JSON.stringify(response), + }; +}; diff --git a/packages/construct/package.json b/packages/construct/package.json index ce1983f..29ebdc8 100644 --- a/packages/construct/package.json +++ b/packages/construct/package.json @@ -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", diff --git a/packages/construct/src/serverless-ui.construct.ts b/packages/construct/src/serverless-ui.construct.ts index 2951934..8ca3a1e 100644 --- a/packages/construct/src/serverless-ui.construct.ts +++ b/packages/construct/src/serverless-ui.construct.ts @@ -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"; @@ -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 { /** @@ -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 */ @@ -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); + }, + }; /** * Creating a Cloudfront distribution for the website bucket with an aggressive caching policy @@ -217,7 +218,7 @@ export class ServerlessUI extends Construct { }); this.websiteBucket = websiteBucket; - this.restApis = restApis; + this.httpApi = httpApi; this.functions = lambdas; this.distribution = distribution; } diff --git a/packages/construct/yarn.lock b/packages/construct/yarn.lock index e0cf615..fa0977b 100644 --- a/packages/construct/yarn.lock +++ b/packages/construct/yarn.lock @@ -30,7 +30,21 @@ "@aws-cdk/cx-api" "1.92.0" constructs "^3.2.0" -"@aws-cdk/aws-apigatewayv2@1.92.0": +"@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/aws-apigatewayv2@1.92.0", "@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== @@ -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/aws-cloudfront-origins@1.92.0": 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== @@ -381,6 +395,17 @@ "@aws-cdk/cx-api" "1.92.0" constructs "^3.2.0" +"@aws-cdk/aws-servicediscovery@1.92.0": + 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/aws-signer@1.92.0": version "1.92.0" resolved "https://registry.yarnpkg.com/@aws-cdk/aws-signer/-/aws-signer-1.92.0.tgz#c39e749079a5a965935a3e2ebfda4d8e055a3c5e" diff --git a/yarn.lock b/yarn.lock index f5b39b9..e95aa39 100644 --- a/yarn.lock +++ b/yarn.lock @@ -30,7 +30,21 @@ "@aws-cdk/cx-api" "1.92.0" constructs "^3.2.0" -"@aws-cdk/aws-apigatewayv2@1.92.0": +"@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/aws-apigatewayv2@1.92.0", "@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== @@ -381,6 +395,17 @@ "@aws-cdk/cx-api" "1.92.0" constructs "^3.2.0" +"@aws-cdk/aws-servicediscovery@1.92.0": + 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/aws-signer@1.92.0": version "1.92.0" resolved "https://registry.yarnpkg.com/@aws-cdk/aws-signer/-/aws-signer-1.92.0.tgz#c39e749079a5a965935a3e2ebfda4d8e055a3c5e"