Sub topic 9
Graviton in Lambda, Fargate and managed databases
This chapter assumes you have read the earlier chapters on Arm64 compatibility and multi-architecture images. It also assumes you can use the AWS CLI and know the basics of Lambda, ECS and RDS.
On EC2 you pick an instance type and manage the operating system yourself. Managed services hide the instance, so moving to Graviton means changing one setting instead of rebuilding a fleet. That is also the reason these services are often the cheapest place to start. The setting differs by service, and so do the risks.
What changes in each service
| Service | Where you choose Graviton | What you must supply | Main risk |
|---|---|---|---|
| Lambda | The function's `architectures` setting (`arm64` or `x86_64`) | Arm64 native dependencies, layers and container images | A native library built for x86-64 fails at import time |
| Fargate (ECS) | `runtimePlatform.cpuArchitecture` in the task definition | An Arm64 or multi-architecture image | `exec format error` when the image has no Arm64 variant |
| RDS, Aurora, ElastiCache, OpenSearch | The instance class (for example `db.r7g.large`) | Nothing in your application code | A restart or failover during the class change |
AWS lists Graviton duration and vCPU prices about 20% below the matching x86 prices for Lambda and Fargate. Check the current pricing pages before you build a business case. Prices differ by Region, and you should compare your own measured cost per request instead of relying on the headline figure. The load-testing chapter covers how to do that.
Lambda
How the setting works
Each Lambda function has one architecture. You set it when you create the function or when you update its code. Lambda runs your code on a Graviton2-based host when the value is arm64. Your handler code is unchanged. Only the artifacts it loads can break.
Pure interpreted code, such as plain Python, Node.js or Ruby with no native extensions, usually moves without changes. Problems appear in three places:
- Native extensions in zip packages. Python wheels, Node.js
.nodeaddons and similar files are compiled for one architecture. - Layers. A layer declares compatible architectures. If you attach an x86-64-only layer to an
arm64function, the update is rejected or the function fails when it loads the layer's code. - Extensions and container images. Lambda extensions (for monitoring, for example) and container-image functions must also be built for Arm64.
Building a zip package for arm64
You can build on any machine if you ask pip for Arm64 wheels. The --only-binary=:all: flag makes pip fail instead of silently compiling a wrong-architecture package from source.
pip install \
--platform manylinux2014_aarch64 \
--implementation cp \
--python-version 3.12 \
--only-binary=:all: \
--target package \
-r requirements.txt
(cd package && zip -r ../function.zip .)
zip -g function.zip app.py
aws lambda update-function-code \
--function-name orders-api \
--zip-file fileb://function.zip \
--architectures arm64If a dependency has no prebuilt Arm64 wheel, pip stops with an error. At that point you need to build it on an Arm64 host or in an Arm64 container, or pick a newer version that publishes the wheel.
Confirming what you are running
A small handler confirms the architecture at run time. platform.machine() returns aarch64 on Graviton.
import platform
def handler(event, context):
return {"architecture": platform.machine()}You can also read the setting back without invoking the function:
aws lambda get-function-configuration \
--function-name orders-api \
--query 'Architectures'Memory and cost
Lambda allocates CPU in proportion to memory. A function that was tuned for x86-64 may need a different memory setting on Arm64, because duration can go up or down. Billing is memory multiplied by duration, so a function that gets faster at the same memory costs less twice over. Run the same test at several memory sizes on both architectures and compare cost per thousand invocations.
Fargate
How the setting works
On Fargate you do not choose an instance. You describe the CPU architecture in the task definition's runtimePlatform. Fargate then places the task on Graviton capacity. Linux tasks on Arm64 need platform version 1.4.0 or later, and Windows containers are not available on Arm64.
{
"family": "my-app-arm64",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::111122223333:role/ecsTaskExecutionRole",
"runtimePlatform": {
"cpuArchitecture": "ARM64",
"operatingSystemFamily": "LINUX"
},
"containerDefinitions": [
{
"name": "app",
"image": "111122223333.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0",
"essential": true,
"portMappings": [{ "containerPort": 8080, "protocol": "tcp" }]
}
]
}Register it and read it back:
aws ecs register-task-definition --cli-input-json file://taskdef-arm64.json
aws ecs describe-task-definition \
--task-definition my-app-arm64 \
--query 'taskDefinition.runtimePlatform'Image architecture must match
Fargate pulls the image variant that matches the task's architecture. This is why the multi-architecture build from the previous chapter helps: the tag my-app:1.0.0 can serve both an x86-64 task definition and an Arm64 one. If the image holds only an amd64 variant and the task asks for ARM64, the task stops. The container log shows exec format error, which is the kernel refusing to run a binary built for another architecture.
When you troubleshoot, check what the image actually contains:
docker buildx imagetools inspect \
111122223333.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0The output lists one entry per platform. Look for linux/arm64.
Sidecars count too
Every container in the task runs on the same architecture. A log router, a metrics agent or a service-mesh proxy sidecar must also publish an Arm64 image. Teams often migrate the application image and then find a third-party sidecar that does not have one. Check every container in the task definition, not only your own.
Rolling out
A single ECS service uses one task definition revision, so you cannot mix architectures inside one service. Create a second service from the Arm64 task definition and attach it to the same load balancer. A weighted pair of target groups on the Application Load Balancer listener lets you move traffic gradually, for example 5%, 25%, then 100%. Rolling back means setting the weight of the x86-64 target group back to 100%. Keep that service running until the Arm64 service has been stable through at least one full business cycle.
Managed databases and caches
For RDS, Aurora, ElastiCache and OpenSearch Service, Graviton is an instance class. Instance classes with a g in the family name, such as db.t4g, db.m7g and db.r7g, run on Graviton. Your application connects over the network to the same endpoint with the same protocol, so the client needs no code changes and no new drivers.
Three conditions apply:
- Engine support. Not every engine offers Graviton classes. RDS for MySQL, MariaDB and PostgreSQL and Aurora MySQL and PostgreSQL do. Some commercial engines do not. Also, Graviton classes can require a minimum engine version.
- Region support. A class that exists in one Region may be missing in another.
- A change event. Switching class is a modification, and it causes a restart or a failover.
Do not guess from the documentation. Ask the API which classes your engine version can use in your Region:
aws rds describe-orderable-db-instance-options \
--engine postgres \
--engine-version 16.4 \
--query "OrderableDBInstanceOptions[?starts_with(DBInstanceClass, 'db.r7g')].DBInstanceClass" \
--output text | tr '\t' '\n' | sort -uAn empty result means that class is not available for that engine version here.
Changing the class with the least downtime
The safest order depends on the topology:
- Aurora cluster. Add a Graviton reader to the cluster, wait until its replica lag is low, and fail over to it. Then replace the remaining x86-64 instances. The failover is the only interruption, and it normally takes seconds to under a minute.
- RDS Multi-AZ. Modify the instance class. RDS updates the standby first and then fails over, so expect a short interruption while the endpoint's DNS switches. Applications that cache DNS for a long time or hold stale connections recover more slowly.
- RDS Single-AZ. The instance is unavailable while it restarts. Schedule this in a maintenance window.
aws rds modify-db-instance \
--db-instance-identifier orders-db \
--db-instance-class db.r7g.large \
--apply-immediatelyWithout --apply-immediately, the change waits for the next maintenance window. Before a production change, take a snapshot and test the same change on a restored copy. The restored copy also lets you run your real queries and compare latency.
Caches and search
ElastiCache nodes and OpenSearch Service data nodes follow the same pattern: pick a Graviton node type such as cache.r7g.large, and let the service replace nodes. For ElastiCache replication groups, the service handles the replacement with a failover, so a client library with automatic reconnect matters. For OpenSearch, a node-type change triggers a blue/green deployment, which copies data and takes time proportional to the domain size. Watch cluster health during the change and keep free storage in reserve.
Verification checklist
Use the same questions in every service, and record the answers before you widen the rollout:
- Does every artifact (code, layer, image, sidecar) have an Arm64 build?
- Does a test environment run on Graviton with production-like data and load?
- Is cost per unit of work lower, measured as cost per thousand requests or per transaction?
- Are p95 and p99 latency and the error rate equal to or better than the x86-64 baseline?
- Is the rollback a configuration change that takes minutes instead of a rebuild?
Common mistakes
- Assuming Graviton is always cheaper. A workload that runs 30% longer on Arm64 can lose the 20% price advantage. Measure first.
- Forgetting the build pipeline. CI that builds on x86-64 runners produces x86-64 artifacts unless you cross-build or use Arm64 runners.
- Updating the function but not the layer. The layer's
CompatibleArchitecturesvalue must includearm64. - Moving the primary database first. Move a reader or standby first and prove it works.
Exercise
A Fargate service was switched to an Arm64 task definition. Every new task stops within seconds, and the container log says "exec format error". The image tag was built with a plain docker build on an x86-64 CI runner. What is wrong, and how do you fix it without changing the task definition?
Show answer
The image contains only a linux/amd64 variant, so the Arm64 task cannot execute its binaries. Rebuild and push the tag as a multi-architecture image, for example with docker buildx build --platform linux/amd64,linux/arm64 --push. Confirm that docker buildx imagetools inspect lists linux/arm64. Also check that any sidecar images in the task have Arm64 variants.
Exercise
You must move a Single-AZ RDS PostgreSQL instance and an Aurora PostgreSQL cluster to Graviton classes. Which command do you run first to check availability, and what is the lowest-risk sequence for the Aurora cluster?
Show answer
Run aws rds describe-orderable-db-instance-options with the engine and engine version, and filter for the Graviton class you want, to confirm it exists in the Region. For Aurora, add a Graviton reader, wait for low replica lag, fail over to it, then replace the remaining x86-64 instances. The Single-AZ instance has no standby, so it will be unavailable during the restart and needs a maintenance window.
Exercise
A Python Lambda function that uses a compiled library works on x86-64. You run update-function-code --architectures arm64 with the same zip file, and invocations fail at import time. Why, and what is the fix?
Show answer
The zip contains wheels compiled for x86-64, and Arm64 cannot load them. Rebuild the package with pip install --platform manylinux2014_aarch64 --only-binary=:all: (or on an Arm64 host), zip it again and update the function. Also confirm that any attached layers list arm64 as a compatible architecture.