Develop, Test and Deploy a Serverless App using Cloud9

What is AWS Cloud9?

AWS Cloud9 is a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with just a browser. It includes a code editor, debugger, and terminal. Cloud9 comes prepackaged with essential tools for popular programming languages, including JavaScript, Python, PHP, and more, so you don’t need to install files or configure your development machine to start new projects. Since your Cloud9 IDE is cloud-based, you can work on your projects from your office, home, or anywhere using an internet-connected machine.

Cloud9 also provides a seamless experience for developing serverless applications enabling you to easily define resources, debug, and switch between local and remote execution of serverless applications. With Cloud9, you can quickly share your development environment with your team, enabling you to pair program and track each other’s inputs in real time.

Shown below is a screenshot of an environment sk_udacity that I created using the defaults, i.e EC2 instance.

The power of Cloud9 is that it is fully integrated with all the AWS resources. For instance, we can directly run S3 commands from the terminal.

The second most useful feature of Cloud9 is that we can build Lambda Functions with just a few clicks.

aws_cloud9

What is AWS Lambda?

AWS Lambda is a new category of computing where business logic can be directly deployed to a production environment without thinking about servers. Another word for this is serverless or FaaS (Function As A Service).

Some of the benefits of AWS Lambda include:

  1. no servers to manage,
  2. continuous scaling, and
  3. billing at the sub-second level.

Another benefit of AWS Lambda includes being part of an ecosystem designed to exploit the capability. Here are two simple examples of how AWS Lambda could be used.

  • The first example is AWS Step Functions - they build upon AWS Lambda to create more sophisticated workflows like polling for a job to finish and performing an action.
  • The second example is AWS DeepLens (a computer vision device) which uses AWS Lambda to serve out predictions.

Perhaps the biggest benefit of AWS Lambda is that almost every service on AWS can utilize AWS Lambda. The AWS ecosystem allows Lambda functions to respond to events instead of constantly running. This is similar to a motion detector that turns on a light in the garage. The duration the light may run for could be only a few hours per year. If the light switch was manually triggered it is possible the light would be run constantly for 365 days in the year.

The AWS Lambda ecosystem allows integration with other core services in AWS through triggers. An example of a trigger would be a movie file that is uploaded to Amazon S3 storage. An AWS Lambda function could be triggered that uses an AWS API to transcode the movie file to a different code or to add captioning.

Create your first AWS Lambda function inside Cloud9

In the AWS Resources section click on the AWS Lambda Icon. Specify the name of the function, eg: sk_hello.

aws_lambda1

In the next section, I decide if I want to do a JavaScript or Python. Choose Python 3.6.

aws_lambda2

Now, we have two options, we can either do a Function trigger - that does basically nothing and I can just execute it without an API, or we can choose API gateway. API Gateway basically turns it into a full web service that underneath the hood is just running the Python Function.

In the next screen, the Role determines if this function can access a database or an S3 filesystem.

aws_lambda3

There we go, our serverless application is now ready.

aws_lambda4

This will take you to the lambda function where you can enter the code for the lambda_handler function. In this case, I am simply printing out the event. Hit run and in the right side, you can provide a payload, which will be returned as a Response.

aws_lambda5

Deploying a Serverless application

As you can see with just a few clicks you can have a Serverless application up and running. The next step is also quite simple, we can deploy this into Amazon Cloud. Right click on the function and hit deploy. What it does is that it takes that whole directory skhello and it is deploying that to AWS cloud.

aws_lambda6

Now, we can go back to AWS and check out that function.

aws_lambda7

Click on the function to see more details, here you can see that the Cloud9 IDE is displayed here, we can if we so desire edit the code here, but lets not do that here, click on Tests - this will let us configure a test event for our function.

aws_lambda8

Hit Test, this will execute the test and display the output.

aws_lambda9

So in a nutshell, we started with Cloud9 and created a Lambda function, Deployed the function and Tested the function, Monitored the logs all within the same eco-system of tools. So this is really the power of doing something that’s Cloud Native. That is, we developed in the cloud, tested it in the cloud, deployed it in the cloud, and maintain it in the cloud. So, this illustrates all of the advantages of a Cloud-Native system.

Operationalizing AWS Lambda

Continuing with the analogy of a motion-sensing light in the garage, imagine if there were many things that could turn on the light in the garage. What are some triggers that could be set up to turn on the light in the garage?

  • Opening of the garage door
  • Home burglar alarm
  • A nightly timer that starts at 8 PM and ends at 8 AM

Just like the home automation example, there are many ways to trigger a Lambda function. One of the most common ways to trigger the Lambda function would be through API Gateway. A web service is created by adding API Gateway to the lambda, and then, if the URL is requested by a browser or an API call, it would, in turn, trigger the Lambda function.

Another common way to trigger the AWS Lambda function would be to attach it to Amazon S3. Instead of the Lambda function being triggered from the web service, it could be triggered every time a new file is placed in an S3 bucket.

Yet another trigger could be a CloudWatch Event Timer. A timer could be set up to run every hour. At the top of each hour, the Lambda function would be called.

Use Case: Let’s get into a real-world example now. A company that needs to collect a competitor’s pricing, say for a similar washing machine they are selling, could create a Lambda function that scrapes the competitor’s website. Next, a CloudWatch Event timer could run nightly to scrape the website and put the results into an S3 bucket. When the S3 bucket receives the HTML results, a second AWS Lambda function could be triggered that extracts the pricing information from the HTML file and writes it to DynamoDB, if it is lower than the current value in the database. Finally, the website itself could host a third AWS Lambda function that uses API Gateway to serve out the companies current prices, which will always be at least the same price or lower than their competitors.

Here’s a snapshot of the triggers that could be added to the Lambda function.

aws_lambda10