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.
- API Gateway
- AWS IoT
- Application Load Balancer
- CloudWatch Events
- CloudWatch Logs
- Cognito Sync Trigger
Here I will discuss 3 of these which I have used until now.
Login to your AWS Console. Within the console, you can find AWS Lambda under Services which takes you to the Lambda console.
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.
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.
AWS gives me 3 options to write my code
- Using the inline code editor (Recommended if your code is short and uses only amazon dependencies like S3)
- Uploading a zip folder (Recommended if you are using non-amazon dependencies)
- 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!
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.
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.
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.
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.
Another good thing about Lambda (AWS in general) we can monitor various events like invocations, time durations, errors, etc.
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