AWS
Cloud Watch CLI
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.json
CloudWatch (Ignore case)
fields @timestamp, @message, @logStream, @log
| filter @message like /(?i)(something)/
| sort @timestamp desc
| limit 10000CloudWatch tail
aws logs tail /aws/lambda/$2 \
--follow \
--region ap-southeast-1 \
--profile $1 \
--format shortUpdate lambda code
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
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-lambda{
"build": "rm -rf dist && esbuild src/index.ts --bundle --minify --sourcemap --platform=node --target=es2022 --outfile=dist/index.js",
}DynamoDB
@aws-sdk/lib-dynamodb - Marsal and Unmarsal @aws-sdk/client-dynamodb - Will return in raw format
Template
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 (not @aws-sdk/client-dynamodb) and send it with your DynamoDBDocumentClient.
To get items
const params = {
TableName: `some_table`,
Key: {
id: partitionKey,
timestamp: sortKey
},
};
const data = await ddbClient.send(new GetCommand(params));To add item
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);
});To upadte item
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
}To delete field
const params = {
TableName: `some_table`,
Key: {
id: 'some_id',
},
UpdateExpression: 'REMOVE some_field',
};
await ddbDocClient.send(new UpdateCommand(params)).catch(err => {
// error handling
});Use transaction
const params: TransactWriteCommandInput = {
TransactItems: [{
Update: {
... // some transaction
}
}, {
Put: {
... // some transaction
}
}]
};
await ddbDocClient.send(new TransactWriteCommand(params)).catch(async (err) => {
// error handling
});To scan Supported functions: contains begins_with attribute_exists attribute_not_exists attribute_type size
Supported logical operations: AND OR NOT
Supported operator
| Operator | Meaning |
|---|---|
= | equal |
<> | not equal |
< <= > >= | comparisons |
BETWEEN | range |
IN | value in list |
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);To append list
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)'
}To upadte if the row is exists
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)'
}To upadte if the row is not exists
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)'
}To do batch get
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];
}To do batch write
const params: BatchWriteCommandInput = {
RequestItems: {
[`table_name`]: [
PutRequest: {
Item: {
id: item.id,
value: item.value
}
}
]
}
};
await ddbDocClient.send(new BatchWriteCommand(params)).catch((err) => {
// Error handling
});To delete a row
const params: DeleteCommandInput = {
TableName: 'table_name',
Key: {
id: some_id
}
};
await ddbClient.send(new DeleteCommand(params)).catch((error) => {
// Error handling
});EC2 starter kit
sudo apt update
sudo apt upgrade -y
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo apt install btop fzf ncdu
sudo timedatectl set-timezone Asia/Kuala_Lumpur
sudo systemctl restart cron