Skip to content

Commit 174a82e

Browse files
committed
6minutes aws lambda for dummy
1 parent c7e9bee commit 174a82e

File tree

178 files changed

+1265
-402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+1265
-402
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
---
2+
title: "6minutes Tech Aws Lambda for Dummy"
3+
type: "post"
4+
date: 2025-10-14T20:17:17+07:00
5+
description: "In this topic, you will learn the main concept about AWS Lambda, develop locally and deploy to production"
6+
keywords: ["6minutes Tech Aws Lambda for Dummy"]
7+
keywords: ["Dependency Injection and Ioc"]
8+
categories: ["cheatsheet", "6minutes-tech"]
9+
tags: []
10+
image: /articles/6minutes/002.png
11+
---
12+
13+
## What is AWS Lambda
14+
15+
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_
16+
17+
### Write your first function
18+
19+
![Lambda service](/articles/6minutes/aws-lambda/001.png)
20+
21+
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.
22+
23+
![Lambda blueprint function](/articles/6minutes/aws-lambda/002.png)
24+
25+
After creating a lambda function, AWS already suggested us to setup locally.
26+
Let's ignore it, we are going to see the code first and how to run it.
27+
28+
Links :
29+
30+
- vscode://AmazonWebServices.aws-toolkit-vscode/lambda/load-function?functionName=helloFromNextJSVietnam&region=us-east-1&isCfn=false&userType=iam-user
31+
- https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html
32+
33+
![Lambda blueprint function](/articles/6minutes/aws-lambda/003.png)
34+
35+
What we can see in the code is a function that receive an event and its context
36+
37+
![Lambda event handler](/articles/6minutes/aws-lambda/004.png)
38+
39+
Let's create an event and invoke it to see what's happened next
40+
41+
![Lambda event ](/articles/6minutes/aws-lambda/005.png)
42+
43+
Event Name has limitation of length: Maximum of 25 characters
44+
45+
![Lambda event ](/articles/6minutes/aws-lambda/006.png)
46+
47+
Event has been invoked, now you can see
48+
49+
![Lambda event ](/articles/6minutes/aws-lambda/007.png)
50+
51+
We must update the code a little bit
52+
53+
```js
54+
console.log("Loading function");
55+
56+
export const handler = async (event, context) => {
57+
//console.log('Received event:', JSON.stringify(event, null, 2));
58+
console.log(context);
59+
console.log(event);
60+
console.log("name =", event.name, typeof event.name);
61+
console.log("website =", event.website, typeof event.website);
62+
console.log("number =", event.number, typeof event.number);
63+
console.log("aBoolean =", event.aBoolean, typeof event.aBoolean);
64+
return {
65+
name: event.name,
66+
website: event.website,
67+
number: event.number,
68+
aBoolean: event.aBoolean,
69+
};
70+
// throw new Error('Something went wrong');
71+
};
72+
```
73+
74+
To test the changes, you must deploy
75+
76+
![Lambda event ](/articles/6minutes/aws-lambda/008.png)
77+
78+
Let's explore
79+
80+
![Lambda event ](/articles/6minutes/aws-lambda/009.png)
81+
82+
![Lambda event ](/articles/6minutes/aws-lambda/010.png)
83+
84+
### Some interesting tests
85+
86+
Default timeout is 3 seconds
87+
88+
![Lambda event ](/articles/6minutes/aws-lambda/011.png)
89+
90+
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).
91+
92+
Let's change it
93+
94+
![Lambda event ](/articles/6minutes/aws-lambda/012.png)
95+
96+
![Lambda event ](/articles/6minutes/aws-lambda/013.png)
97+
98+
And try again
99+
100+
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
101+
102+
```js
103+
console.log("Loading function");
104+
105+
export const handler = async (event, context) => {
106+
console.log("Remaining Time", context.getRemainingTimeInMillis() / 1000);
107+
await new Promise((resolve) => setTimeout(resolve, 5000));
108+
//console.log('Received event:', JSON.stringify(event, null, 2));
109+
console.log("Remaining Time", context.getRemainingTimeInMillis() / 1000);
110+
await new Promise((resolve) => setTimeout(resolve, 5000));
111+
console.log(context);
112+
console.log(event);
113+
console.log("name =", event.name, typeof event.name);
114+
console.log("website =", event.website, typeof event.website);
115+
console.log("number =", event.number, typeof event.number);
116+
console.log("aBoolean =", event.aBoolean, typeof event.aBoolean);
117+
console.log("Remaining Time", context.getRemainingTimeInMillis() / 1000);
118+
return {
119+
name: event.name,
120+
website: event.website,
121+
number: event.number,
122+
aBoolean: event.aBoolean,
123+
remainingTime: context.getRemainingTimeInMillis() / 1000,
124+
};
125+
// throw new Error('Something went wrong');
126+
};
127+
```
128+
129+
![Lambda event ](/articles/6minutes/aws-lambda/015.png)
130+
131+
Let's make the function public so we can test with http request or postman
132+
133+
![Lambda event ](/articles/6minutes/aws-lambda/016.png)
134+
135+
![Lambda event ](/articles/6minutes/aws-lambda/017.png)
136+
137+
And you have
138+
139+
![Lambda event ](/articles/6minutes/aws-lambda/018.png)
140+
141+
It can trigger but
142+
143+
![Lambda event ](/articles/6minutes/aws-lambda/019.png)
144+
145+
Let's change
146+
147+
```js
148+
console.log("Loading function");
149+
150+
export const handler = async (event, context) => {
151+
// ---- parse JSON body safely (Function URL / API GW v2) ----
152+
const isB64 = event?.isBase64Encoded;
153+
154+
const raw =
155+
typeof event?.body === "string"
156+
? isB64
157+
? Buffer.from(event.body, "base64").toString("utf8")
158+
: event.body
159+
: null;
160+
161+
let data = {};
162+
if (raw) {
163+
try {
164+
data = JSON.parse(raw);
165+
} catch {
166+
data = {};
167+
}
168+
} else if (event && typeof event === "object") {
169+
// fallback for Lambda Console tests (where event is already an object)
170+
data = event;
171+
}
172+
173+
// ---- read your fields from parsed body ----
174+
const { name, website, number, aBoolean } = data;
175+
176+
// ---- return a proper HTTP response for Function URLs ----
177+
return {
178+
statusCode: 200,
179+
headers: { "content-type": "application/json" },
180+
body: JSON.stringify({
181+
name,
182+
website,
183+
number,
184+
aBoolean,
185+
remainingTime: context.getRemainingTimeInMillis() / 1000,
186+
}),
187+
isBase64Encoded: false,
188+
};
189+
};
190+
```
191+
192+
![Lambda event ](/articles/6minutes/aws-lambda/020.png)
193+
194+
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.
195+
196+
## References
197+
198+
- https://docs.aws.amazon.com/lambda/latest/dg/concepts-basics.html
199+
- https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam-overview.html

public/articles/6minutes/002.png

446 KB
Loading
86 KB
Loading
243 KB
Loading
282 KB
Loading
209 KB
Loading
418 KB
Loading
32.8 KB
Loading
464 KB
Loading
459 KB
Loading

0 commit comments

Comments
 (0)