0
0
AWScloud~5 mins

Serverless vs container decision in AWS - CLI Comparison

Choose your learning style9 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.