Using AWS Lambda[1] — Setup

Rahul Tripathi
The Startup
Published in
4 min readSep 7, 2020

Before getting started with lambda setup lets take a look at our use case and how we landed on serverless as a solution to our problem .

Problem statement :- Every product has its website and so did ours but it wasn’t accessed very frequently as majority of our traffic was on the mobile application , but sometimes we witnessed traffic surge .

Solution :- To serve the Rest APIs required by the website , provisioning a server was not the best solution as it would require more maintenance and we had to pay even if there was no traffic

[ENTER SERVERLESS]

Using serverless functions we could easily serve the needs of the website without worrying much about the provisioning , maintenance of the server and also paying only for what we use

Summary :- This is why we chose serveless as a solution to our problem but this is not the case every time as lambda itself has some drawbacks and in a few cases provisioning a server is better , so choose wisely

Ingredients :

To build a serverless backend you’ll need ,

Made by me on onenote

Recipe :

Setting up aws lambda is very easy , all you need is good code . You can find awesome guides on getting started with serverless and that should help you set up a basic hello world “ function . I’ve listed down a few steps incase you need them

these steps are too simple

And here is a sample serverless.yml file to define your function

serverless.yml

But wait , you don’t run hello world “ in production ? The basic example has some drawbacks if you're running it in production

  • You need to zip the code every time and update it through the lambda console , which is not considered a very good practice
  • If you’ve multiple event triggers , you’ll have to set them up and update them manually

[ENTER SERVERLESS APPLICATION MODEL (SAM)]

Sam is a service provided by AWS to deploy serverless applications with ease , Sam takes care of provisioning and updating every service related to the service definition . SAM is basically a wrapper around Cloudformation , if you’ve used that , Also it makes local development a lot easy . All you need is a template.yml file.

And now you can use simple commands to deploy , test your function . Before starting make sure you’ve properly configure AWS-cli with proper roles

  • Build
sam build
  • Deploy
sam deploy --guided
  • Test
sam local invoke

to test using the api gateway

sam local start-api

Getting started with sam might take a while if you’re not familiar with cloudformation , but it’ll make developing and deploying lambda easier

Considerations to keep in mind while using Lambda Functions

  • Lambda functions do not have a static ip
  • You need to configure lambda to be accessible in VPC
  • You cannot access the Internet from Lambda , for that you’ll need to add a NAT gateway to your VPC

Using RDS In Lambda :

If you want to access RDS from lambda , it is highly recommended that you use RDS Proxy (https://aws.amazon.com/rds/proxy/) to connect to database . RDS Proxy will manage a pool of warm connections so that when functions scale , they do not overload your db by eating up all the connections also make sure your lambda is in the same VPC .

Accessing S3 from Lambda :

to access s3 from lambda , you need to add a VPC Endpoint (https://docs.aws.amazon.com/vpc/latest/userguide/vpce-gateway.html) to s3 in your VPC , you cannot directly access s3 from lambda

--

--