Skip to content

Instantly share code, notes, and snippets.

@elijahzarlin
Created June 13, 2019 16:38
Show Gist options
  • Save elijahzarlin/965914a42b386da00278b2644f66b599 to your computer and use it in GitHub Desktop.
Save elijahzarlin/965914a42b386da00278b2644f66b599 to your computer and use it in GitHub Desktop.
pulumixmapbox3.js
```ts
//* Create DynamoDB Table
const assetTable = new aws.dynamodb.Table("assetTable", {
attributes: [
{
name: "id",
type: "S"
},
{
name: "ts",
type: "N"
}
],
hashKey: "id",
rangeKey: "ts",
ttl: {
attributeName: "expiration",
enabled: true
},
billingMode: "PAY_PER_REQUEST"
});
//* Create API to read DynamoDB
const endpoint = new awsx.apigateway.API("assetQuery", {
routes: [
{
path: "/",
method: "GET",
eventHandler: (request, ctx, cb) => {
const AWS = require("aws-sdk");
const ddb = new AWS.DynamoDB.DocumentClient({
apiVersion: "2012-10-08"
});
const tableName = assetTable.name.value;
const params = {
TableName: tableName
};
ddb.scan(params, (err, data) => {
const features = data.Items.map(item => {
const point = turf.point([item.longitude, item.latitude], {
id: item.id,
speed: item.speed
});
return point;
});
const featureCollection = turf.featureCollection(features);
cb(undefined, {
statusCode: 200,
body: Buffer.from(
JSON.stringify(featureCollection),
"utf8"
).toString("base64"),
isBase64Encoded: true,
headers: { "content-type": "application/json" }
});
});
}
}
],
stageName: "dev"
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment