How to Create a Lambda Function with AWS CLI

Introduction

AWS Lambda is a serverless computing platform that allows you to run code without having to manage any underlying infrastructure. It is a great way to quickly deploy applications and services without having to worry about managing servers. In this lesson, we will learn how to create a Lambda function with AWS CLI.

Prerequisites

Before we begin, there are a few prerequisites that you should be aware of. First, you will need an AWS account and access to the AWS CLI. You will also need to have a basic understanding of the AWS Lambda service and how it works. Finally, you should have a basic understanding of the command line and how to use it.

Creating a Lambda Function with AWS CLI

Now that we have all of the prerequisites out of the way, let’s get started. The first step is to create a Lambda function using the AWS CLI. To do this, we will use the aws lambda create-function command. This command takes a few parameters, such as the name of the function, the runtime, and the code that will be executed.

For example, let’s say we want to create a function called my-function that runs Node.js code. We can do this with the following command:

aws lambda create-function \
  --function-name my-function \
  --runtime nodejs12.x \
  --zip-file fileb://my-function.zip

In this command, we are specifying the name of the function (my-function), the runtime (nodejs12.x), and the code that will be executed (my-function.zip).

Configuring the Lambda Function

Once the Lambda function has been created, we can configure it using the aws lambda update-function-configuration command. This command takes a few parameters, such as the memory size, timeout, and environment variables.

For example, let’s say we want to configure our my-function function to have a memory size of 128 MB and a timeout of 10 seconds. We can do this with the following command:

aws lambda update-function-configuration \
  --function-name my-function \
  --memory-size 128 \
  --timeout 10

Deploying the Lambda Function

Once the Lambda function has been configured, we can deploy it using the aws lambda update-function-code command. This command takes a few parameters, such as the name of the function and the code that will be executed.

For example, let’s say we want to deploy our my-function function with the code in the my-function.zip file. We can do this with the following command:

aws lambda update-function-code \
  --function-name my-function \
  --zip-file fileb://my-function.zip

Testing the Lambda Function

Once the Lambda function has been deployed, we can test it using the aws lambda invoke command. This command takes a few parameters, such as the name of the function and the input data.

For example, let’s say we want to test our my-function function with the input data in the input.json file. We can do this with the following command:

aws lambda invoke \
  --function-name my-function \
  --payload fileb://input.json \
  output.json

Conclusion

In this lesson, we learned how to create a Lambda function with AWS CLI. We started by creating the function using the aws lambda create-function command. We then configured the function using the aws lambda update-function-configuration command. Finally, we deployed the function using the aws lambda update-function-code command and tested it using the aws lambda invoke command. By following these steps, you should now be able to create and deploy Lambda functions with AWS CLI.

Share :