Cấu hình memory, ephemeral storage, timeout và đóng gói function
Mỗi function được cấp một lượng tài nguyên cố định cho mỗi lần thực thi; cấu hình tài nguyên phù hợp ảnh hưởng trực tiếp tới hiệu năng và chi phí.
Mỗi Lambda function có một số thiết lập quyết định tài nguyên của execution environment:
- Memory từ 128 MB (mặc định) tới 10.240 MB. Lambda không có thiết lập CPU riêng: CPU được cấp tỷ lệ với memory, tại 1.769 MB function có tương đương một vCPU. Tác vụ nặng về CPU chạy chậm thì tăng memory. Chi phí duration tính theo GB-giây nên phụ thuộc memory đã cấp.
- Ephemeral storage (
/tmp) từ 512 MB (mặc định) tới 10.240 MB, mã hoá khi lưu trữ bằng key do AWS quản lý. Dữ liệu trong/tmpriêng cho từng execution environment và chỉ dùng làm cache tạm thời: có thể còn lại khi môi trường được tái sử dụng, nhưng Lambda thu hồi môi trường sau vài giờ. Dữ liệu cần giữ lâu dài hoặc dùng chung giữa các function đặt trên Amazon S3 hoặc Amazon EFS. - Timeout mặc định 3 giây, tối đa 900 giây. Timeout quá sát thời gian chạy dễ gây lỗi khi dữ liệu lớn hơn bình thường; timeout quá cao làm lời gọi bị treo giữ môi trường thực thi lâu hơn cần thiết. Giá trị phù hợp được xác định bằng load test.
- Kiểu đóng gói: tệp .zip (tối đa 250 MB khi giải nén, kể cả layer) hoặc container image lưu trong Amazon ECR cùng Region (tối đa 10 GB). Lambda layer chứa thư viện, custom runtime hoặc tệp cấu hình dùng chung cho nhiều function; mỗi function dùng tối đa 5 layer, mỗi layer version là bản chụp bất biến, và layer chỉ áp dụng cho function dạng .zip.
Thời gian khởi tạo (cold start) phụ thuộc kích thước package, gồm cả thư viện, dependency và layer. Đưa thư viện vào layer giúp package gọn và dùng chung được, nhưng không loại bỏ thời gian khởi tạo thư viện đó; cách giảm cold start là chỉ import những gì function cần, hoặc dùng provisioned concurrency (phần 2).
Chọn từng tác vụ của MFTECH, điều chỉnh cấu hình rồi triển khai thử:
Ghép ảnh banner khuyến mãi, tác vụ nặng về CPU. Load test cho thấy cần tối thiểu 1 vCPU và chạy khoảng 8 giây.
Vạch dọc: mức tác vụ cần. Thang đo theo giới hạn tối đa của Lambda.
Chọn tác vụ, điều chỉnh cấu hình function, sau đó triển khai và chạy thử. Cấu hình ban đầu là mặc định của Lambda: 128 MB memory, 512 MB /tmp, timeout 3 giây.
Layer không thay thế được nơi lưu dữ liệu. Khi team gợi ý sản phẩm cần đọc bộ dữ liệu lớn, liên tục tăng trên EFS thuộc account đối tác, Lambda mount trực tiếp EFS đó qua access point, kèm file system policy cho phép cross-account và kết nối VPC peering: xem bài Storage, phần 3. Đóng gói dữ liệu vào layer không phù hợp vì layer version bất biến và bị giới hạn 250 MB chung với package.
- CPU tỷ lệ với memory (1.769 MB ≈ 1 vCPU).
/tmptừ 512 MB tới 10.240 MB, tạm thời, riêng cho từng execution environment; dữ liệu bền vững dùng S3 hoặc EFS.- Timeout đặt theo load test, không đặt dư.
- Gói .zip tối đa 250 MB giải nén kể cả layer; lớn hơn dùng container image (tối đa 10 GB).
- Layer cho thư viện dùng chung, không cho dữ liệu.
Configuring memory, ephemeral storage, timeout and function packaging
Each function gets a fixed amount of resources for every invocation; configuring those resources correctly directly affects performance and cost.
A few settings of each Lambda function decide the resources of its execution environment:
- Memory from 128 MB (the default) to 10,240 MB. Lambda has no separate CPU setting: CPU is allocated in proportion to memory, and at 1,769 MB a function has the equivalent of one vCPU. A CPU-bound task that runs slowly needs more memory. Duration is billed in GB-seconds, so its cost depends on the memory allocated.
- Ephemeral storage (
/tmp) from 512 MB (the default) to 10,240 MB, encrypted at rest with an AWS managed key. Data in/tmpis unique to each execution environment and is only a transient cache: it can remain when the environment is reused, but Lambda terminates environments every few hours. Data that must last or be shared between functions belongs in Amazon S3 or Amazon EFS. - Timeout, 3 seconds by default and at most 900 seconds. A timeout close to the run time fails when the data is larger than usual; a timeout set far too high lets a hung invocation hold its execution environment longer than needed. Load testing finds the right value.
- Package type: a .zip file archive (at most 250 MB unzipped, including layers) or a container image stored in Amazon ECR in the same Region (up to 10 GB). A Lambda layer holds libraries, a custom runtime or configuration files shared by several functions; a function uses up to 5 layers, each layer version is an immutable snapshot, and layers apply only to .zip functions.
Initialization time (the cold start) depends on the package size, including libraries, dependencies and layers. Moving a library into a layer keeps the package small and shareable, but does not remove the library’s initialization time; to reduce cold starts, import only what the function needs, or use provisioned concurrency (part 2).
Pick each MFTECH workload, adjust the configuration and deploy it:
Ghép ảnh banner khuyến mãi, tác vụ nặng về CPU. Load test cho thấy cần tối thiểu 1 vCPU và chạy khoảng 8 giây.
Vạch dọc: mức tác vụ cần. Thang đo theo giới hạn tối đa của Lambda.
Chọn tác vụ, điều chỉnh cấu hình function, sau đó triển khai và chạy thử. Cấu hình ban đầu là mặc định của Lambda: 128 MB memory, 512 MB /tmp, timeout 3 giây.
A layer is not a place to keep data. When the recommendations team needs to read a large, growing dataset on EFS in a partner’s account, Lambda mounts that EFS file system directly through an access point, with a file system policy that allows cross-account access and VPC peering for connectivity: see the Storage lesson, part 3. Packaging data into a layer does not fit, because layer versions are immutable and share the 250 MB limit with the package.
- CPU is proportional to memory (1,769 MB ≈ 1 vCPU).
/tmpis 512 MB to 10,240 MB, temporary and unique to each execution environment; durable data goes to S3 or EFS.- Set the timeout from load tests, not with a large margin.
- A .zip package is at most 250 MB unzipped including layers; anything larger uses a container image (up to 10 GB).
- Layers are for shared libraries, not data.