Skip to content

Commit

Permalink
Fix aws-ts-apigateway compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas11 committed Oct 23, 2023
1 parent 02f2bb6 commit cf12e76
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
70 changes: 40 additions & 30 deletions aws-ts-apigateway/index.ts
Original file line number Diff line number Diff line change
@@ -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", {
Expand All @@ -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 = <any>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,
}],
});

Expand Down
4 changes: 3 additions & 1 deletion aws-ts-apigateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit cf12e76

Please sign in to comment.