Creating your first Lambda function

Once your new environment shows up, create a new Lambda function by clicking on the AWS Resources:

AWS-Cloud9-Lambda-ApiGateway-5.1

When the side bar shows, click the Lambda sign with the plus sign:

AWS-Cloud9-Lambda-ApiGateway-6

A new dialogue shows up for creating a new serverless application. Fill in the function name and the application name:

AWS-Cloud9-Lambda-ApiGateway-7

The next screen shows what framework and template you want to use for your serverless application. By default, Cloud9 provides templates only for node.js and Python and only up to some specific versions. Other frameworks and versions can also be setup but that is outside the scope of this tutorial.

For this tutorial we are going to choose an empty node.js function with node.js version 6.10.

AWS-Cloud9-Lambda-ApiGateway-8

In the next screen, we have options to choose either no function trigger or Api Gateway. We will choose Api Gateway, this way we will integrate our Lambda function to an Api Gateway resource so this API can be consumed by clients.

AWS-Cloud9-Lambda-ApiGateway-13

After choosing API Gateway, more options will appear:

AWS-Cloud9-Lambda-ApiGateway-12.3

Provide a resource path. Resource path identifies this resource specifically and is postfixed with the API’s base URL, which consists of the API Gateway API’s endpoint and a stage name(whichever stages you choose to create). This base URI is called the Invoke URI. This base URI can also be your custom domain name. The API Gateway’s base address will look something like {api-id}.execute-api.{region}.amazonaws.com.

Remember that / is the root address for your API. If you want, you can choose / to integrate this Lambda function with the root API resource. All the resources are children to the root resource.

For the security mechanism, we are going to choose None, which means this resource is available to everyone publicly. Other options such as AWS authentication are also available but we won’t go into them for this tutorial.

Next up, choose the maximum amount of memory that you want to allow your function to be able to consume.

AWS-Cloud9-Lambda-ApiGateway-10

Secondly, you have the option to either create a new role for the function or use an existing one. If you choose Automatically generate role, then a ew role will get generated with AWSLambdaBasicExecutionRole and has permissions to write to CloudWatch logs. So if you are choose to use an existing role, it is good to have a role that has permission to write to the CloudWatch logs and has AWSLambdaBasicExecutionRole.

Review the settings and click Finish.

AWS-Cloud9-Lambda-ApiGateway-11

You have successfully created a new AWS Serverless application.

What AWS resources are generated?

Before going into the development environment, it’s important to mention that when we create a new serverless application, AWS uses CloudFormation to create the serverless application, the Lambda function and other resources(IAM roles, EC2 instance etc).

So let’s take a look at what resources are generated when we create a new AWS serverless application.

CloudFormation Stack

As mentioned above, a new CloudFormation stack is generated. You can see it in the CloudFormation dashboard:

AWS-Cloud9-Lambda-ApiGateway-17

IMPORTANT: If you ever want to delete your serverless application, don’t do it directly from the AWS Lambda console as it will not delete other related resources. So always delete your Lambda serverless applications from the CloudFormation dashboard.

IAM Role

Next up, an IAM role is generated(if you selected Automatically generate role in the dialogue mentioned earlier).

AWS-Cloud9-Lambda-ApiGateway-16

Lambda Function

In your AWS Lambda console, you will be able to see something like cloud9-YourApplicationName-YourLambdaFunctionName-SomeId.

AWS-Cloud9-Lambda-ApiGateway-18

Clicking on it will take you to details and will also show the resources related to this Lambda function:

AWS-Cloud9-Lambda-ApiGateway-19

And also a code editor where you can edit the Lambda function (in case you are not using Cloud9). We won’t be using this code editor as we will be using Cloud9 for development instead:

AWS-Cloud9-Lambda-ApiGateway-20

API Gateway

As we chose to integrate the Lambda function with the API Gateway, an API Gateway resource is also created in your API Gateway console:

AWS-Cloud9-Lambda-ApiGateway-21

In the next post, we will see how to work with AWS lambda functions in Cloud9.

Cloud9

AWS-Cloud9-Lambda-ApiGateway-12.png

Environment (Left Side Panel)

The left side panel shows your local environment(local in regard to your EC2 instance). All of these files reside on your EC2 instance and are not published to Lambda until you deploy them(which we will see how later in this post).

Here you see a folder for your application and inside it a folder for your function. One application can contain as many functions as you like.

The application contains some other files apart from the Lambda function folder.

  1. application.json: Used by AWS Cloud9, containing settings specific to the serverless application in JSON format.
  2. .gitignore: Files to ignore in case you are using Git as your version control system.
  3. template.yaml: An AWS SAM template file that contains information about the AWS Lambda functions and other related resources. Whenever you want to deploy your local changes to Lambda, AWS Cloud9 calls AWS SAM to use this template to do the upload.

The Lambda function’s folder, in this case WaqasTestLambdaFunction, contains an index.js file (as we use node.js in this case).

AWS Resources (Right Hand Panel)

On the right hand side bar, there are multiple tabs. Click AWS Resources and you will see the local and remote Lambda functions. When you create a new Lambda function in your environment, it will appear in the Local Functions list, and when you deploy it, it will also show up in the Remote Functions section as well, with a name prefixed with cloud9 and postfixed with an ID.

We will learn how to write, edit and test Lambda function with API Gateway integrations in Cloud9.