Skip to content

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 10000

CloudWatch tail

aws logs tail /aws/lambda/$2 \
  --follow \
  --region ap-southeast-1 \
  --profile $1 \
  --format short

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-lambda
json
{
  "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

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 JSON

Note: Import the command from @aws-sdk/lib-dynamodb (not @aws-sdk/client-dynamodb) and send it with your DynamoDBDocumentClient.

To get items

ts
const params = {
  TableName: `some_table`,
  Key: {
    id: partitionKey,
    timestamp: sortKey
  },
};
const data = await ddbClient.send(new GetCommand(params));

To add 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);
});

To upadte 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
}

To delete 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 transaction

ts
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

OperatorMeaning
=equal
<>not equal
< <= > >=comparisons
BETWEENrange
INvalue 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);

To append 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)'
}

To upadte if the row is 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)'
}

To upadte if the row is not 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_not_exists(#id) AND attribute_not_exists(#sid)'
}

To do 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];
}

To do 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
});

To 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

bash
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