logo
Menu
Decrease cost and boost performance by moving to Graviton

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).
Lambda ARM-based Graviton architecture
Lambda ARM-based Graviton architecture

What is Graviton architecture?

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.
Eco-friendly cloud
Eco-friendly cloud

What are the benefits of changing the architecture from X86_64 to ARM64 Graviton?

General comparison of X86_64 architecture with Graviton
General comparison of X86_64 architecture with Graviton
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.
Lambda and Python on Graviton
Lambda and Python on Graviton

How to change Lambda’s architecture to Graviton with Python and AWS CDK?

Sample architecture which will be transitioned to Graviton
Sample architecture which will be transitioned to Graviton
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.

Select Graviton architecture for your Lambda function

Defining ARM64 architecture in Lambda’s CDK construct
Defining ARM64 architecture in Lambda’s CDK construct
You need to select ARM_64 as an architecture for your Lambda function.

Set Graviton as an architecture for your Lambda Layer

Defining target architecture of Lambda Layer
Defining target architecture of Lambda Layer
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.
Python and dependencies bundling for Lambda layer in proper architecture
Python and dependencies bundling for Lambda layer in proper architecture

Demo

Let’s check if everything works well. I’ve triggered cdk deploy command, and all assets were created successfully.
Deploying Graviton Lambda
Deploying Graviton Lambda
Let’s go to the AWS console and see what is there.
Checking Lambda architecture in AWS Console
Checking Lambda architecture in AWS Console
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.
Invoking Graviton Lambda with pytest
Invoking Graviton Lambda with pytest
Tests passed, meaning that everything works as expected 😎.
 

2 Comments