![Decrease cost and boost performance by moving to Graviton](https://assets.community.aws/a/2clooUiyvyRdUErrziH7PyOVMYE/Lamb.webp)
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](https://assets.community.aws/a/2clpGOTAg18d2I1TeqBM5VNjjvm/1-jpe.webp)
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](https://assets.community.aws/a/2clpJbyj4YEGECZhuT8irxbvGsO/2-jpe.webp)
![General comparison of X86_64 architecture with Graviton General comparison of X86_64 architecture with Graviton](https://assets.community.aws/a/2clpMKj7KeCs5e7R2NAcAzdfAZA/3-pn.webp)
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](https://assets.community.aws/a/2clpP60QYHrXxuGF1aasoJ2wHKO/4-pn.webp)
![Sample architecture which will be transitioned to Graviton Sample architecture which will be transitioned to Graviton](https://assets.community.aws/a/2clpRZ1Z0fC81FzWn8qNWdTe8Kp/5-pn.webp)
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.
![Defining ARM64 architecture in Lambda’s CDK construct Defining ARM64 architecture in Lambda’s CDK construct](https://assets.community.aws/a/2clpWVEiyTHlzsmeu7AbUc1Qqn6/6-pn.webp)
You need to select ARM_64 as an architecture for your Lambda function.
![Defining target architecture of Lambda Layer Defining target architecture of Lambda Layer](https://assets.community.aws/a/2clpZJj90oO7z89yTo0SF9YybRf/7-pn.webp)
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](https://assets.community.aws/a/2clpcNjTTRTHLwdHfwdfZ5WXl1l/8-pn.webp)
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](https://assets.community.aws/a/2clphpdshdEkipH3KL5UxVfYQeG/9-pn.webp)
Let’s go to the AWS console and see what is there.
![Checking Lambda architecture in AWS Console Checking Lambda architecture in AWS Console](https://assets.community.aws/a/2clpkApEeDlXELpUcHzk2wQLF5m/10-p.webp)
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](https://assets.community.aws/a/2clpmqrkl2qCcWXA6aOTsZSn5nW/11-p.webp)
Tests passed, meaning that everything works as expected 😎.