DEV Community

Dhruv
Dhruv

Posted on • Originally published at Medium on

Writing my first AWS Lambda Function

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.

Lambda Console Page

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.

Create a Lambda Function Configuration

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.

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!

Test your Amazon AWS Lambda Function

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.

Adding a trigger for Lambda Function

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.

Amazon API Gateway Configuration

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

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.

Creating a method in Amazon API Gateway

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.

Monitoring in AWS Lambda

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

Discussion (0)