Skip to main content

URL Shortener with DocumentDb(MongoDB Compatible), Lambda and CDK

Create and Deploy Simple URL Shortener with AWS CDK and DocumentDB

Create and Deploy Simple URL Shortener with AWS CDK and DocumentDB

In this tutorial we will walk you through creating and deploying a simple URL shortener application using AWS CDK, DocumentDB, Lambda and…

Create a Simple URL Shortener with AWS CDK and DocumentDB

In this tutorial, we will walk you through creating and deploying a simple URL shortener application using AWS CDK, DocumentDB, Lambda and API Gateway. DocumentDB is a document database which is fully compatible with MongoDB 3.6 API. DocumentDB takes lots of inspiration from other AWS Database Services, in which storage and compute layers are separated. This allows fairly simple scalability and high-performance throughput. CDK is a new way to manage AWS resources using common programming languages.

Let’s get started.

In case you haven’t installed AWS CDK yet, here is how to do it.
npm install -g aws-cdk

Let’s build the application logic first

This lambda function will handle requests to API Gateway and create an item in the database with generated short id. We will also save source IP address to the database.

Here we are getting the value from the path parameters and we will query the database searching for that value. If none found we are returning 404 response. If the query returns something, we simply redirect requests to target URL using the API Gateway response.

Since TLS is enabled by default, you will need a public key for interacting with DocumentDB instances.
https://docs.aws.amazon.com/documentdb/latest/developerguide/getting-started.connect.html
Download the key and put it inside src folder without changing the filename(rds-combined-ca-bundle.pem) or content.

Application logic is all set, let’s move onto provisioning AWS resources

We are creating a VPC with two private and one public subnet, a security group, DocumentDB cluster&instance and two Lambda functions and API gateway REST API resource. We pass the database URL and database name via environment variables to Lambda functions. At the end API Gateway will print out the endpoint for our API Gateway resource. We will also print out the database URL.

Now it’s time for deployment

# Install dependencies
npm install
# Edit .env file for environment variables
vim .env
# Install dependencies and build the source code for Lambda functions
cd src && npm i && npm run build
# Deploy using CDK CLI
cd .. && cdk deploy

You should see an output similar to this after a few minutes of provisioning resources.

Now we can try the app logic. We need to do a POST request to /urls API resource. We will get the short version of it afterwards if the request is successful.

All good, visit the endpoint shortURL via browser and you should be re-directed to StackExchange question.

Don’t forget to delete resources after you are done with the application.

cdk destroy


Popular posts from this blog

Concurrency With Boto3

Concurrency with Boto3 Concurrency with Boto3 Asyncio provides set of tools for concurrent programming in Python. In a very simple sense it does this by having an event loop execute a… Concurrency in Boto3 Asyncio provides a set of tools for concurrent programming in Python . In a very simple sense, it does this by having an event loop execute a collection of tasks, with a key difference being that each task chooses when to yield control back to the event loop. Asyncio is a good fit for IO-bound and high-level structured network code. Boto3 (AWS Python SDK) falls into this category. A lot of existing libraries are not ready to be used with asyncio out of the box. They may block, or depend on concurrency features not available through the module. It’s still possible to use those libraries in an application based on asyncio by using an executor from concurrent.futures to run the code either in a separate thread or a separate process. The run_in_executor() method of the event...

AWS Lambda Function URLs

AWS Lambda Function URLs AWS Lambda Function URLs AWS Lambda is a Serverless computing service offered by Amazon Web Services (AWS) that allows developers to run code without provisioning… AWS Lambda Function URLs AWS Lambda AWS Lambda is a Serverless computing service offered by Amazon Web Services ( AWS ) that allows developers to run code without provisioning or managing servers. In this tutorial, we will explore AWS Lambda Function URLs , which are the endpoints that allow you to invoke your Lambda functions. AWS Lambda Function URLs are unique HTTP endpoints that you can create using AWS Console, SDK or any other IaC tool. These URLs are used to trigger your Lambda function, and they can be integrated with a variety of workloads. Function URLs are dual stack-enabled, supporting IPv4 and IPv6. After you configure a function URL for your function, you can invoke your function through its HTTP(S) endpoint via a web browser, curl, Postman, or any HTTP client. Once you create ...

DNS Failover with Route53

DNS Failover with Route53 DNS Failover with Route53 Route 53‘s DNS Failover feature gives you the power to monitor your website and automatically route your visitors to a backup site if it… DNS Failover with Route53 Route 53 ‘s DNS Failover feature gives you the power to monitor your website and automatically route your visitors to a backup site if the main target is not healthy. To showcase this feature, we are going to deploy an application, which we built in this blog post , to two different AWS regions. We are also going to set active-passive failover in Route53, then we will remove the application from one region and we’ll observe how DNS queries will react to the changes. AWS describes the failover scenarios in 3 different categories Active-passive : Route 53 actively returns a primary resource. In case of failure, Route 53 returns the backup resource. Configured using a failover policy. Active-active : Route 53 actively returns more than one resource. In case of failure...