Bird
Raised Fist0
AWScloud~5 mins

Serverless vs container decision in AWS - CLI Comparison

Choose your learning style10 modes available
Introduction
Choosing between serverless and containers helps you decide how to run your app in the cloud. Serverless lets you run code without managing servers, while containers package your app and its environment to run anywhere.
When you want to run a small app that only runs when needed and you don't want to manage servers.
When you have a complex app that needs specific software or settings and you want full control over the environment.
When you want to save money by paying only for the exact time your code runs.
When you need to run multiple apps or services together with consistent environments.
When you want to quickly scale your app up or down based on demand without manual setup.
Commands
This command creates a serverless function in AWS Lambda using Python 3.9. It uploads your code packaged in app.zip and sets the execution role and handler.
Terminal
aws lambda create-function --function-name my-serverless-app --runtime python3.9 --role arn:aws:iam::123456789012:role/lambda-execution-role --handler app.lambda_handler --zip-file fileb://app.zip
Expected OutputExpected
{ "FunctionName": "my-serverless-app", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-serverless-app", "Runtime": "python3.9", "Role": "arn:aws:iam::123456789012:role/lambda-execution-role", "Handler": "app.lambda_handler", "CodeSize": 12345, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2024-06-01T12:00:00.000+0000", "CodeSha256": "abc123def456ghi789", "Version": "$LATEST" }
→
--runtime - Specifies the language and version your function uses
→
--role - Defines permissions your function has when running
→
--handler - Specifies the entry point of your code
This command creates a new container cluster in AWS ECS where you can run and manage containerized apps.
Terminal
aws ecs create-cluster --cluster-name my-container-cluster
Expected OutputExpected
{ "cluster": { "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/my-container-cluster", "clusterName": "my-container-cluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0 } }
→
--cluster-name - Names your container cluster for easy identification
This command registers a task definition for your container app specifying CPU, memory, and container details to run on AWS Fargate.
Terminal
aws ecs register-task-definition --family my-task --network-mode awsvpc --requires-compatibilities FARGATE --cpu 256 --memory 512 --container-definitions '[{"name":"my-app","image":"nginx:1.23","portMappings":[{"containerPort":80,"protocol":"tcp"}]}]'
Expected OutputExpected
{ "taskDefinition": { "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/my-task:1", "family": "my-task", "revision": 1, "status": "ACTIVE", "requiresAttributes": [ {"name": "com.amazonaws.ecs.capability.ecr-auth"}, {"name": "ecs.capability.execution-role-awslogs"} ], "cpu": "256", "memory": "512", "networkMode": "awsvpc", "containerDefinitions": [ { "name": "my-app", "image": "nginx:1.23", "portMappings": [ {"containerPort": 80, "protocol": "tcp"} ] } ] } }
→
--requires-compatibilities - Specifies the launch type, here Fargate for serverless containers
→
--network-mode - Defines networking for the container, awsvpc gives each container its own IP
This command runs your container task on the cluster using Fargate, assigning it to a subnet with a public IP for internet access.
Terminal
aws ecs run-task --cluster my-container-cluster --launch-type FARGATE --task-definition my-task --network-configuration 'awsvpcConfiguration={subnets=["subnet-0abc1234"],assignPublicIp=ENABLED}'
Expected OutputExpected
{ "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/my-task/1234567890abcdef", "lastStatus": "PENDING", "desiredStatus": "RUNNING" } ], "failures": [] }
→
--launch-type - Chooses Fargate to run containers without managing servers
→
--network-configuration - Sets networking details like subnet and IP assignment
This command runs your serverless function and saves the output to a file named output.txt to check the result.
Terminal
aws lambda invoke --function-name my-serverless-app output.txt
Expected OutputExpected
{ "StatusCode": 200, "ExecutedVersion": "$LATEST" }
→
--function-name - Specifies which Lambda function to run
Key Concept

If you remember nothing else from this pattern, remember: serverless runs code without managing servers, while containers package your app and environment for flexible, controlled deployment.

Common Mistakes
Trying to run a container without registering a task definition first
ECS needs a task definition to know what container to run and how
Always register a task definition before running a container task
Not assigning the correct IAM role to Lambda functions
Without proper permissions, Lambda cannot access resources it needs and will fail
Create and assign an IAM role with the right permissions for your Lambda function
Using serverless for apps that need long-running processes or specific OS-level control
Serverless functions have time limits and limited environment control
Use containers when you need full control or long-running services
Summary
Create a Lambda function to run code without managing servers.
Create an ECS cluster and register a task definition to run containers with control over environment.
Run containers on AWS Fargate to avoid managing servers but keep environment control.
Invoke Lambda functions to test serverless code execution.
Choose serverless for simple, event-driven apps and containers for complex, customizable apps.

Practice

(1/5)
1. Which AWS service lets you run code without managing servers, automatically scaling with demand?
easy
A. Amazon ECS
B. AWS Lambda
C. Amazon EC2
D. AWS Elastic Beanstalk

Solution

  1. Step 1: Understand serverless concept

    Serverless means you don't manage servers; code runs on demand.
  2. Step 2: Identify AWS service matching serverless

    AWS Lambda runs code without server management and scales automatically.
  3. Final Answer:

    AWS Lambda -> Option B
  4. Quick Check:

    Serverless = AWS Lambda [OK]
Hint: Serverless means no servers to manage, pick Lambda [OK]
Common Mistakes:
  • Choosing EC2 which requires server management
  • Confusing containers with serverless
  • Picking Elastic Beanstalk which manages servers but not fully serverless
2. Which AWS service is best described by this syntax snippet for deploying a container?
aws ecs create-cluster --cluster-name myCluster
easy
A. Amazon S3 bucket creation
B. AWS Lambda function deployment
C. Amazon ECS container cluster creation
D. AWS CloudFormation stack deployment

Solution

  1. Step 1: Analyze the command

    The command uses 'aws ecs create-cluster' which is for ECS clusters.
  2. Step 2: Match command to service

    ECS is AWS service for managing containers in clusters.
  3. Final Answer:

    Amazon ECS container cluster creation -> Option C
  4. Quick Check:

    "aws ecs create-cluster" = ECS container cluster [OK]
Hint: ECS commands manage containers, Lambda uses different syntax [OK]
Common Mistakes:
  • Thinking this command deploys Lambda functions
  • Confusing S3 bucket commands with ECS
  • Assuming CloudFormation syntax
3. You have a web app that needs to run continuously with custom OS settings. Which AWS option fits best?
Options:
A) AWS Lambda
B) AWS CloudFront
C) Amazon S3
D) Amazon ECS with Fargate
medium
A. AWS Lambda
B. AWS CloudFront
C. Amazon S3
D. Amazon ECS with Fargate

Solution

  1. Step 1: Identify app requirements

    The app runs continuously and needs custom OS settings, so it needs control over environment.
  2. Step 2: Match service to requirements

    AWS Lambda is event-driven and short-lived, not continuous. ECS with Fargate runs containers continuously with environment control.
  3. Final Answer:

    Amazon ECS with Fargate -> Option D
  4. Quick Check:

    Continuous app + custom OS = ECS Fargate [OK]
Hint: Continuous with custom OS? Choose containers (ECS) not Lambda [OK]
Common Mistakes:
  • Choosing Lambda for continuous workloads
  • Confusing S3 (storage) or CloudFront (CDN) as compute
  • Ignoring environment control needs
4. You tried running a container on AWS Lambda but it failed. What is the likely cause?
medium
A. Lambda does not support running containers longer than 15 minutes
B. Lambda requires a Dockerfile to deploy containers
C. Lambda only supports Python code, not containers
D. Lambda cannot run containers at all

Solution

  1. Step 1: Understand Lambda container support

    Lambda supports container images but has a max execution time of 15 minutes.
  2. Step 2: Identify cause of failure

    If container runs longer than 15 minutes, Lambda will fail the execution.
  3. Final Answer:

    Lambda does not support running containers longer than 15 minutes -> Option A
  4. Quick Check:

    Lambda container max time = 15 minutes [OK]
Hint: Lambda containers have 15-minute max runtime limit [OK]
Common Mistakes:
  • Thinking Lambda cannot run containers at all
  • Believing Lambda only supports Python code
  • Confusing Dockerfile requirement
5. Your startup wants to minimize costs and avoid managing servers for a new app with unpredictable traffic spikes. Which AWS approach is best?
hard
A. Deploy on AWS Lambda for automatic scaling and no server management
B. Use Amazon ECS with EC2 instances for full control
C. Host on Amazon EC2 with manual scaling
D. Use AWS Elastic Beanstalk with fixed server capacity

Solution

  1. Step 1: Analyze startup needs

    Startup wants to minimize costs and avoid managing servers, with unpredictable traffic.
  2. Step 2: Match AWS service to needs

    AWS Lambda automatically scales with traffic and requires no server management, reducing cost and effort.
  3. Step 3: Compare other options

    ECS with EC2 or Elastic Beanstalk requires server management or fixed capacity, increasing cost and complexity.
  4. Final Answer:

    Deploy on AWS Lambda for automatic scaling and no server management -> Option A
  5. Quick Check:

    Min cost + no servers + spikes = Lambda [OK]
Hint: No servers + spikes? Pick Lambda for auto scaling [OK]
Common Mistakes:
  • Choosing ECS or EC2 which need server management
  • Assuming Elastic Beanstalk auto-scales without servers
  • Ignoring cost and management factors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard