Sonia Manoubi
crunchcrunch

crunchcrunch

Save money by deploying your lambda on ARM with CDK

Save money by deploying your lambda on ARM with CDK

In 2018, AWS launched their first EC2 instances powered by ARM-based AWS Graviton Processors. These are 64-bit Arm-based processors that are custom built by AWS and offer a better price to performance ratio. The second generation (AWS Graviton2) of processors deliver 7x more performance, 4x more compute cores, 5x faster memory, and 2x larger caches.

AWS launched Graviton2 support for AWS Lambda Functions, which means that Lambda Functions can now execute on Graviton2 processors and take advantage of the improved price performance of that architecture.

In this article, i'll show you how to configure the processor using AWS CDK.

Make a new project

Let's create the main folder of the project

mkdir armLambda
cd armLambda
npm init -y

Then install CDK and aws-lambda package

npm install -g aws-cdk
npm i @aws-cdk/aws-lambda
mkdir cdk-folder
cd cdk-folder
cdk init app --language typescript

CDK part

Open the file lib/cdk-folder-stack.ts


import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import { Architecture } from '@aws-cdk/aws-lambda';
import * as path from 'path';

export class CdkFolderStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // The code that defines the lambda

    const myLambdaFunction = new lambda.Function(this, 'my-lambda', {
      runtime: lambda.Runtime.NODEJS_14_X,
      memorySize: 256,
      timeout: cdk.Duration.seconds(60),
      handler: 'index.main',
      // 🤖 --- this line below makes all the difference --- 🤖
      architecture: Architecture.ARM_64,
      code: lambda.Code.fromAsset(path.join(__dirname, '/../app')),
    });

}

Create the lambda

We are going to create a folder for the lambda at the root (next to lib) :

Let's create a simple lambda that fetches the time on a free API

mkdir app
cd app
mkdir my-lambda
cd my-lambda
npm init -y 
touch index.js

Here is the code for a super simple lambda :)


export async function main(event) {
  console.log("Hello ARM Lambda");
}

Let's try it

Let's bootstrap first

cdk bootstrap
⏳  Bootstrapping environment aws://XXXXXXXXXXXXX/us-east-1...
 ✅  Environment aws://XXXXXXXXXXXXXX/us-east-1 bootstrapped (no changes).

Second command, to create the Cloudformation template for our app :

cdk synth

And now, let's deploy !

cdk deploy

Go to the console

On the console, go to Lambda > your lambda

Capture d’écran 2021-11-01 à 10.06.35.png

Congrats !

Now you can save some money ! 💰

Cleaning

Don't forget to clean after this tutorial, i don't want you to have a huge bill on AWS!

cdk destroy

Conclusion

As you can see, it's pretty simple with CDK to deploy your lambdas on ARM.

Let me know if you have any question in the comments ;)

Did you find this article valuable?

Support Sonia Manoubi by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this