Javascript required
Skip to content Skip to sidebar Skip to footer

How to Copy Aws Lambda Function to Local and Upload Back to Cloud

Amazon Web Services (AWS) are one of the almost used Deject Services in 2021 and they have many services to execute your lawmaking in the cloud. Today we are going to talk virtually one of that services: The AWS Lambda.

AWS Lambda

https://miro.medium.com/max/13334/1*RJs-ufPYhfbJaugiKtoOZA.png

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

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

Lambda Service supports many languages, similar Java, .NET, Go, JS, Python, and others. Cull your preferred and nosotros tin kickoff 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 nosotros shall employ AWS CLI to demonstrate how to deploy our local code to the Cloud.

Configure your AWS CLI locally

Before nosotros offset our tutorial you demand 2 steps:

  1. You need an active AWS Account. To create an account, get into this AWS Link.
  2. If don't accept installed AWS CLI on your car y'all tin check this AWS Link.

After installation is finished you lot need to configure your AWS credentials on your local calculator. The Credentials are to place in your local automobile, who are you and which AWS account your terminal volition accept access. The values that you lot need are AWS Access Primal and Secret Key.

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

  1. Become to https://console.aws.amazon.com/iamv2/abode#/users
  2. Click in your AWS user
  3. Click in the Security credentials tab
  4. Create access key if don't have one

Be VERY CAREFUL about these keys!!

  1. NEVER share them!
  2. 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 y'all want to brand sure you inserted correctly, yous can run the same control once again and it will impress your keys or a message showing None if failed.

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

Validate if your AWS CLI is working

Run the following control in your final:

          aws —version        

If your AWS CLI version is returned, yous tin continue the tutorial.

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

Creating a Lambda Office

Every AWS Lambda needs to know what permissions they have within AWS Earth. Then we came into some other AWS concept, which is IAM Roles. A role describes what actions your Lambda can practice with others AWS Resources.

For this bones tutorial, we need a office that allows our lambda to give the service principal lambda.amazonaws.com permission to telephone call the AWS Security Token Service AssumeRole action. If you want more than data most this function, enter this AWS Link.

Here it'south the part:

          {
"Version": "2012-10-17",
"Statement": [
{
"Issue": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Activeness": "sts:AssumeRole"
}
]
}

You tin insert this JSON in a file named trust-policy.json, and so now we tin create this function and transport information technology to AWS.

Run the following code in your local concluding:

          aws iam create-role — role-name bones-lambda-function — presume-role-policy-document file://trust-policy.json        

If a JSON with your role is printed in your last, information technology means that your role is created in AWS!

Creating a Sum Figurer Lambda

At present let'due south start the code role!

The apply case that we will implement will be a unproblematic Sum Calculator that will receive two arguments to add and return the outcome.

Create a file proper noun bones-lambda.js with the post-obit code:

          exports.handler            =            async function(result) {
const { numberA, numberB} = result;

return {
"sumResult": numberA + numberB
};
}

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

Lambdas are serverless

In resume, they stay in a "sleep" mode, that yous don't accept to pay when you are not using. There are many AWS services that can call a lambda, but in our use instance, we will call them from the AWS Console for testing.

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

At present you lot must zip your source lawmaking to transport it to AWS. You can do information technology with the concluding (if you have the goose egg installed) or you manually zip with the File Manager of your Os.

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

To deploy our lambda code, we demand the Amazon Resource Proper noun (ARN) of the role that we created in our last footstep.

Run the following command:

          aws iam get-part — function-proper name basic-lambda-function        

Search for the attribute of

          {
"Function": {
"Arn": "arn:aws:iam:***:function/bones-lambda-role"

And re-create the value of the Arn attribute.

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

          aws lambda create-office \
--function-name bones-lambda \
--runtime nodejs14.x \
--zip-file fileb://basic-lambda-role.zip \
--handler basic-lambda.handler \
--office arn:aws:iam:***:office/bones-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 bones-lambda that y'all just deployed.

AWS Lambda Panel

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

AWS Lambda Code

Click on the Test push to create a new Test.

Create a new Event Proper 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 push button.

At present, click again on the Test button and your lambda volition be triggered with your new consequence!

Lambda event result

At present you lot tin can create your own code and exam it easily in the AWS console.

Delete your Lambda after testing

Later you finished your test, always remember to delete your lambda so no extra cost appears in your AWS business relationship.

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

Delete your lambda with console

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

          aws lambda delete-function — office-name bones-lambda        

Github Page

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

References:

  • https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-awscli.html
  • https://docs.aws.amazon.com/cli/latest/reference/lambda/create-office.html

cahillessurn.blogspot.com

Source: https://towardsaws.com/deploying-aws-lambda-function-with-aws-cli-debddbea7b68