
6minutes Tech Aws Lambda for Dummy
Post Date : 2025-10-14T20:17:17+07:00
Modified Date : 2025-10-14T20:17:17+07:00
Category: cheatsheet 6minutes-tech
Tags:
What is AWS Lambda
AWS Lambda is a compute service that runs code without the need to manage servers. Your code runs, scaling up and down automatically, with pay-per-use pricing
Write your first function
Let’s start by creating a function from a blueprint to see the structure and required materials to allow a lambda function can run. Later, we’ll analyze more details.
After creating a lambda function, AWS already suggested us to setup locally. Let’s ignore it, we are going to see the code first and how to run it.
Links :
- vscode://AmazonWebServices.aws-toolkit-vscode/lambda/load-function?functionName=helloFromNextJSVietnam®ion=us-east-1&isCfn=false&userType=iam-user
- https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html
What we can see in the code is a function that receive an event and its context
Let’s create an event and invoke it to see what’s happened next
Event Name has limitation of length: Maximum of 25 characters
Event has been invoked, now you can see
We must update the code a little bit
console.log("Loading function");
export const handler = async (event, context) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log(context);
console.log(event);
console.log("name =", event.name, typeof event.name);
console.log("website =", event.website, typeof event.website);
console.log("number =", event.number, typeof event.number);
console.log("aBoolean =", event.aBoolean, typeof event.aBoolean);
return {
name: event.name,
website: event.website,
number: event.number,
aBoolean: event.aBoolean,
};
// throw new Error('Something went wrong');
};
To test the changes, you must deploy
Let’s explore
Some interesting tests
Default timeout is 3 seconds
Lambda runs your code for a set amount of time before timing out. Timeout is the maximum amount of time in seconds that a Lambda function can run. The default value for this setting is 3 seconds, but you can adjust this in increments of 1 second up to a maximum value of 900 seconds (15 minutes).
Let’s change it
And try again
So now you can see, the execution time limit is 60 seconds, and when lambda function run, it will check every seconds, with our sample code, here is the final output
console.log("Loading function");
export const handler = async (event, context) => {
console.log("Remaining Time", context.getRemainingTimeInMillis() / 1000);
await new Promise((resolve) => setTimeout(resolve, 5000));
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log("Remaining Time", context.getRemainingTimeInMillis() / 1000);
await new Promise((resolve) => setTimeout(resolve, 5000));
console.log(context);
console.log(event);
console.log("name =", event.name, typeof event.name);
console.log("website =", event.website, typeof event.website);
console.log("number =", event.number, typeof event.number);
console.log("aBoolean =", event.aBoolean, typeof event.aBoolean);
console.log("Remaining Time", context.getRemainingTimeInMillis() / 1000);
return {
name: event.name,
website: event.website,
number: event.number,
aBoolean: event.aBoolean,
remainingTime: context.getRemainingTimeInMillis() / 1000,
};
// throw new Error('Something went wrong');
};
Let’s make the function public so we can test with http request or postman
And you have
It can trigger but
Let’s change
console.log("Loading function");
export const handler = async (event, context) => {
// ---- parse JSON body safely (Function URL / API GW v2) ----
const isB64 = event?.isBase64Encoded;
const raw =
typeof event?.body === "string"
? isB64
? Buffer.from(event.body, "base64").toString("utf8")
: event.body
: null;
let data = {};
if (raw) {
try {
data = JSON.parse(raw);
} catch {
data = {};
}
} else if (event && typeof event === "object") {
// fallback for Lambda Console tests (where event is already an object)
data = event;
}
// ---- read your fields from parsed body ----
const { name, website, number, aBoolean } = data;
// ---- return a proper HTTP response for Function URLs ----
return {
statusCode: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({
name,
website,
number,
aBoolean,
remainingTime: context.getRemainingTimeInMillis() / 1000,
}),
isBase64Encoded: false,
};
};
Let’s finish the topic right there. Next time we’re going to develop locally, run test and deploy a small script with node modules as well.