Deploy a Lambda function using AWS Cloud9

Learn how to deploy a Lambda function using AWS Cloud9.

Rushi Donga
Enlear Academy

--

What is AWS Cloud9?

AWS Cloud9 is an Integrated Development Environment which allows you to run, write and debug the code within a browser in the cloud.

It provides three main functionalities

  • Terminal
  • Code Editor
  • Debugger

Overview

  • Pre-requisite
  • Configuration
  • Output

Pre-requisite

  • An AWS account is required to implement the configurations.
  • Prior knowledge of AWS Lambda and EC2-Instance.

Configuration

Note : Configure all the resources in the same Region.

Log in to the AWS Management Console and search for the Cloud9 in the Search Bar and select Cloud9.

You will then be redirected to the AWS Cloud9 Management Console.

Step 1 — Create an AWS Cloud9 Environment.

Click on Create Environment button.

You will be redirected to the page which allows you to create a Cloud9 Environment.

In the Step-1 simply give a name and the description to the Cloud9 Environment and press Next Step.

In the Step-2 i.e. Configure Settings, leave all the settings to default and click Next Step.

NOTE: Step-2 involves the configuration of the Cloud9 Environment in an EC2 Instance. Select the Environment type, Instance type and the Platform according to your requirement.

In Step-3, review the configurations and click on Create environment button.

After clicking the Create Environment button you will be redirected to the page, which looks something like this.

AWS Cloud9 cloud-based Integrated Development Environment

Step 2 — Write the code for the Lambda function.

Now, in the terminal at the bottom, create a directory named Lambda using the command mkdir, and then move into the lambda directory using the command cd.

Now, create a Python file named lambda_function.py.

NOTE: The name of the python file will be used in future, make sure you note it down before you proceed.

Open the python file in the nano or vi editor and paste the code for the function in the editor. Then close the editor by saving the file and confirming the name of the python file.

Click on this link, for the sample lambda code.

NOTE: Below are the steps to install an external dependency onto a Lambda function. Follow this if your lambda function contains an external dependency or you can skip it.

Installing an external dependency on a Lambda function.

Here, I’m install an external dependency named requests, and below is the command to install requests external dependency.

NOTE: Here, -t . at the end of the command, indicates that this dependency will be installed only in this folder and not globally in the virtual environment.

Now, zip all the files contents within the folder lambda, using the zip command.

NOTE: Here -r *, at the end of command indicates, that all content in this lambda directory will be zipped recursively i.e. one by one.

Step 3 — Create a Lambda function.

AWS CLI for Lambda — Create function reference link

Use the following command to create a Lambda function.

Command description

  1. — function-name: Name of the Lambda function.
  2. — runtime: Defines the runtime environment of the Lambda function. For us it is python3.9.
  3. — role: Give the ARN of the IAM role which you want to assign to the Lambda function. Here we have simply assigned the ARN of basic-lambda-execution role.
  4. — zip-file: Give the full path of the zip file we created in step-2. It is mandatory to start the name of the file with fileb://Path/of/the/file.zip.
  5. — handler: Contains two parts with the following syntax.

python-file-name.function-to-be-executed-first

In our case, it is lambda_function.lambda_handler.

NOTE: Do not forget the dot in between the file name and the function name.

Congratulations ♥

With this, you have successfully configured and created a Lambda function using Cloud9.

Output

You can now check and test the newly created Lambda function in the AWS Lambda Management Console.

--

--

AWS Certified Solutions Architect. Presenting my learnings @AWS by translating them to a plain English. Writer @Enlear Academy | Writer @AWS in plain English