AWS
CloudWatch CLI
sh
aws logs filter-log-events --log-group-name /aws/lambda/LogGroupName --output json --region ap-southeast-1 --start-time 1706918400000 --end-time 1707004800000 --query "events[?contains(message, 'some text')].{Timestamp:timestamp, Message:message}" > result.jsonCloudWatch Logs Insights
Case-insensitive search
sql
fields @timestamp, @message, @logStream, @log
| filter @message like /(?i)(something)/
| sort @timestamp desc
| limit 10000Tail Lambda logs
sh
aws logs tail /aws/lambda/$2 \
--follow \
--region ap-southeast-1 \
--profile $1 \
--format shortLambda
Update Lambda code
sh
aws lambda update-function-code --region ap-southeast-1 --profile my-profile --function-name YourFunctionName --zip-file fileb://path/to/your/file.zip
aws lambda get-function-configuration --function-name YourFunctionName --region ap-southeast-1 --profile my-profile | jq ."Environment.Variables"
aws lambda update-function-configuration --region ap-southeast-1 --function-name my-lambda-function --environment "Variables={VAR1=new_value1,VAR2=new_value2}"
aws lambda update-function-configuration --region ap-southeast-1 --function-name function-name --environment "Variables={$(cat env.config | jq -r 'to_entries | map("\(.key)=\(.value | gsub(","; "\\,"))") | join(",")')}"Lambda starter kit
sh
pnpm add -D esbuild eslint typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser @types/node @types/aws-lambda globals typescript-eslint @eslint/js
pnpm add @aws-sdk/client-lambdajson
{
"build": "rm -rf dist && esbuild src/index.ts --bundle --minify --sourcemap --platform=node --target=es2022 --outfile=dist/index.js",
}DynamoDB
@aws-sdk/lib-dynamodb handles marshalling and unmarshalling. @aws-sdk/client-dynamodb returns the raw DynamoDB format.
Template
ts
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
ScanCommand, // <-- from @aws-sdk/lib-dynamodb
} from "@aws-sdk/lib-dynamodb";
const ddbClient = new DynamoDBClient({});
const marshallOptions = {
convertEmptyValues: false,
removeUndefinedValues: true,
convertClassInstanceToMap: false,
};
const unmarshallOptions = { wrapNumbers: false };
const translateConfig = { marshallOptions, unmarshallOptions };
const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, translateConfig);
// Use plain JS in your params; no {S:".."} etc.
const params = { TableName: "MyTable" };
const data = await ddbDocClient.send(new ScanCommand(params));
console.log(data.Items); // => [{ id: "123", count: 5, ... }] // normal JSONNote: Import the command from @aws-sdk/lib-dynamodb instead of @aws-sdk/client-dynamodb, then send it with DynamoDBDocumentClient.
Get items
ts
const params = {
TableName: `some_table`,
Key: {
id: partitionKey,
timestamp: sortKey
},
};
const data = await ddbClient.send(new GetCommand(params));Add an item
ts
const putParams = {
TableName: `table_name`,
Item: {
id: some_id,
createTime: now.getTime(),
status: 'P',
payer,
details,
amount
},
};
await ddbDocClient.send(new PutCommand(putParams)).catch((err) => {
log.error(err);
});Update an item
ts
const params = {
TableName: `some_table`,
Key: {
id: 'some_id',
},
Item: someObject,
ExpressionAttributeNames: { '#v': 'value', },
UpdateExpression: 'set #v = :v',
ExpressionAttributeValues: {
':v': someObject
},
};
await ddbDocClient.send(new UpdateCommand(params)).catch((err) => {
// error handling
}Delete a field
ts
const params = {
TableName: `some_table`,
Key: {
id: 'some_id',
},
UpdateExpression: 'REMOVE some_field',
};
await ddbDocClient.send(new UpdateCommand(params)).catch(err => {
// error handling
});Use transactions
ts
const params: TransactWriteCommandInput = {
TransactItems: [{
Update: {
... // some transaction
}
}, {
Put: {
... // some transaction
}
}]
};
await ddbDocClient.send(new TransactWriteCommand(params)).catch(async (err) => {
// error handling
});Scan
Supported functions:
containsbegins_withattribute_existsattribute_not_existsattribute_typesize
Supported logical operations:
ANDORNOT
Supported operators:
| Operator | Meaning |
|---|---|
= | equal |
<> | not equal |
< <= > >= | comparisons |
BETWEEN | range |
IN | value in list |
ts
const params: UpdateParam = {
TableName: `some_table`,
ExpressionAttributeNames: { '#d': 'date' },
ExpressionAttributeValues: {
':d1': from_date,
':d2': to_date
},
ScanIndexForward: false,
FilterExpression: '#d BETWEEN :d1 and :d2' // other function: contains
};
try {
let data;
do {
if (data) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
}
data = await ddbClient.send(new ScanCommand(params));
if (data.Items && data.Items instanceof Array) {
for (const item of data.Items) {
// append to list
}
}
} while (data.LastEvaluatedKey);Append to a list
ts
const params = {
TableName: `some_table`,
Key: {
id: some_key
},
ExpressionAttributeNames: {
'#l': 'list'
},
ExpressionAttributeValues: {
':l': arrayList,
':emptyList': []
},
UpdateExpression: 'SET #l = list_append(if_not_exists(#l, :emptyList), :l)'
}Update only if the row exists
ts
const params = {
TableName: `some_table`,
Key: {
id: some_key.
sid: some_sort_key
},
ExpressionAttributeNames: { '#v': 'value', },
UpdateExpression: 'set #v = :v',
ExpressionAttributeValues: {
':v': someObject
},
ConditionExpression: 'attribute_exists(#id) AND attribute_exists(#sid)'
}Update only if the row does not exist
ts
const params = {
TableName: `some_table`,
Key: {
id: some_key.
sid: some_sort_key
},
ExpressionAttributeNames: { '#v': 'value', },
UpdateExpression: 'set #v = :v',
ExpressionAttributeValues: {
':v': someObject
},
ConditionExpression: 'attribute_not_exists(#id) AND attribute_not_exists(#sid)'
}Batch get
ts
const params: BatchGetCommandInput = {
RequestItems: {
[tableName]: {
Keys: [ ... some_keys ]
}
}
};
const data = await ddbClient.send(new BatchGetCommand(params));
if (data.Responses && data.Responses[tableName]) {
return data.Responses[tableName];
}Batch write
ts
const params: BatchWriteCommandInput = {
RequestItems: {
[`table_name`]: [
PutRequest: {
Item: {
id: item.id,
value: item.value
}
}
]
}
};
await ddbDocClient.send(new BatchWriteCommand(params)).catch((err) => {
// Error handling
});Delete a row
ts
const params: DeleteCommandInput = {
TableName: 'table_name',
Key: {
id: some_id
}
};
await ddbClient.send(new DeleteCommand(params)).catch((error) => {
// Error handling
});EC2 starter kit
For the Ubuntu EC2 bootstrap script, refer to Operating System.