|
1 | 1 | // Copyright 2016-2025, Pulumi Corporation. All rights reserved.
|
2 | 2 |
|
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"; |
4 | 7 | import handler from "./handler";
|
5 | 8 |
|
6 | 9 | /**
|
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/ |
8 | 11 | */
|
9 | 12 |
|
| 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 | + |
10 | 42 | // Create an API endpoint.
|
11 |
| -const endpoint = new awsx.apigateway.API("hello-world", { |
| 43 | +const endpoint = new apigateway.RestAPI("hello-world", { |
12 | 44 | routes: [
|
13 | 45 | {
|
14 | 46 | path: "/{route+}",
|
15 | 47 | 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, |
18 | 50 | },
|
19 | 51 | {
|
20 | 52 | path: "/{route+}",
|
21 | 53 | 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, |
27 | 55 | },
|
28 | 56 | {
|
29 | 57 | path: "/{route+}",
|
30 | 58 | 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, |
36 | 60 | },
|
37 | 61 | ],
|
38 | 62 | });
|
|
0 commit comments