Design Resilient Architectures · Topic 2 of 5

Serverless and Microservices on AWS

Build scalable stateless designs with Lambda, API Gateway, Step Functions, Fargate, ECS and EKS, and learn how to choose between functions and containers.

  • 5 min
In a nutshell
  • Lambda runs for up to 15 minutes, scales automatically and is billed for execution, so it fits short event driven work.
  • Reserved concurrency limits and guarantees capacity, and provisioned concurrency reduces cold starts.
  • API Gateway adds throttling, caching, authorization and stages in front of a back end.

What it is

A microservice design splits an application into small services that each do one job and talk through APIs or messages. Each service can be built, deployed and scaled on its own.

AWS gives two main ways to run the pieces without managing servers by hand. Serverless services, such as AWS Lambda, run code on demand with automatic scaling. Containers package code with its dependencies and run on a managed platform such as Amazon ECS or Amazon EKS.

ClientappHTTPSAPI Gatewayfront doorinvokesLambdabusiness logicreads and writesDynamoDBdata

How it works

AWS Lambda

Lambda runs your function in response to an event, and AWS handles servers, patching and scaling. You choose the memory, and CPU power scales with it. A function has a maximum run time of 15 minutes, so it is not for long running jobs.

Common triggers include API Gateway, S3 events, SQS queues, SNS, EventBridge schedules and DynamoDB streams. For a queue, Lambda polls SQS for you and scales the number of concurrent runs with the backlog.

Two concurrency controls matter. Reserved concurrency guarantees and also caps the concurrency for one function, which can protect a downstream database. Provisioned concurrency keeps execution environments warm to avoid cold start delay for latency sensitive workloads.

Amazon API Gateway

API Gateway is the managed front door for APIs. It handles authorization, request throttling, caching, usage plans, API keys and versions (stages). It offers REST APIs, HTTP APIs (a lighter and cheaper option) and WebSocket APIs for two way connections.

It can call Lambda, other AWS services and HTTP back ends. Authentication can use IAM, a Cognito user pool or a Lambda authorizer.

AWS Step Functions

Step Functions coordinates several steps as a state machine, with retries, error handling, branches, parallel steps and waits. Use it for workflows such as order processing, so the logic does not sit inside one large function. Standard workflows suit long running, auditable flows, and Express workflows suit short, high volume ones.

Containers: ECS, EKS and Fargate

ChoiceWhat it isPick it when
Amazon ECSAWS container orchestratorYou want the simplest AWS native container service
Amazon EKSManaged KubernetesYou already use Kubernetes or need its ecosystem and portability
AWS FargateServerless compute for ECS and EKSYou do not want to manage the instances that run containers
ECS or EKS on EC2Containers on instances you manageYou need control of the host, special instance types or a cost model based on instances
Amazon ECRPrivate container image registryYou need to store and scan images

Fargate removes the work of patching and scaling the container hosts. ECS services can sit behind an Application Load Balancer and scale their task count with Application Auto Scaling.

Functions or containers?

FactorLambdaContainers
Run timeUp to 15 minutesNo fixed limit
ScalingAutomatic per eventScales by task or pod count
ManagementLeastMore, even with Fargate
FitsEvent driven, spiky, short tasksLong running services, existing container images, steady load

Moving an application into containers

The usual path is: package the application and its dependencies into an image, store the image in Amazon ECR, then run it on ECS or EKS, with Fargate if you do not want to manage hosts. You describe the image in a Dockerfile.

Existing appon a serverDockerfileImagecontainerpushECRregistrypullECS or EKSon Fargate

Containerizing fits a monolith when you want to move it with little code change, run it for longer than a Lambda function allows, or split it into services over time. Move user sessions and uploaded files out of the instance first (see stateless design below), so any copy of the container can serve any request.

Stateless design

To scale horizontally, a server should not keep user state in its own memory or disk. Keep sessions in Amazon ElastiCache or DynamoDB, store files in S3 or EFS, and keep data in a database. Then any instance can serve any request, and replacing one loses nothing.

Userrequestany instanceStateless appmany copiessession inElastiCacheshared state
Exam tip

If a question asks for the least operational overhead for a new event driven or API based workload, lean toward Lambda with API Gateway. If it needs a long running process or a container image that already exists, lean toward ECS on Fargate.

What to remember for the exam

  • Lambda runs for up to 15 minutes, scales automatically and is billed for execution, so it fits short event driven work.
  • Reserved concurrency limits and guarantees capacity, and provisioned concurrency reduces cold starts.
  • API Gateway adds throttling, caching, authorization and stages in front of a back end.
  • Step Functions orchestrates multi step workflows with retries and error handling.
  • Fargate runs containers without managing servers, for both ECS and EKS.
  • EKS is for teams that want Kubernetes. ECS is simpler and AWS specific.
  • Stateless instances behind a load balancer scale horizontally and recover easily.
  • Store session state in ElastiCache or DynamoDB, and files in S3 or EFS.

Common mistakes

Using Lambda for a job that runs for hours. The function times out at 15 minutes. Split the job, use Step Functions, or run it on containers or EC2.

Letting Lambda overwhelm a database. Each concurrent run can open a connection. Limit concurrency, or place a connection pooling layer such as Amazon RDS Proxy in front of the database.

Choosing EKS without a Kubernetes need. If nothing asks for Kubernetes, ECS is the simpler answer.

Storing sessions on the instance. Local sessions force sticky routing and are lost when the instance is replaced.

Watch out

Provisioned concurrency is charged while it is configured, even when there is no traffic, so use it only where cold start latency is a real problem.

Check yourself

  • What is the maximum run time of a Lambda function?

    15 minutes.

  • Which option runs containers without managing the underlying servers?

    AWS Fargate, used with either Amazon ECS or Amazon EKS.

  • Why should web servers be stateless in a scalable design?

    Any instance can then handle any request, so instances can be added or replaced without losing user data.

Topic 2 of 5 in Design Resilient Architectures

Test yourself on Design Resilient Architectures

The full AWS SAA-C03 question bank has 823 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.