From 5b0c8d1402e49b671c5a3baa444a25d442b69711 Mon Sep 17 00:00:00 2001 From: Thomas Kappler Date: Thu, 5 Oct 2023 08:44:57 +0200 Subject: [PATCH] Fix aws-ts-apigateway compilation --- aws-ts-apigateway/index.ts | 70 +++++++++++++++++++--------------- aws-ts-apigateway/package.json | 4 +- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/aws-ts-apigateway/index.ts b/aws-ts-apigateway/index.ts index 20429bd98..0997ac93c 100644 --- a/aws-ts-apigateway/index.ts +++ b/aws-ts-apigateway/index.ts @@ -1,7 +1,10 @@ // Copyright 2016-2019, Pulumi Corporation. All rights reserved. +import * as dynamoClient from "@aws-sdk/client-dynamodb"; +import * as dynamoLib from "@aws-sdk/lib-dynamodb"; + import * as aws from "@pulumi/aws"; -import * as awsx from "@pulumi/awsx"; +import * as apigateway from "@pulumi/aws-apigateway"; // Create a mapping from 'route' to a count const counterTable = new aws.dynamodb.Table("counterTable", { @@ -14,39 +17,46 @@ const counterTable = new aws.dynamodb.Table("counterTable", { writeCapacity: 5, }); +const getHandler = new aws.lambda.CallbackFunction("GET", { + policies: [aws.iam.ManagedPolicy.AWSLambdaVPCAccessExecutionRole, aws.iam.ManagedPolicy.LambdaFullAccess], + callback: async (ev, ctx) => { + const event = ev; + const route = event.pathParameters!["route"]; + console.log(`Getting count for '${route}'`); + + const dynoClient = new dynamoClient.DynamoDBClient({}); + const doc = dynamoLib.DynamoDBDocument.from(dynoClient); + + // get previous value and increment + // reference outer `counterTable` object + const tableData = await doc.get({ + TableName: counterTable.name.get(), + Key: { id: route }, + ConsistentRead: true, + }); + + const value = tableData.Item; + let count = (value && value.count) || 0; + + await doc.put({ + TableName: counterTable.name.get(), + Item: { id: route, count: ++count }, + }); + + console.log(`Got count ${count} for '${route}'`); + return { + statusCode: 200, + body: JSON.stringify({ route, count }), + }; + }, +}); + // Create an API endpoint -const endpoint = new awsx.apigateway.API("hello-world", { +const endpoint = new apigateway.RestAPI("hello-world", { routes: [{ path: "/{route+}", method: "GET", - eventHandler: async (event) => { - const route = event.pathParameters!["route"]; - console.log(`Getting count for '${route}'`); - - const client = new aws.sdk.DynamoDB.DocumentClient(); - - // get previous value and increment - // reference outer `counterTable` object - const tableData = await client.get({ - TableName: counterTable.name.get(), - Key: { id: route }, - ConsistentRead: true, - }).promise(); - - const value = tableData.Item; - let count = (value && value.count) || 0; - - await client.put({ - TableName: counterTable.name.get(), - Item: { id: route, count: ++count }, - }).promise(); - - console.log(`Got count ${count} for '${route}'`); - return { - statusCode: 200, - body: JSON.stringify({ route, count }), - }; - }, + eventHandler: getHandler, }], }); diff --git a/aws-ts-apigateway/package.json b/aws-ts-apigateway/package.json index fc10abfdd..6e72188f0 100644 --- a/aws-ts-apigateway/package.json +++ b/aws-ts-apigateway/package.json @@ -2,8 +2,10 @@ "name": "aws-ts-apigateway", "version": "0.1.0", "dependencies": { + "@aws-sdk/client-dynamodb": "^3.423.0", + "@aws-sdk/lib-dynamodb": "^3.423.0", "@pulumi/aws": "^6.0.0", - "@pulumi/awsx": "2.0.0-beta.3", + "@pulumi/aws-apigateway": "^2.0.0-alpha.1", "@pulumi/pulumi": "^3.0.0" } }