AWS Lambda S3 Based URL Shortner using Serverless

Why we choose S3 and Lambda

Let me start with our use case. We usually send couple of hundreds of million emails monthly. We used our own short URL to keep the URLS clean. We developed this redirection using a simple PHP script with MySQL on back of that.

For example someone type

im.co/trial

Script will check database and query the corresponding URL and redirect the end user to there. But we have seen that when large campaigns are happening, this is making our MySQL usage very high.

Then you may think about why can’t we use a NoSQL data store like DynamoDB. Well, still we end up paying more in this case too.

So we end creating Lambda function in AWS using Node.js and store all these redirections as a file in an S3 bucket.

Since AWS does not charge us for same region data transfer, For 2.5Million S3 requests we barely need to spend $1 only. So we opt this way.

Now we can handle 5 Million requestes under $3.

You can see the code for this Lambda function here.

https://github.com/vimson/url-shortner-s3

Steps to setup this Lambda functions are

Step1

git clone https://github.com/vimson/lambda-url-shortner-s3 .

Step2

copy the configuration variables to .env and use proper credentials for S3 API access

cp sample.env .env

Step3

Install node dependencies like aws-sdk, dotenv etc

npm install

Step4

Create a file in the S3 with same name of the short url and extension as .txt. For example if i would like to redirect im.co/register, then create one file with register.txt in the S3 bucket specified in the .env configuration file.

Step5

You can test the Lambda function locally using sls offline plugin

sls offline

Step6

You can deploy this into AWS Lambda using the following command

serverless deploy --aws-profile <profile_name>

Step6

You can remove this application from AWS Lambda using the following command

serverless remove --aws-profile <profile_name>