Skip to content

Commit ef31a15

Browse files
authored
Fix: Updating aws-ts-apigateway-lambda-serverless for TS2339, fixes 2159 (#2164)
This pull request includes several changes to improve the AWS API Gateway and Lambda serverless setup. The most important changes involve importing additional modules, modifying the handler function to include context, and creating separate Lambda functions for each HTTP method. ### Improvements to Lambda handler: * [`aws-ts-apigateway-lambda-serverless/handler.ts`](diffhunk://#diff-0a564020005e94424a9318c3ad252cb4119c4304b15ebe92e953385768f0b355L4-R17): Modified the handler function to include the `Context` parameter and updated the function signature to use `APIGatewayProxyEvent` and `APIGatewayProxyResult` types. ### Enhancements to API Gateway setup: * [`aws-ts-apigateway-lambda-serverless/index.ts`](diffhunk://#diff-cbf043ee80f083bc116640a425c2f7c19700de323da4fef1d55d0a8faf851bc7L3-R60): Replaced the use of `awsx.apigateway.API` with `apigateway.RestAPI`, and created separate Lambda functions for GET, POST, and DELETE methods using `aws.lambda.CallbackFunction`. ### Dependency updates: * [`aws-ts-apigateway-lambda-serverless/package.json`](diffhunk://#diff-4c66d2c846846f26db19087ce1526a6ed4a1ba78ed5947f6e7e47187496e39a4R9): Added `@pulumi/aws-apigateway` as a new dependency. This fixes #2159
1 parent 9a35f2d commit ef31a15

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

aws-ts-apigateway-lambda-serverless/handler.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
// Copyright 2016-2025, Pulumi Corporation. All rights reserved.
22

33
import * as aws from "@pulumi/aws";
4-
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
4+
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from "aws-lambda";
55

66
/**
77
* A simple function that returns the request.
88
*
9-
* @param {APIGatewayProxyEvent} event -
10-
* @returns returns a confirmation to the message to the
9+
* @param {APIGatewayProxyEvent} event - API Gateway event
10+
* @param {Context} context - Lambda context
11+
* @returns returns a confirmation to the message
1112
*/
12-
export const handler: aws.lambda.EventHandler<APIGatewayProxyEvent, APIGatewayProxyResult> = async (event) => {
13-
const route = event.pathParameters!["route"];
13+
export const handler = async (
14+
event: APIGatewayProxyEvent,
15+
context: Context,
16+
): Promise<APIGatewayProxyResult> => {
17+
const route = event.pathParameters?.["route"] || "default";
1418
const body = event.body ? JSON.parse(event.body) : null;
1519

1620
console.log("Received body: ", body);

aws-ts-apigateway-lambda-serverless/index.ts

+39-15
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
11
// Copyright 2016-2025, Pulumi Corporation. All rights reserved.
22

3-
import * as awsx from "@pulumi/awsx";
3+
import * as aws from "@pulumi/aws";
4+
import * as apigateway from "@pulumi/aws-apigateway";
5+
import * as pulumi from "@pulumi/pulumi";
6+
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
47
import handler from "./handler";
58

69
/**
7-
* api-gatewayx https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/
10+
* api-gateway https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/
811
*/
912

13+
// Create Lambda functions for our API
14+
const handlerFunction = new aws.lambda.CallbackFunction("get-handler", {
15+
callback: handler,
16+
runtime: aws.lambda.Runtime.NodeJS18X,
17+
});
18+
19+
const postHandlerFunction = new aws.lambda.CallbackFunction("post-handler", {
20+
callback: async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
21+
console.log("Inline event handler");
22+
console.log(event);
23+
return {
24+
statusCode: 200,
25+
body: JSON.stringify({ message: "POST successful" }),
26+
};
27+
},
28+
runtime: aws.lambda.Runtime.NodeJS18X,
29+
});
30+
31+
const deleteHandlerFunction = new aws.lambda.CallbackFunction("delete-handler", {
32+
callback: async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
33+
console.log(event);
34+
return {
35+
statusCode: 200,
36+
body: JSON.stringify({ message: "DELETE successful" }),
37+
};
38+
},
39+
runtime: aws.lambda.Runtime.NodeJS18X,
40+
});
41+
1042
// Create an API endpoint.
11-
const endpoint = new awsx.apigateway.API("hello-world", {
43+
const endpoint = new apigateway.RestAPI("hello-world", {
1244
routes: [
1345
{
1446
path: "/{route+}",
1547
method: "GET",
16-
// Functions can be imported from other modules
17-
eventHandler: handler,
48+
// Use the Lambda function reference for the event handler
49+
eventHandler: handlerFunction,
1850
},
1951
{
2052
path: "/{route+}",
2153
method: "POST",
22-
// Functions can be created inline
23-
eventHandler: (event) => {
24-
console.log("Inline event handler");
25-
console.log(event);
26-
},
54+
eventHandler: postHandlerFunction,
2755
},
2856
{
2957
path: "/{route+}",
3058
method: "DELETE",
31-
// Functions can be created inline
32-
eventHandler: (event) => {
33-
console.log("Inline delete event handler");
34-
console.log(event);
35-
},
59+
eventHandler: deleteHandlerFunction,
3660
},
3761
],
3862
});

aws-ts-apigateway-lambda-serverless/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
"dependencies": {
88
"@pulumi/aws": "6.73.0",
9+
"@pulumi/aws-apigateway": "2.6.2",
910
"@pulumi/awsx": "2.21.1",
1011
"@pulumi/pulumi": "3.158.0"
1112
}

0 commit comments

Comments
 (0)