Simplifying Serverless Development with AWS Lambda: A Step-by-Step Guide
IT Tips & Insights from our team members
When a new project starts, the developer thinks about the technology, the database and everything he can use to develop. But, when we need to define infrastructure our problems start, whether thinking about the architecture or the cost.
With the cloud, the cost of buying and sizing the server is no longer a problem, everything is charged according to use, however, the architecture is 2 servers, loadbalance, disponibility, growth and decrease when needed and more, more, more. Improving this comes the lambda that runs our code and only charges you for what is actually used and that’s it, now you choose the language, use the RDS and that’s it.
And it’s much simpler than you might think, the code is in this link, and the beginning below:
To start the service starts with creating a HandleClass and it implements RequestHandle, you define the Request and the Response and go.
This example just returns how many characters this word has.
In Response Class I created a toString() method just to return the value
And just add dependencies to lambda (of course, you will add others for real projects)
With your project running, you just create the jar file with command: maven clean package.
Ok, first part done.
Now, go to aws console -> lambda -> create new.
Choose Author from scratch to push your jar file.
Put the app name and language/version you will use, click create.
After create choose upload from .zip or .jar file
After upload, configure the class and method you will call.
After configure you can test:
Enter Json and click Test:
You will get the response and can see the logs:
Ok, after your lambda worked, you need create an endpoint to it, you do it using API Gateway.
Go to this service and create REST API
- Create the API.
- Actions -> Create Method
3. Choose POST and click ok.
4. Include lambda function name (as you add before)
5. Save and now you can test:
6. Down to request Body field, include it and press test
7. Search for the response in top of the page:
8. Now you have your API gateway working, and an endpoint to it.
Deploy the API
9. Choose the stage name and click ok.
10. Now you have endpoint to access, just copy and you can use from postman:
11. Set the body and enjoy
The code is on this git: https://github.com/fbeli/lambda_basic
Software Engineer since 2007