Writing my first AWS Lambda Function
Tags: serverless, amazonwebservices, aws, lambda
Published: 2019-04-17 12:29:27

---
title: Writing my first AWS Lambda Function
published: true
tags: serverless,amazon-web-services,aws,lambda
canonical_url: 
---

First things first, Lambda functions are incredible!  
The best part about Lambda is that you only pay for the time the function is running which saves you a lot of money compared to running a 24x7 AWS EC2 instance.

To get a Lambda function working we need three things in place — A working code, a trigger, and a monitoring tool.

Trigger, as the word suggests is used to invoke the function. Currently, AWS has 12 ways to trigger any Lambda Function.

1. API Gateway
2. AWS IoT
3. Application Load Balancer
4. CloudWatch Events
5. CloudWatch Logs
6. CodeCommit
7. Cognito Sync Trigger
8. DynamoDB
9. Kinesis
10. S3
11. SNS
12. SQS

Here I will discuss 3 of these which I have used until now.

### Creating a Lambda Function

Login to your AWS Console. Within the console, you can find AWS Lambda under Services which takes you to the Lambda console.

![](https://cdn-images-1.medium.com/max/1024/1*-3QJmXZ-zf-R4HpFQgseHA.png)<figcaption>Lambda Console Page</figcaption>

This is what you’ll see if this is your first Lambda. Click on the **Create a function** button to start with your first function.

You’ll be taken to the setup page where you configure the function (name, runtime, role). You can create a Lambda from Blueprints or Serverless Application Repos. I used the **Author from scratch ** option.

![](https://cdn-images-1.medium.com/max/1024/1*Vm5Imz0IJQd3aiyh7ZuHLQ.png)<figcaption>Create a Lambda Function Configuration</figcaption>

After deciding on what to call my function I can choose my runtime, AWS has a lot of supported languages and versions (.Net, Go, Java, Node.js, Python, Ruby). I use Node.js 8.10 for most of my projects. You’ll have to create a new role if you don’t already have one.

#### Writing My Lambda Function Code

AWS gives me 3 options to write my code

1. Using the inline code editor (Recommended if your code is short and uses only amazon dependencies like S3)
2. Uploading a zip folder (Recommended if you are using non-amazon dependencies)
3. Uploading a file from S3 (Recommended if your zip \> 10MB)

After writing the code you can alter the configuration if you want. I had to increase the default timeout of 3s for my use case.

![](https://cdn-images-1.medium.com/max/1024/1*rInLTU7hG4MahUHOwSj5OQ.png)

Hit the **Save** button and boom your lambda function is ready!

Taking a TDD approach you can always test your code. You can also configure your test events!

![](https://cdn-images-1.medium.com/max/1024/1*unWFCBLcQutSL9eJstSeRQ.png)<figcaption>Test your Amazon AWS Lambda Function</figcaption>

### Creating a trigger

I have used API gateway and CloudWatch Events for all my projects until now. You can add a trigger by selecting your favorite option from the list when you open your function.

![](https://cdn-images-1.medium.com/max/1024/1*2olxdQhTkdttaUCjiWDJlg.png)<figcaption>Adding a trigger for Lambda Function</figcaption>

#### Configure API Gateway

You can select an existing API or you can configure a new. If this is your first time you can find AWS API Gateway under Services which takes you to the API Gateway console.

![](https://cdn-images-1.medium.com/max/1024/1*S9VG5lekB2vyrgtFISs9_A.png)<figcaption>Amazon API Gateway Configuration</figcaption>

Hit that **Get Started** button and it will take you to the configuration page. Choose the protocol, enter the name, description, etc and create your new API

![](https://cdn-images-1.medium.com/max/1024/1*LoXWAat-nk1iAjdXo-Lk3A.png)

Create a new method as per your use case and integrate it your lambda function. After saving you will get an endpoint for your REST API. Requesting this endpoint will trigger your lambda function.

![](https://cdn-images-1.medium.com/max/1024/1*Gn1jzj28S7klHAFs5DcQBA.png)<figcaption>Creating a method in Amazon API Gateway</figcaption>

#### Configure CloudWatch Events

You can select an existing role or you can create one. Creating a new rule requires you to enter a name, description, rule type, etc. In scheduled expressions, you can set up a Cron. All Cron expressions are in UTC.

> cron(0 17 ? \* MON-FRI \*)

this triggers your lambda function at 17:00:00 GMT Monday to Friday.

### Monitoring

Another good thing about Lambda (AWS in general) we can monitor various events like invocations, time durations, errors, etc.

![](https://cdn-images-1.medium.com/max/1024/1*Zi4xkPnse1F2QzZR782apw.png)<figcaption>Monitoring in AWS Lambda</figcaption>

CloudWatch Logs gives you insights about execution start, execution end, request length, memory used for _every_ execution.

If you’ve reached this far I hope you have fun exploring the serverless architecture :)


This post was originally published on [medium](https://medium.com/@nendhruv/writing-my-first-aws-lambda-function-2d4e5b0015fe)