How to Create an AWS Lambda Function in Cloud9

Beginners guide to get you started

Marvin Davila
Analytics Vidhya
8 min readJan 8, 2020

--

Photo by Gian D. on Unsplash

AWS Cloud9 is an IDE that support many programming languages, runtime debuggers, and a built in terminal. It contains a collection of tools that you use to code, build, and helps you release software to the cloud.

You can create a Lambda Function from Cloud9 and deploy to AWS Lambda. AWS Lambda is a compute service that lets you run code without provisioning or managing servers. It executes your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time you consume — there is no charge when your code is not running.

If you’re just getting started with all the AWS services, hopefully you’ll find this helpful! For the majority of this article, I will go through the Cloud9 setup on AWS and then a brief overview of AWS Lambda.

What is covered in this article:

  • How to Create a Cloud9 Environment
  • How to create a Lambda Function from Cloud9
  • The basics of Cloud9
  • Use your Lambda Function from AWS Lambda
  • What are Event Triggers and how to use it

Creating a Cloud9 Environment

Go to the homepage and click on the orange box Create environment.

Now you will need to give your environment a name and description. Then click Next step.

In the Environment settings keep everything as is, except for Platform. Change it to Ubuntu Server 18.04 LTS. Then click Next step.

Review your environment name and settings. Click Create environment if your setting is correct.

You will have to wait until your environment is created. We will create a Lambda Function when environment is ready.

Create Lambda Function in Cloud9

A Welcome tab will open up when your environment is made. Click on Create Lambda Function… to continue.

You can also create a Lambda Function by clicking on the AWS Resources tab on the right side. Then click on the Lambda+ symbol to create the function. You can create as many as you want and you will see the list of them under Local Functions.

Give your function a name and click Next.
Note: Application name autofills with your Function name input.

Select runtime to Python 3.6 and click empty-python. Click Next.

Don’t change Function trigger and click Next.
Note: You can change this if you know what you are doing.

Don’t change any settings here and click Next.
Note: For Memory (MB), choose the amount of memory, in megabytes, that this function will use. To get more compute power increase Memory (MB). 🤑

Look over your Lambda function settings and click Finish.

Now you have created a Lambda Function in AWS Cloud9. We are going to continue and learn the basics of Cloud9.

Cloud9 Basics

The following part will show you how to navigate your environment and test a function.
On the left side of your Cloud9 environment, you can view the folder structure. The Tutorial folder is your Cloud9 environment folder. The tutorial folder is where the lambda function files are located. If you click lambda_function.py it will open up and you can edit your function. The venv folder is where your dependencies is stored when you pip install in your virtual environment.

Let’s pip install numpy in our Lambda Function environment. Right-Click tutorial and click Open Terminal Here.
Note: This is just for a demonstration and not going to be used in the function.

It will open a new tab next to lambda_function.py.
Note: If (venv) is in front of line you are using your virtual environment. 👍🏼

# to enable our virtual env
source vent/bin/activate
# checking dependencies installed already
pip freeze
# installing numpy in venv environment
pip install numpy
# checking if numpy version installed correctly
pip freeze

Now you know how to install packages in your virtual environment.
Note: Careful of the size of the packages. Lambda Function deployment size limit is 250MB.

The following code is the lambda function we will be using. Copy and paste it to your lambda_function.py.

import numpy as npdef math(a, b):
add = a + b
sub = a - b
mul = a * b
div = a / b
return print("add:", add, "\nsub:", sub, "\nmul:", mul, "\ndiv:", div)
def lambda_handler(event, context):
math(3,2)
return 'You did it! :)'

Save file(cmd+s) and press Run at the top center of the page. Then a side window will appear on the right. Click Run to test your function.

After you clicked Run, wait and check your result. You should get the same result. Important things to look at is the Max memory used and Time.
Note: Some failures when running a lambda function could be a timeout issue. To fix it, you will need to right-click your function on the right and click Edit Config. (Max 15-minutes) You shouldn’t run into this issue with the code above.

We have a working Lambda Function and we should deploy it. On the right side open the AWS Resources tab. Right-Click your lambda function tutorial and click Deploy. Now wait for the loading symbol to disappear to know it completed the deployment with no failures. A pop-up will appear if the deployment failed.

Now we can move on to testing the function on AWS Lambda.

Use Function in AWS Lambda

Go to AWS Lambda from your console. On the right click on Functions and you will see a list of functions. Look for the function name that starts with cloud9-yourfunctionname-yourfunctionname-letters&numbers and click on it.

The page you are on is that specific Lambda function dashboard. Towards the top to the right side, click on Select a test event and then click on Configure test events.

We are going to test the lambda function by creating a test event. Click Create new test event and change Event name to anything you want. In the code block all we need is an open and close curly bracket. Then click Create.

Select your test event and click Test.

You should get the same result in green if you did everything correctly. Otherwise read your error and try to debug. You could also restart this tutorial. :)

Important thing to look at is Duration to check how long it takes to execute your function. The Bill duration for billing information. The Log output to see your results or error message.

We were able to test your Cloud9 Lambda function from AWS Lambda. :)

Event Triggers

For a lambda functions to execute, an event must occur. You can set up a trigger that execute at a time interval. That is the example I’m going to show.

In your lambda function dashboard, click on + Add trigger and select CloudWatch Events.

For Rule, select Create a new rule. Name your rule whatever you like. Give a short description for your rule. Select Schedule expression for Rule type. In the Schedule expression* field type rate(5 minutes) and this will trigger the function every 5 minutes. Check in Enable trigger and then click Add button.

Note: Here is a list of Schedule expression.

You have created a trigger and now you can click the trigger CloudWatch Events to see more details. The CloudWatch Events will display below with the rule name and you can Enable/Disable the trigger. Also you can delete the trigger.

Now you know how to create a simple trigger that execute at a certain time interval. 🕒

Tutorial is over! Now you can create a Lambda Function in your cloud9 environment and set up a simple time event trigger. Make sure to delete your environment and lambda function to not run up your bill. Hopefully this introductory article made it easier to use AWS Services and you get to continue your learning with AWS. For any questions or mistake, please reach out to get it fixed. 😉

This article will eventually become part of a series of articles from lessons I learned with my team while working on a project called Cryptolytic (article will be written about this soon and linked here). It’s the guide we wish we had when working on the project.

The notebook containing all the code used in this article can be found here which is inside the repo of our Cryptolytic project — so if you’re curious, check it out!

Find me on Twitter @malexmad, Github, or connect with me on LinkedIn!

--

--