Skip to content

Serverless — API Gateway, Step Functions & Cognito — Theory (Bản gốc slide / Original slide)

1. AWS API Gateway — Tổng quan (Overview)

  • AWS Lambda + API Gateway: không có hạ tầng nào phải quản lý
  • Hỗ trợ giao thức WebSocket
  • Quản lý versioning API (v1, v2…)
  • Quản lý nhiều môi trường (dev, test, prod…)
  • Xử lý bảo mật (Authentication và Authorization)
  • Tạo API key, xử lý request throttling
  • Import Swagger/Open API để định nghĩa API nhanh chóng
  • Biến đổi & validate request và response
  • Sinh SDK và API specification
  • Cache API response
  • AWS Lambda + API Gateway: no infrastructure to manage
  • Support for the WebSocket Protocol
  • Handle API versioning (v1, v2…)
  • Handle different environments (dev, test, prod…)
  • Handle security (Authentication and Authorization)
  • Create API keys, handle request throttling
  • Swagger/Open API import to quickly define APIs
  • Transform and validate requests and responses
  • Generate SDK and API specifications
  • Cache API responses

2. API Gateway — Các kiểu tích hợp (Integrations High Level)

  • Lambda Function
    • Invoke Lambda function
    • Cách đơn giản để expose REST API dựa trên AWS Lambda
  • HTTP
    • Expose HTTP endpoint ở backend
    • Ví dụ: HTTP API nội bộ on-premise, Application Load Balancer…
    • Vì sao? Thêm rate limiting, caching, xác thực user, API key…
  • AWS Service
    • Expose bất kỳ AWS API nào thông qua API Gateway
    • Ví dụ: khởi động một AWS Step Function workflow, post message vào SQS
    • Vì sao? Thêm authentication, deploy công khai, kiểm soát rate…
  • Lambda Function
    • Invoke a Lambda function
    • Easy way to expose a REST API backed by AWS Lambda
  • HTTP
    • Expose HTTP endpoints in the backend
    • Example: internal on-premise HTTP API, Application Load Balancer…
    • Why? Add rate limiting, caching, user authentication, API keys…
  • AWS Service
    • Expose any AWS API through API Gateway
    • Example: start an AWS Step Function workflow, post a message to SQS
    • Why? Add authentication, deploy publicly, rate control…

3. API Gateway — Tích hợp AWS Service (Ví dụ Kinesis Data Streams)

ClientAPI GatewayKinesis Data StreamsKinesis Firehoserequestssendrecordsstore .json files → Amazon S3

Ví dụ: Client → API Gateway → Kinesis Data Streams → Kinesis Data Firehose → lưu file .json vào Amazon S3 — API Gateway expose thẳng một AWS Service (Kinesis) mà không cần Lambda ở giữa.

Example: Client → API Gateway → Kinesis Data Streams → Kinesis Data Firehose → store .json files into Amazon S3 — API Gateway exposes an AWS Service (Kinesis) directly, without a Lambda in between.

4. API Gateway — Endpoint Types

  • Edge-Optimized (mặc định): dành cho client toàn cầu
    • Request được định tuyến qua các CloudFront Edge location (cải thiện latency)
    • API Gateway vẫn nằm ở một region duy nhất
  • Regional: dành cho client trong cùng region
    • Có thể tự kết hợp thủ công với CloudFront để kiểm soát caching/distribution tốt hơn
  • Private: chỉ truy cập được từ VPC của bạn qua interface VPC endpoint (ENI)
    • Dùng resource policy để định nghĩa quyền truy cập
  • Edge-Optimized (default): for global clients
    • Requests are routed through CloudFront Edge locations (improves latency)
    • The API Gateway still lives in only one region
  • Regional: for clients within the same region
    • Could manually combine with CloudFront for more control over caching strategies and distribution
  • Private: can only be accessed from your VPC using an interface VPC endpoint (ENI)
    • Use a resource policy to define access

5. API Gateway — Security

Xác thực user thông qua:

  • IAM Roles (hữu ích cho ứng dụng nội bộ)
  • Cognito (danh tính cho user bên ngoài — ví dụ user mobile)
  • Custom Authorizer (logic tự viết riêng)

HTTPS security cho Custom Domain Name thông qua tích hợp với AWS Certificate Manager (ACM):

  • Nếu dùng Edge-Optimized endpoint → certificate phải nằm ở us-east-1
  • Nếu dùng Regional endpoint → certificate phải nằm ở cùng region với API Gateway
  • Phải cấu hình bản ghi CNAME hoặc A-alias trong Route 53

User Authentication through:

  • IAM Roles (useful for internal applications)
  • Cognito (identity for external users — e.g., mobile users)
  • Custom Authorizer (your own logic)

Custom Domain Name HTTPS security through integration with AWS Certificate Manager (ACM):

  • If using an Edge-Optimized endpoint, the certificate must be in us-east-1
  • If using a Regional endpoint, the certificate must be in the API Gateway's region
  • Must set up a CNAME or A-alias record in Route 53

6. AWS Step Functions

  • Xây dựng workflow trực quan, serverless để orchestrate các Lambda function
  • Tính năng: tuần tự (sequence), song song (parallel), điều kiện (conditions), timeout, xử lý lỗi…
  • Có thể tích hợp với EC2, ECS, server on-premises, API Gateway, SQS queue…
  • Có khả năng triển khai tính năng human approval (chờ con người phê duyệt)
  • Use cases: order fulfillment, xử lý dữ liệu, ứng dụng web, bất kỳ workflow nào
  • Build serverless visual workflows to orchestrate your Lambda functions
  • Features: sequence, parallel, conditions, timeouts, error handling…
  • Can integrate with EC2, ECS, on-premises servers, API Gateway, SQS queues, etc.
  • Possibility of implementing a human approval feature
  • Use cases: order fulfillment, data processing, web applications, any workflow

7. Amazon Cognito — Tổng quan (Overview)

  • Cấp cho user một danh tính (identity) để tương tác với ứng dụng web/mobile của bạn
  • Cognito User Pools:
    • Chức năng đăng nhập (sign in) cho user của ứng dụng
    • Tích hợp với API Gateway & Application Load Balancer
  • Cognito Identity Pools (Federated Identity):
    • Cấp AWS credential cho user để họ truy cập trực tiếp tài nguyên AWS
    • Tích hợp với Cognito User Pool như một identity provider
  • Cognito vs. IAM: dùng Cognito khi có "hàng trăm user", "user mobile", cần "xác thực bằng SAML"
  • Give users an identity to interact with your web or mobile application
  • Cognito User Pools:
    • Sign in functionality for app users
    • Integrate with API Gateway & Application Load Balancer
  • Cognito Identity Pools (Federated Identity):
    • Provide AWS credentials to users so they can access AWS resources directly
    • Integrate with Cognito User Pools as an identity provider
  • Cognito vs. IAM: use Cognito for "hundreds of users", "mobile users", "authenticate with SAML"

8. Cognito User Pools (CUP) — Tính năng cho User

  • Tạo một database user serverless cho ứng dụng web & mobile
  • Đăng nhập đơn giản: kết hợp Username (hoặc email) / password
  • Reset password
  • Xác minh Email & Số điện thoại
  • Multi-factor authentication (MFA)
  • Federated Identities: user từ Facebook, Google, SAML…
  • Create a serverless database of users for your web & mobile apps
  • Simple login: Username (or email) / password combination
  • Password reset
  • Email & Phone Number Verification
  • Multi-factor authentication (MFA)
  • Federated Identities: users from Facebook, Google, SAML…

9. Cognito User Pools (CUP) — Tích hợp

CUP tích hợp với API GatewayApplication Load Balancer:

  • Với API Gateway: client authenticate qua Cognito User Pool → lấy token → gửi REST API request kèm token tới API Gateway → API Gateway evaluate Cognito Token → forward tới backend
  • Với Application Load Balancer: client authenticate qua Cognito User Pool → ALB (với Listener & Rule) → forward tới Target GroupBackend

CUP integrates with API Gateway and Application Load Balancer:

  • With API Gateway: client authenticates via Cognito User Pool → retrieves a token → sends a REST API request with the token to API Gateway → API Gateway evaluates the Cognito Token → forwards to the backend
  • With Application Load Balancer: client authenticates via Cognito User Pool → ALB (with Listener & Rules) → forwards to a Target GroupBackend

10. Cognito Identity Pools (Federated Identities)

Web & MobileApplicationsCognito IdentityPoolsSocial Identity ProviderCognito User PoolsPrivate S3 BucketDynamoDB Tableexchange tokenvalidatedirect access
  • Cấp identity cho "user" để họ nhận được AWS credential tạm thời
  • Nguồn user có thể là Cognito User Pools, đăng nhập bên thứ 3…
  • User sau đó có thể truy cập trực tiếp AWS service hoặc qua API Gateway
  • IAM policy áp dụng cho credential được định nghĩa trong Cognito
  • Có thể tuỳ biến theo user_id để kiểm soát chi tiết (fine-grained)
  • IAM Role mặc định cho authenticated userguest user
  • Get identities for "users" so they obtain temporary AWS credentials
  • User source can be Cognito User Pools, 3rd party logins, etc.
  • Users can then access AWS services directly or through API Gateway
  • The IAM policies applied to the credentials are defined in Cognito
  • Can be customized based on user_id for fine-grained control
  • Default IAM roles for authenticated and guest users

11. Cognito Identity Pools — Ứng dụng: Row Level Security trong DynamoDB

Kết hợp Cognito Identity Pools với việc tuỳ biến IAM policy theo user_id cho phép triển khai Row Level Security (bảo mật theo từng dòng) trong DynamoDB — mỗi user chỉ được cấp quyền truy cập vào chính các item/row thuộc về họ, dựa trên user_id gắn trong credential tạm thời mà Cognito Identity Pool cấp.

Combining Cognito Identity Pools with user_id-customized IAM policies enables Row Level Security in DynamoDB — each user is only granted access to the items/rows that belong to them, based on the user_id embedded in the temporary credentials issued by the Cognito Identity Pool.

Personal notes by thanhlt