Decrease cost and boost performance by moving to Graviton
Comprehensive guide showing what the Graviton architecture is and how to transfer Lambda functions to the Graviton architecture using Python and AWS CDK.
Published Feb 23, 2024
Last Modified Apr 1, 2024
If you have a cloud environment, it is always worth considering its optimization. Certain tweaks can result not only into reduced costs, but also into increased efficiency. In today’s post, we will look at what the Graviton architecture is and how to transfer existing Lambda functions to the Graviton architecture using Python and AWS CDK IaC (eng. Infrastructure as a Code).
AWS Graviton is a family of 64-bit ARM-based CPUs. They are presented as cheaper and more efficient equivalents of the classic X86_64 architecture. By reducing costs and increasing the efficiency of the ARM architecture, we are able to perform more operations per watt of energy and, therefore, per dollar. We are also becoming more eco-friendly by reducing our carbon footprint, thanks to the lower energy-consumption of ARM architecture.
Let’s take a look at the research that AWS made to compare the performance and cost of Lambda functions based on the Graviton architecture. You can find the whole analysis here. We see increase of performance and work that your function can do for every $ spent, but what’s the most important we see decrease in cost. Everything in compare with X86_64 architecture.
For one of my talks, I once created a project showing different approaches of testing Lambda functions. If you’re interested in this topic, you can find some guides in my previous post here. This is a Python project based on AWS CDK as a tool for IAC. It consists of a simple API Gateway with Lambda function connected to read data from SSM. It fits perfectly into our needs, because Graviton was not used in it before. You can find the complete project here. To move to ARM64 (Graviton architecture), we need to do some steps.
You need to select ARM_64 as an architecture for your Lambda function.
Code that runs in a Lambda function often needs various dependencies, e.g. external packages. The best place to store them is the Lambda Layer connected to the appropriate function. We need to ensure that the packages included there support the ARM_64 architecture.
The topic of installing dependencies and placing them in Lambda function layers is a separate thing to discuss. For the purposes of this post, I used the simplest way to build dependencies, using the CDK construct:
aws_lambda_python_alpha.PythonLayerVersion
, which, after passing the appropriate parameters, will correctly install dependencies in our Lambda Layer by running Docker container in which the whole bundling process will be executed.Let’s check if everything works well. I’ve triggered
cdk deploy
command, and all assets were created successfully.Let’s go to the AWS console and see what is there.
As you see our Lambda is working with Graviton (arm64) as expected. Now, let’s run the final test so let’s invoke this Lambda by running my integration test.
Tests passed, meaning that everything works as expected 😎.