Skip to content

Instantly share code, notes, and snippets.

@michaelwittig
Last active February 3, 2021 10:28
Show Gist options
  • Save michaelwittig/369718f5045c4c6446bcc5c75edac13b to your computer and use it in GitHub Desktop.
Save michaelwittig/369718f5045c4c6446bcc5c75edac13b to your computer and use it in GitHub Desktop.
esbuild & AWS Lambda
// script
import {
DynamoDBClient,
UpdateItemCommand,
paginateScan
} from '@aws-sdk/client-dynamodb';
async function updateItem(dynamodb, id) {
return dynamodb.send(new UpdateItemCommand({
TableName: 'table-name',
Key: {
id: {S: id}
},
UpdateExpression: 'REMOVE attr1',
ConditionExpression: 'attribute_exists(id)'
}));
}
async function run() {
const dynamodb = new DynamoDBClient({
region: 'us-east-1',
apiVersion: '2012-08-10'
});
const config = {
client: dynamodb,
pageSize: 10
};
const input = {
TableName: 'table-name'
};
const paginator = paginateScan(config, input);
for await (const page of paginator) {
await Promise.all(page.Items.map(item =>
updateItem(dynamodb, item.id.S)));
}
}
run().then(() => {
console.log('done'); // eslint-disable-line no-console
});
// esbuild.js (or esbuild.cjs) file
const esbuild = require('esbuild');
// if you have more than one handler run this in a loop for every handler
esbuild.buildSync({
entryPoints: ['handler1.js'],
bundle: true,
platform: 'node',
external: ['aws-sdk'],
// minify: true,
target: ['node12'],
outfile: 'build/handler1/bundle.js'
});
// in your CloudFormation/SAM template
Function:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: 'build/handler1'
Handler: 'bundle.handler'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment