{"id":4429,"date":"2024-12-14T12:00:14","date_gmt":"2024-12-14T05:00:14","guid":{"rendered":"https:\/\/www.marketenterprise.vn\/blog\/?p=4429"},"modified":"2024-12-14T12:12:53","modified_gmt":"2024-12-14T05:12:53","slug":"toi-uu-hoa-aws-lambda-function","status":"publish","type":"post","link":"https:\/\/www.marketenterprise.vn\/blog\/toi-uu-hoa-aws-lambda-function.html","title":{"rendered":"AWS Lambda: Gi\u1ea3m th\u1eddi gian cold start v\u00e0 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t 1 c\u00e1ch v\u01b0\u1ee3t tr\u1ed9i"},"content":{"rendered":"<h2><\/h2>\n<h2>N\u1ed9i dung ch\u00ednh<\/h2>\n<div class=\"group\/conversation-turn relative flex w-full min-w-0 flex-col agent-turn\">\n<div class=\"flex-col gap-1 md:gap-3\">\n<div class=\"flex max-w-full flex-col flex-grow\">\n<div data-message-author-role=\"assistant\" data-message-id=\"a448968c-4c51-43d1-b2f7-a7f73f407ebb\" dir=\"auto\" class=\"min-h-8 text-message flex w-full flex-col items-end gap-2 whitespace-normal break-words text-start [.text-message+&amp;]:mt-5\" data-message-model-slug=\"gpt-4o\">\n<div class=\"flex w-full flex-col gap-1 empty:hidden first:pt-[3px]\">\n<div class=\"markdown prose w-full break-words dark:prose-invert dark\">\n<ol>\n<li><strong>M\u1edf \u0111\u1ea7u<\/strong><\/li>\n<li><strong>C\u01a1 ch\u1ebf ho\u1ea1t \u0111\u1ed9ng\u00a0<\/strong><br \/>\n2.1. Lifecycle<br \/>\n2.2. C\u00e1c y\u1ebfu t\u1ed1 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn cold start<\/li>\n<li><strong>C\u00e1ch gi\u1ea3m th\u1eddi gian cold start<\/strong><br \/>\n3.1. Ch\u1ecdn runtime ph\u00f9 h\u1ee3p<br \/>\n3.2. T\u0103ng b\u1ed9 nh\u1edb<br \/>\n3.3. S\u1eed d\u1ee5ng Layer \u0111\u1ec3 qu\u1ea3n l\u00fd dependencies<br \/>\n3.4. Pre-warming<br \/>\n3.5. T\u1ed1i \u01b0u h\u00f3a code<br \/>\n3.6. Gi\u1ea3m th\u1eddi gian k\u1ebft n\u1ed1i khi d\u00f9ng VPC<\/li>\n<li><strong>K\u1ebft lu\u1eadn<\/strong><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2><b>1. M\u1edf \u0111\u1ea7u<\/b><\/h2>\n<p><span style=\"font-weight: 400;\"><a href=\"https:\/\/www.google.com\/aclk?sa=l&amp;ai=DChcSEwj56-vm042KAxUSaw8CHcD3B5oYABAAGgJ0Yg&amp;ae=2&amp;aspm=1&amp;co=1&amp;ase=2&amp;gclid=CjwKCAiAmMC6BhA6EiwAdN5iLfH5hFhC0N0tFFYxAx6BygnP8ImboBfrd_jH2ELkHfKuCAikb4LSTRoCvK8QAvD_BwE&amp;sig=AOD64_1eRUb08VClfXIAle__DRTyWT_0mQ&amp;q&amp;nis=4&amp;adurl&amp;ved=2ahUKEwi8yebm042KAxUHm68BHafdNM0Q0Qx6BAgPEAE\" target=\"_blank\" rel=\"noopener\">AWS Lambda<\/a> l\u00e0 m\u1ed9t d\u1ecbch v\u1ee5 serverless \u0111\u01b0\u1ee3c AWS cung c\u1ea5p, cho ph\u00e9p b\u1ea1n ch\u1ea1y function m\u00e0 kh\u00f4ng c\u1ea7n qu\u1ea3n l\u00fd server. V\u1edbi kh\u1ea3 n\u0103ng t\u1ef1 \u0111\u1ed9ng m\u1edf r\u1ed9ng, t\u00ednh to\u00e1n chi ph\u00ed d\u1ef1a tr\u00ean th\u1eddi gian th\u1ef1c thi, v\u00e0 t\u00edch h\u1ee3p s\u00e2u v\u1edbi c\u00e1c d\u1ecbch v\u1ee5 kh\u00e1c c\u1ee7a AWS, Lambda \u0111\u00e3 tr\u1edf th\u00e0nh l\u1ef1a ch\u1ecdn ph\u1ed5 bi\u1ebfn trong vi\u1ec7c x\u00e2y d\u1ef1ng ki\u1ebfn tr\u00fac serverless.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, m\u1eb7c d\u00f9 Lambda mang l\u1ea1i s\u1ef1 linh ho\u1ea1t v\u00e0 t\u1ed1i \u01b0u h\u00f3a chi ph\u00ed, vi\u1ec7c s\u1eed d\u1ee5ng n\u00f3 kh\u00f4ng ph\u1ea3i l\u00fac n\u00e0o c\u0169ng ho\u00e0n h\u1ea3o. M\u1ed9t v\u1ea5n \u0111\u1ec1 th\u01b0\u1eddng g\u1eb7p v\u1edbi AWS Lambda l\u00e0 <\/span><a href=\"https:\/\/lumigo.io\/blog\/this-is-all-you-need-to-know-about-lambda-cold-starts\/#:~:text=A%20%E2%80%9Ccold%20start%E2%80%9D%20is%20the,initialize%20the%20execution%20environment\" target=\"_blank\" rel=\"noopener\"><i><span style=\"font-weight: 400;\">cold start<\/span><\/i><\/a><span style=\"font-weight: 400;\">. \u0110\u00e2y l\u00e0 hi\u1ec7n t\u01b0\u1ee3ng x\u1ea3y ra khi Lambda c\u1ea7n th\u1eddi gian \u0111\u1ec3 kh\u1edfi t\u1ea1o m\u00f4i tr\u01b0\u1eddng th\u1ef1c thi l\u1ea7n \u0111\u1ea7u ti\u00ean(init time) ho\u1eb7c sau m\u1ed9t kho\u1ea3ng th\u1eddi gian kh\u00f4ng ho\u1ea1t \u0111\u1ed9ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Cold start c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn th\u1eddi gian ph\u1ea3n h\u1ed3i l\u00e2u h\u01a1n, \u0111\u1eb7c bi\u1ec7t l\u00e0 trong c\u00e1c \u1ee9ng d\u1ee5ng y\u00eau c\u1ea7u \u0111\u1ed9 tr\u1ec5 th\u1ea5p, g\u00e2y \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng v\u00e0 hi\u1ec7u su\u1ea5t t\u1ed5ng th\u1ec3. V\u00ec v\u1eady, vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a Lambda function \u0111\u1ec3 gi\u1ea3m th\u1eddi gian cold start kh\u00f4ng ch\u1ec9 gi\u00fap t\u0103ng hi\u1ec7u su\u1ea5t m\u00e0 c\u00f2n gi\u1ea3m chi ph\u00ed v\u1eadn h\u00e0nh v\u00e0 mang l\u1ea1i tr\u1ea3i nghi\u1ec7m t\u1ed1t h\u01a1n cho ng\u01b0\u1eddi d\u00f9ng cu\u1ed1i.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u00e0i vi\u1ebft n\u00e0y s\u1ebd h\u01b0\u1edbng d\u1eabn b\u1ea1n c\u00e1ch hi\u1ec3u v\u00e0 t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u qu\u1ea3 AWS Lambda function, gi\u00fap v\u01b0\u1ee3t qua c\u00e1c th\u00e1ch th\u1ee9c li\u00ean quan \u0111\u1ebfn cold start v\u00e0 t\u1eadn d\u1ee5ng t\u1ed1i \u0111a ti\u1ec1m n\u0103ng c\u1ee7a ki\u1ebfn tr\u00fac serverless.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><b>2. C\u01a1 ch\u1ebf ho\u1ea1t \u0111\u1ed9ng c\u1ee7a Lambda<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u1ed1i \u01b0u h\u00f3a AWS Lambda, b\u1ea1n c\u1ea7n hi\u1ec3u r\u00f5 c\u00e1ch th\u1ee9c ho\u1ea1t \u0111\u1ed9ng c\u1ee7a n\u00f3, \u0111\u1eb7c bi\u1ec7t l\u00e0 v\u00f2ng \u0111\u1eddi c\u1ee7a Lambda function v\u00e0 c\u00e1c y\u1ebfu t\u1ed1 g\u00e2y ra hi\u1ec7n t\u01b0\u1ee3ng <\/span><i><span style=\"font-weight: 400;\">cold start<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><b>2.1 Lifecycle c\u1ee7a Lambda function<\/b><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04144437\/lambda-lifecycle.png\" alt=\"lambda-lifecycle\" width=\"1001\" height=\"460\" class=\"wp-image-4432 size-full aligncenter\" srcset=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04144437\/lambda-lifecycle.png 1001w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04144437\/lambda-lifecycle-300x138.png 300w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04144437\/lambda-lifecycle-768x353.png 768w\" sizes=\"auto, (max-width: 1001px) 100vw, 1001px\" \/><\/p>\n<p style=\"text-align: center;\"><i><span style=\"font-weight: 400;\">CloudWatch log ghi l\u1ea1i v\u00f2ng \u0111\u1eddi c\u1ee7a m\u1ed9t Lambda function<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Lambda function c\u00f3 hai giai \u0111o\u1ea1n ch\u00ednh trong v\u00f2ng \u0111\u1eddi:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Giai \u0111o\u1ea1n kh\u1edfi t\u1ea1o (Init phase)<\/b><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Khi Lambda \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t l\u1ea7n \u0111\u1ea7u ti\u00ean ho\u1eb7c sau m\u1ed9t kho\u1ea3ng th\u1eddi gian kh\u00f4ng ho\u1ea1t \u0111\u1ed9ng, AWS s\u1ebd kh\u1edfi t\u1ea1o m\u1ed9t container \u0111\u1ec3 ch\u1ea1y function c\u1ee7a b\u1ea1n.<\/span><span style=\"font-weight: 400;\">Trong giai \u0111o\u1ea1n n\u00e0y, h\u1ec7 th\u1ed1ng th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5: t\u1ea3i code, c\u00e0i \u0111\u1eb7t m\u00f4i tr\u01b0\u1eddng runtime (Node.js, Python, Java, v.v.), v\u00e0 th\u1ef1c thi c\u00e1c \u0111o\u1ea1n code kh\u1edfi t\u1ea1o (nh\u01b0 import th\u01b0 vi\u1ec7n ho\u1eb7c thi\u1ebft l\u1eadp bi\u1ebfn m\u00f4i tr\u01b0\u1eddng). <\/span><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 giai \u0111o\u1ea1n m\u00e0 <\/span><i><span style=\"font-weight: 400;\">cold start<\/span><\/i><span style=\"font-weight: 400;\"> x\u1ea3y ra, khi\u1ebfn th\u1eddi gian ph\u1ea3n h\u1ed3i d\u00e0i h\u01a1n.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Giai \u0111o\u1ea1n x\u1eed l\u00fd y\u00eau c\u1ea7u (Invoke phase)<\/b><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Sau khi container \u0111\u00e3 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o, Lambda function s\u1eb5n s\u00e0ng x\u1eed l\u00fd c\u00e1c request. <\/span><span style=\"font-weight: 400;\">\u1ede giai \u0111o\u1ea1n n\u00e0y, kh\u00f4ng c\u00f3 cold start v\u00ec container \u0111\u00e3 \u0111\u01b0\u1ee3c kh\u1edfi \u0111\u1ed9ng s\u1eb5n. Y\u00eau c\u1ea7u s\u1ebd \u0111\u01b0\u1ee3c x\u1eed l\u00fd g\u1ea7n nh\u01b0 ngay l\u1eadp t\u1ee9c, ch\u1ec9 t\u1ed1n th\u1eddi gian th\u1ef1c thi code.<\/span><\/p>\n<h3><b>2.2 C\u00e1c y\u1ebfu t\u1ed1 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn cold start<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Cold start b\u1ecb t\u00e1c \u0111\u1ed9ng b\u1edfi nhi\u1ec1u y\u1ebfu t\u1ed1 li\u00ean quan \u0111\u1ebfn c\u1ea5u h\u00ecnh v\u00e0 c\u00e1ch x\u00e2y d\u1ef1ng Lambda function:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Lo\u1ea1i runtime: <\/b><span style=\"font-weight: 400;\">M\u1ed7i runtime c\u00f3 th\u1eddi gian kh\u1edfi \u0111\u1ed9ng kh\u00e1c nhau VD: Node.js v\u00e0 Python c\u00f3 th\u1eddi gian kh\u1edfi \u0111\u1ed9ng ng\u1eafn h\u01a1n so v\u1edbi Java ho\u1eb7c .NET, do c\u00e1c runtime n\u00e0y y\u00eau c\u1ea7u \u00edt t\u00e0i nguy\u00ean h\u01a1n \u0111\u1ec3 kh\u1edfi t\u1ea1o.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u00edch th\u01b0\u1edbc b\u1ed9 nh\u1edb \u0111\u01b0\u1ee3c c\u1ea5u h\u00ecnh: <\/b><span style=\"font-weight: 400;\">B\u1ed9 nh\u1edb th\u1ea5p th\u01b0\u1eddng \u0111i k\u00e8m v\u1edbi \u00edt t\u00e0i nguy\u00ean t\u00ednh to\u00e1n h\u01a1n (CPU, I\/O), d\u1eabn \u0111\u1ebfn th\u1eddi gian kh\u1edfi t\u1ea1o l\u00e2u h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Codebase l\u1edbn ho\u1eb7c nhi\u1ec1u dependencies: <\/b><span style=\"font-weight: 400;\">Khi Lambda function ch\u1ee9a nhi\u1ec1u th\u01b0 vi\u1ec7n ho\u1eb7c m\u00e3 ngu\u1ed3n l\u1edbn, th\u1eddi gian t\u1ea3i m\u00e3 s\u1ebd t\u0103ng l\u00ean \u0111\u00e1ng k\u1ec3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng VPC (Virtual Private Cloud): <\/b><span style=\"font-weight: 400;\">N\u1ebfu Lambda function c\u1ee7a b\u1ea1n n\u1eb1m trong m\u1ed9t VPC, h\u1ec7 th\u1ed1ng c\u1ea7n thi\u1ebft l\u1eadp k\u1ebft n\u1ed1i m\u1ea1ng, nh\u01b0 g\u00e1n \u0111\u1ecba ch\u1ec9 IP v\u00e0 t\u1ea1o li\u00ean k\u1ebft v\u1edbi c\u00e1c subnet. <\/span><span style=\"font-weight: 400;\">Qu\u00e1 tr\u00ecnh n\u00e0y c\u00f3 th\u1ec3 m\u1ea5t v\u00e0i gi\u00e2y, t\u00f9y thu\u1ed9c v\u00e0o c\u1ea5u h\u00ecnh m\u1ea1ng v\u00e0 s\u1ed1 l\u01b0\u1ee3ng Lambda function \u0111ang ho\u1ea1t \u0111\u1ed9ng trong c\u00f9ng m\u1ed9t VPC.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Hi\u1ec3u r\u00f5 c\u00e1c y\u1ebfu t\u1ed1 n\u00e0y s\u1ebd gi\u00fap b\u1ea1n nh\u1eadn di\u1ec7n \u0111\u01b0\u1ee3c nguy\u00ean nh\u00e2n ch\u00ednh g\u00e2y ra cold start trong \u1ee9ng d\u1ee5ng c\u1ee7a m\u00ecnh, t\u1eeb \u0111\u00f3 tri\u1ec3n khai c\u00e1c chi\u1ebfn l\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<h2><b>3. C\u00e1ch gi\u1ea3m th\u1eddi gian cold start<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Cold start l\u00e0 m\u1ed9t th\u00e1ch th\u1ee9c l\u1edbn v\u1edbi AWS Lambda, nh\u01b0ng c\u00f3 nhi\u1ec1u c\u00e1ch \u0111\u1ec3 gi\u1ea3m thi\u1ec3u th\u1eddi gian n\u00e0y. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c ph\u01b0\u01a1ng ph\u00e1p hi\u1ec7u qu\u1ea3 gi\u00fap b\u1ea1n t\u1ed1i \u01b0u h\u00f3a Lambda function v\u00e0 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t.<\/span><\/p>\n<h3><b>3.1. Ch\u1ecdn runtime ph\u00f9 h\u1ee3p<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Runtime \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong th\u1eddi gian kh\u1edfi t\u1ea1o c\u1ee7a Lambda function:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>So s\u00e1nh th\u1eddi gian cold start<\/b><span style=\"font-weight: 400;\">: <\/span><b>Node.js<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Python<\/b><span style=\"font-weight: 400;\"> c\u00f3 th\u1eddi gian kh\u1edfi \u0111\u1ed9ng nhanh nh\u1ea5t, th\u01b0\u1eddng ch\u1ec9 m\u1ea5t v\u00e0i tr\u0103m mili gi\u00e2y trong khi \u0111\u00f3\u00a0<\/span><b>Java<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>.NET Core<\/b><span style=\"font-weight: 400;\"> y\u00eau c\u1ea7u th\u1eddi gian kh\u1edfi t\u1ea1o d\u00e0i h\u01a1n v\u00ec c\u1ea7n t\u1ea3i v\u00e0 bi\u00ean d\u1ecbch code ho\u1eb7c th\u1ef1c thi nhi\u1ec1u t\u00e1c v\u1ee5 kh\u1edfi t\u1ea1o ph\u1ee9c t\u1ea1p h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Khi n\u00e0o n\u00ean ch\u1ecdn runtime nh\u1eb9? <\/b><span style=\"font-weight: 400;\">V\u1edbi c\u00e1c \u1ee9ng d\u1ee5ng y\u00eau c\u1ea7u \u0111\u1ed9 tr\u1ec5 th\u1ea5p, nh\u01b0 API ho\u1eb7c d\u1ecbch v\u1ee5 th\u1eddi gian th\u1ef1c, h\u00e3y \u01b0u ti\u00ean s\u1eed d\u1ee5ng Node.js ho\u1eb7c Python. <\/span><span style=\"font-weight: 400;\">N\u1ebfu \u1ee9ng d\u1ee5ng c\u1ea7n x\u1eed l\u00fd ph\u1ee9c t\u1ea1p ho\u1eb7c y\u00eau c\u1ea7u \u0111\u1eb7c th\u00f9 t\u1eeb ng\u00f4n ng\u1eef nh\u01b0 Java, h\u00e3y xem x\u00e9t t\u1ed1i \u01b0u h\u00f3a runtime tr\u01b0\u1edbc khi deploy.<\/span><\/li>\n<\/ul>\n<h3><b>3.2. T\u0103ng b\u1ed9 nh\u1edb cho Lambda<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c t\u0103ng b\u1ed9 nh\u1edb kh\u00f4ng ch\u1ec9 \u0111\u01a1n gi\u1ea3n l\u00e0 c\u1ea3i thi\u1ec7n kh\u00f4ng gian l\u01b0u tr\u1eef m\u00e0 c\u00f2n gi\u00fap t\u0103ng t\u00e0i nguy\u00ean CPU:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00e1ch t\u0103ng b\u1ed9 nh\u1edb gi\u1ea3m th\u1eddi gian kh\u1edfi t\u1ea1o<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">AWS cung c\u1ea5p t\u1eeb 128 MB \u0111\u1ebfn 10,240 MB b\u1ed9 nh\u1edb cho Lambda. <\/span><span style=\"font-weight: 400;\">B\u1ed9 nh\u1edb cao h\u01a1n t\u01b0\u01a1ng \u1ee9ng v\u1edbi vCPU m\u1ea1nh h\u01a1n, gi\u00fap gi\u1ea3m th\u1eddi gian kh\u1edfi t\u1ea1o container v\u00e0 th\u1ef1c thi m\u00e3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>M\u1ed1i quan h\u1ec7 gi\u1eefa b\u1ed9 nh\u1edb, vCPU v\u00e0 hi\u1ec7u su\u1ea5t<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">V\u1edbi b\u1ed9 nh\u1edb l\u1edbn h\u01a1n, b\u1ea1n c\u00f3 th\u1ec3 th\u1ea5y th\u1eddi gian th\u1ef1c thi v\u00e0 kh\u1edfi t\u1ea1o gi\u1ea3m \u0111\u00e1ng k\u1ec3. <\/span><span style=\"font-weight: 400;\">Tuy nhi\u00ean, c\u1ea7n ki\u1ec3m tra chi ph\u00ed, v\u00ec b\u1ed9 nh\u1edb cao h\u01a1n s\u1ebd t\u0103ng gi\u00e1 m\u1ed7i l\u1ea7n th\u1ef1c thi.<\/span><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145642\/Screenshot-2024-12-04-at-16.16.43.png\" alt=\"lambda-config\" width=\"574\" height=\"405\" class=\"aligncenter wp-image-4433 size-full\" srcset=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145642\/Screenshot-2024-12-04-at-16.16.43.png 574w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145642\/Screenshot-2024-12-04-at-16.16.43-300x212.png 300w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/p>\n<p style=\"text-align: center;\"><i><span style=\"font-weight: 400;\">M\u00e0n h\u00ecnh console \u0111i\u1ec1u ch\u1ec9nh c\u1ea5u h\u00ecnh Lambda<\/span><\/i><\/p>\n<h3><b>3.3. S\u1eed d\u1ee5ng Layer \u0111\u1ec3 qu\u1ea3n l\u00fd dependencies<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">AWS Lambda Layers gi\u00fap qu\u1ea3n l\u00fd dependencies d\u1ec5 d\u00e0ng h\u01a1n b\u1eb1ng c\u00e1ch t\u00e1ch c\u00e1c th\u01b0 vi\u1ec7n chung ho\u1eb7c t\u00e0i nguy\u00ean l\u1edbn, b\u1ea1n c\u00f3 th\u1ec3 gi\u1ea3m k\u00edch th\u01b0\u1edbc c\u1ee7a source code l\u00e0m cho th\u1eddi gian t\u1ea3i nhanh h\u01a1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: \u0110\u01b0a th\u01b0 vi\u1ec7n <\/span><span style=\"font-weight: 400;\">pandas<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">numpy<\/span><span style=\"font-weight: 400;\"> v\u00e0o Layer thay v\u00ec \u0111\u00f3ng g\u00f3i trong source code.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145844\/Screenshot-2024-12-04-at-16.20.47.png\" alt=\"lambda-layer\" width=\"1354\" height=\"533\" class=\"aligncenter wp-image-4434 size-full\" srcset=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145844\/Screenshot-2024-12-04-at-16.20.47.png 1354w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145844\/Screenshot-2024-12-04-at-16.20.47-300x118.png 300w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145844\/Screenshot-2024-12-04-at-16.20.47-1024x403.png 1024w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/12\/04145844\/Screenshot-2024-12-04-at-16.20.47-768x302.png 768w\" sizes=\"auto, (max-width: 1354px) 100vw, 1354px\" \/><\/p>\n<p style=\"text-align: center;\"><i><span style=\"font-weight: 400;\">Th\u00eam layer cho Lambda<\/span><\/i><\/p>\n<h3><b>3.4. Pre-warming Lambda<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Pre-warming gi\u00fap gi\u1ea3m cold start b\u1eb1ng c\u00e1ch gi\u1eef Lambda function lu\u00f4n \u1edf tr\u1ea1ng th\u00e1i &#8220;\u1ea5m&#8221;:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u1ea1o warm-up requests \u0111\u1ecbnh k\u1ef3<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">G\u1eedi c\u00e1c y\u00eau c\u1ea7u \u0111\u1ecbnh k\u1ef3 \u0111\u1ebfn Lambda \u0111\u1ec3 gi\u1eef container lu\u00f4n s\u1eb5n s\u00e0ng. <\/span><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y \u0111\u1eb7c bi\u1ec7t hi\u1ec7u qu\u1ea3 v\u1edbi c\u00e1c \u1ee9ng d\u1ee5ng c\u00f3 l\u01b0u l\u01b0\u1ee3ng truy c\u1eadp kh\u00f4ng li\u00ean t\u1ee5c.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00f4ng c\u1ee5 t\u1ef1 \u0111\u1ed9ng h\u00f3a warm-up<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><b>AWS CloudWatch Events<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 thi\u1ebft l\u1eadp l\u1ecbch tr\u00ecnh k\u00edch ho\u1ea1t Lambda. <\/span><span style=\"font-weight: 400;\">T\u00edch h\u1ee3p v\u1edbi <\/span><b>AWS Step Functions<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 ki\u1ec3m so\u00e1t quy tr\u00ecnh pre-warming ph\u1ee9c t\u1ea1p h\u01a1n.<\/span><\/li>\n<\/ul>\n<h3><b>3.5. T\u1ed1i \u01b0u h\u00f3a code<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c gi\u1ea3m thi\u1ec3u th\u1eddi gian kh\u1edfi t\u1ea1o ph\u1ee5 thu\u1ed9c r\u1ea5t nhi\u1ec1u v\u00e0o c\u00e1ch b\u1ea1n t\u1ed5 ch\u1ee9c v\u00e0 vi\u1ebft source code:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Lo\u1ea1i b\u1ecf \u0111o\u1ea1n code kh\u00f4ng c\u1ea7n thi\u1ebft<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">Gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng import trong h\u00e0m Lambda. Ch\u1ec9 s\u1eed d\u1ee5ng c\u00e1c module th\u1ef1c s\u1ef1 c\u1ea7n thi\u1ebft.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 clean code<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">Trong Node.js, tree-shaking gi\u00fap lo\u1ea1i b\u1ecf m\u00e3 kh\u00f4ng s\u1eed d\u1ee5ng.<\/span><\/li>\n<\/ul>\n<h3><b>3.6. Gi\u1ea3m th\u1eddi gian k\u1ebft n\u1ed1i khi d\u00f9ng VPC<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">N\u1ebfu Lambda function c\u1ee7a b\u1ea1n \u0111\u01b0\u1ee3c deploy trong m\u1ed9t VPC, th\u1eddi gian thi\u1ebft l\u1eadp k\u1ebft n\u1ed1i c\u00f3 th\u1ec3 l\u00e0m t\u0103ng \u0111\u1ed9 tr\u1ec5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng VPC endpoint<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">Tri\u1ec3n khai endpoint trong VPC \u0111\u1ec3 gi\u1ea3m th\u1eddi gian k\u1ebft n\u1ed1i v\u1edbi c\u00e1c d\u1ecbch v\u1ee5 AWS kh\u00e1c nh\u01b0 S3 ho\u1eb7c DynamoDB. <\/span><span style=\"font-weight: 400;\">Endpoint gi\u00fap b\u1ecf qua b\u01b0\u1edbc k\u1ebft n\u1ed1i qua internet c\u00f4ng c\u1ed9ng, ti\u1ebft ki\u1ec7m th\u1eddi gian.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Khi n\u00e0o n\u00ean v\u00e0 kh\u00f4ng n\u00ean s\u1eed d\u1ee5ng VPC? <\/b><span style=\"font-weight: 400;\">Khi b\u1ea1n c\u1ea7n k\u1ebft n\u1ed1i v\u1edbi t\u00e0i nguy\u00ean trong VPC (nh\u01b0 RDS ho\u1eb7c t\u00e0i nguy\u00ean EC2). <\/span><span style=\"font-weight: 400;\">N\u1ebfu Lambda kh\u00f4ng c\u1ea7n truy c\u1eadp t\u00e0i nguy\u00ean n\u1ed9i b\u1ed9. S\u1eed d\u1ee5ng tr\u1ef1c ti\u1ebfp c\u00e1c d\u1ecbch v\u1ee5 AWS \u0111\u1ec3 gi\u1ea3m \u0111\u1ed9 ph\u1ee9c t\u1ea1p v\u00e0 th\u1eddi gian kh\u1edfi t\u1ea1o.<\/span><\/li>\n<\/ul>\n<h2><b>4. K\u1ebft lu\u1eadn<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">T\u1ed1i \u01b0u h\u00f3a AWS Lambda kh\u00f4ng ch\u1ec9 mang l\u1ea1i hi\u1ec7u su\u1ea5t cao h\u01a1n m\u00e0 c\u00f2n gi\u00fap gi\u1ea3m chi ph\u00ed v\u1eadn h\u00e0nh v\u00e0 c\u1ea3i thi\u1ec7n \u0111\u00e1ng k\u1ec3 tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng. B\u1eb1ng c\u00e1ch gi\u1ea3m th\u1eddi gian cold start v\u00e0 t\u1ed1i \u01b0u h\u00f3a code, b\u1ea1n c\u00f3 th\u1ec3 t\u1eadn d\u1ee5ng to\u00e0n b\u1ed9 ti\u1ec1m n\u0103ng c\u1ee7a ki\u1ebfn tr\u00fac serverless, t\u1eeb \u0111\u00f3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng nhanh h\u01a1n, m\u1ea1nh m\u1ebd h\u01a1n v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong t\u01b0\u01a1ng lai, b\u1ea1n c\u00f3 th\u1ec3 <a href=\"https:\/\/www.marketenterprise.vn\/blog\/aws-lambda-micro-framework-chalice.html\">t\u00edch h\u1ee3p Lambda v\u1edbi c\u00e1c d\u1ecbch v\u1ee5 AWS kh\u00e1c<\/a> nh\u01b0 API Gateway, DynamoDB, v\u00e0 Step Functions \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c h\u1ec7 th\u1ed1ng serverless to\u00e0n di\u1ec7n. Ngo\u00e0i ra, h\u00e3y theo d\u00f5i v\u00e0 th\u1eed nghi\u1ec7m c\u00e1c c\u00f4ng ngh\u1ec7 m\u1edbi t\u1eeb AWS, ch\u1eb3ng h\u1ea1n nh\u01b0 t\u00ednh n\u0103ng t\u0103ng c\u01b0\u1eddng hi\u1ec7u su\u1ea5t ho\u1eb7c c\u00f4ng c\u1ee5 t\u1ef1 \u0111\u1ed9ng h\u00f3a quy tr\u00ecnh t\u1ed1i \u01b0u h\u00f3a.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u00e2y gi\u1edd l\u00e0 l\u00fac \u0111\u1ec3 b\u1ea1n h\u00e0nh \u0111\u1ed9ng! H\u00e3y \u00e1p d\u1ee5ng c\u00e1c m\u1eb9o v\u00e0 ph\u01b0\u01a1ng ph\u00e1p trong b\u00e0i vi\u1ebft v\u00e0o d\u1ef1 \u00e1n c\u1ee7a m\u00ecnh v\u00e0 theo d\u00f5i s\u1ef1 thay \u0111\u1ed5i v\u1ec1 hi\u1ec7u su\u1ea5t. N\u1ebfu b\u1ea1n mu\u1ed1n t\u00ecm hi\u1ec3u s\u00e2u h\u01a1n, AWS Documentation v\u00e0 c\u00e1c b\u00e0i vi\u1ebft t\u1eeb c\u1ed9ng \u0111\u1ed3ng serverless l\u00e0 nh\u1eefng t\u00e0i li\u1ec7u tuy\u1ec7t v\u1eddi \u0111\u1ec3 b\u1eaft \u0111\u1ea7u.<\/span><\/p>\n<p><b>B\u1ea1n \u0111\u00e3 s\u1eb5n s\u00e0ng n\u00e2ng c\u1ea5p Lambda function c\u1ee7a m\u00ecnh ch\u01b0a? H\u00e3y th\u1eed ngay v\u00e0 chia s\u1ebb kinh nghi\u1ec7m c\u1ee7a b\u1ea1n!<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>N\u1ed9i dung ch\u00ednh M\u1edf \u0111\u1ea7u C\u01a1 ch\u1ebf ho\u1ea1t \u0111\u1ed9ng\u00a0 2.1. Lifecycle 2.2. C\u00e1c y\u1ebfu t\u1ed1 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn cold start C\u00e1ch gi\u1ea3m th\u1eddi gian cold start 3.1. Ch\u1ecdn runtime ph\u00f9 h\u1ee3p 3.2. T\u0103ng b\u1ed9 nh\u1edb 3.3. S\u1eed d\u1ee5ng Layer \u0111\u1ec3 qu\u1ea3n l\u00fd dependencies 3.4. Pre-warming 3.5. T\u1ed1i \u01b0u h\u00f3a code 3.6. Gi\u1ea3m th\u1eddi gian k\u1ebft [&hellip;]<\/p>\n","protected":false},"author":49,"featured_media":4464,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[23,22],"class_list":["post-4429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-lambda","tag-technical"],"_links":{"self":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/4429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/users\/49"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/comments?post=4429"}],"version-history":[{"count":0,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/4429\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media\/4464"}],"wp:attachment":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media?parent=4429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/categories?post=4429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/tags?post=4429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}