Vận hành Lambda function trong môi trường production
Một function chạy tốt khi thử nghiệm vẫn có thể gặp vấn đề trong môi trường production: kết nối mạng khi gắn VPC, cold start, concurrency quota và cách lưu thông tin xác thực (credentials).
Một số vấn đề chỉ xuất hiện khi triển khai lên production: function gắn vào VPC không truy cập được Internet, độ trễ tăng cao vào giờ cao điểm do cold start, thông báo bị throttle khi vượt concurrency quota, mật khẩu database lưu trong cấu hình. Chọn từng tình huống, chạy thử, sau đó bật tuỳ chọn để khắc phục:
Chọn một tình huống vận hành Lambda của MFTECH và chạy thử; sau đó bật tuỳ chọn khắc phục và chạy lại.
Hai lưu ý bổ sung. Mỗi lần thực thi Lambda function tiêu chuẩn tối đa 15 phút (900 giây; Lambda Managed Instances cho phép tới 90 phút với lời gọi bất đồng bộ hoặc qua event source mapping, trừ Amazon MQ và Amazon DocumentDB); job dài hơn nên chạy bằng container trên Fargate (bài Containers). Khi chưa gắn vào VPC, Lambda function vẫn gọi được Internet và các AWS API công khai; chỉ gắn VPC khi cần truy cập tài nguyên private như RDS, và khi đó cần NAT gateway như mô phỏng trên. Cách định cỡ memory, /tmp, timeout và đóng gói (layer, container image) được trình bày ở phần 3.
- Lambda trong VPC cần NAT gateway để truy cập Internet.
- Cold start vào giờ cao điểm: provisioned concurrency.
- Concurrency có quota theo account và Region; cấu hình alarm trên ConcurrentExecutions.
- Không lưu mật khẩu: IAM database authentication.
- Truy cập cross-account: cả execution role và bucket policy đều phải cho phép.
Running Lambda functions in production
A function that works in testing can still run into problems in production: networking after VPC attachment, cold starts, concurrency quotas and how credentials are stored.
Some problems appear only after deploying to production: a function attached to a VPC cannot reach the internet, latency rises at peak hours because of cold starts, notifications are throttled when the concurrency quota is exceeded, the database password is stored in configuration. Pick each scenario, try it, then turn on the option to fix it:
Chọn một tình huống vận hành Lambda của MFTECH và chạy thử; sau đó bật tuỳ chọn khắc phục và chạy lại.
Two more notes. Each invocation of a standard Lambda function runs at most 15 minutes (900 seconds; Lambda Managed Instances allows up to 90 minutes for asynchronous invocations or through an event source mapping, except Amazon MQ and Amazon DocumentDB); longer jobs should run as containers on Fargate (Containers lesson). A Lambda function that is not attached to a VPC can already reach the internet and public AWS APIs; attach it to a VPC only when it needs private resources such as RDS, and then it needs a NAT gateway as in the simulation above. Sizing memory, /tmp and the timeout, and packaging (layers, container images), are covered in part 3.
- Lambda in a VPC needs a NAT gateway to reach the internet.
- Cold starts at peak hours: provisioned concurrency.
- Concurrency has a quota per account and Region; configure an alarm on ConcurrentExecutions.
- Do not store passwords: IAM database authentication.
- Cross-account access: both the execution role and the bucket policy must allow it.