Deploying AWS Lambda Function with AWS CLI
By: Leo Campanha
Tags: Development, AWS, Lambda, Backend, Serverless

Amazon Web Services (AWS) are one of the most used Cloud Services in 2021 and they have many services to execute your code in the cloud. Today we are going to talk about one of that services: The AWS Lambda.

AWS Lambda

Lambda is an AWS service that you can upload your software code and then execute what you need, without worrying too much about the infrastructure of your code.

If you want to have advanced configurations, you can set the balance of memory, CPU, network, and others. But this is not our current focus.

Lambda Service supports many languages, like Java, .NET, Go, JS, Python, and others. Choose your preferred and we can start the deployment of our lambda.

Deploying an AWS Lambda

There are many options to deploy an AWS Lambda to Amazon, like doing in the AWS Console Website, using AWS CLI, Python Boto3, AWS CDK, and many more. But today we shall use AWS CLI to demonstrate how to deploy our local code to the Cloud.

Configure your AWS CLI locally

Before we start our tutorial you need 2 steps:

You need an active AWS Account. To create an account, go into this AWS Link. If don’t have installed AWS CLI on your machine you can check this AWS Link.

After installation is finished you need to configure your AWS credentials on your local computer. The Credentials are to identify in your local machine, who are you and which AWS account your terminal will have access. The values that you need are AWS Access Key and Secret Key.

If you don’t know where to get these keys:

Go to https://console.aws.amazon.com/iamv2/home#/users Click in your AWS user Click in the Security credentials tab Create access key if don’t have one

Be VERY CAREFUL about these keys!!

NEVER share them! NEVER copy them to anyone!!!

After getting your keys, run in your local terminal:

aws configure

Enter your keys and you are all set. If you want to make sure you inserted correctly, you can run the same command again and it will print your keys or a message showing None if failed.

Before we start creating our code, you can see all the examples that we will do in this tutorial on this GitHub page.

Validate if your AWS CLI is working

Run the following command in your terminal:

aws —version

If your AWS CLI version is returned, you can continue the tutorial.

If any error returns you can enter this AWS Link for more information.

Creating a Lambda Role

Every AWS Lambda needs to know what permissions they have inside AWS World. So we came into another AWS concept, which is IAM Roles. A role describes what actions your Lambda can do with others AWS Resources.

For this basic tutorial, we need a role that allows our lambda to give the service principal lambda.amazonaws.com permission to call the AWS Security Token Service AssumeRole action. If you want more information about this role, enter this AWS Link.

Here it’s the role:

{

"Version": "2012-10-17",

"Statement": [

{

"Effect": "Allow",

"Principal": {

"Service": "lambda.amazonaws.com"

},

"Action": "sts:AssumeRole"

}

]

}

You can insert this JSON in a file named trust-policy.json, so now we can create this role and send it to AWS.

Run the following code in your local terminal:

aws iam create-role — role-name basic-lambda-role — assume-role-policy-document file://trust-policy.json

If a JSON with your role is printed in your terminal, it means that your role is created in AWS!

Creating a Sum Calculator Lambda

Now let’s start the code part!

The use case that we will implement will be a simple Sum Calculator that will receive two arguments to add and return the result.

Create a file name basic-lambda.js with the following code:

exports.handler = async function(event) {

const { numberA, numberB} = event;



return {

"sumResult": numberA + numberB

};

}

In a Lambda you need to say where the logic will begin. And it begins in this method called handler that is being exported. Within the handler, you will insert all code that you want.

Lambdas are serverless

In resume, they stay in a “sleep” mode, that you don’t have to pay when you are not using. There are many AWS services that can call a lambda, but in our use case, we will call them from the AWS Console for testing.

When you call a Lambda, you can send via the input event some data that you like to be processed. Like in this code case, we are passing two numbers to be added, and then return the result.

Now you must zip your source code to send it to AWS. You can do it with the terminal (if you have the zip installed) or you manually zip with the File Manager of your OS.

zip basic-lambda-role.zip basic-lambda.js

To deploy our lambda code, we need the Amazon Resource Name (ARN) of the role that we created in our last step.

Run the following command:

aws iam get-role — role-name basic-lambda-role

Search for the attribute of

{

"Role": {

"Arn": "arn:aws:iam:***:role/basic-lambda-role"

And copy the value of the Arn attribute.

Let’s deploy our lambda function, changing the role ARN from the value that you just copied.

aws lambda create-function \

--function-name basic-lambda \

--runtime nodejs14.x \

--zip-file fileb://basic-lambda-role.zip \

--handler basic-lambda.handler \

--role arn:aws:iam:***:role/basic-lambda-role

If the terminal returned the JSON with the lambda attributes, the deploy worked!

Test the Lambda in Console

Let’s test if our lambda is working! Enter in the link: https://console.aws.amazon.com/lambda/ and search for the basic-lambda that you just deployed.

AWS Lambda Console

Click on the basic-lambda link and you will see our lambda code.

AWS Lambda Code

Click on the Test button to create a new Test.

Create a new Event Name as SumCalculator and insert the following Input Event, that we will send to our lambda:

{

“numberA”: 5,

“numberB”: 8

}

Create a Lambda Test

And click on the Create button.

Now, click again on the Test button and your lambda will be triggered with your new event!

Lambda event result

Now you can create your own code and test it easily in the AWS console.

Delete your Lambda after testing

After you finished your test, always remember to delete your lambda so no extra cost appears in your AWS account.

You can delete it in the AWS lambda page that you were testing by clicking on Actions -> Delete function.

Delete your lambda with console

Or you can run an AWS CLI command to delete it as you created:

aws lambda delete-function — function-name basic-lambda

Github Page

All tutorial code is in: https://github.com/lcampanha/awscli-lambda-deploy

References: