Skip to content

A URL Shortener in AWS with the AWS CDK in Typescript - generated by GPT-4

Notifications You must be signed in to change notification settings

WooodHead/gpt-url-redirect

 
 

Repository files navigation

URL Shortener

This URL Shortener application is built with the AWS Cloud Development Kit (CDK) in TypeScript. It provides a simple RESTful API to create and manage short URLs that redirect to original URLs. The prompts which were used to generate this.

Screenshot

Architecture

The application consists of two AWS Lambda functions and an Amazon DynamoDB table, exposed via an Amazon API Gateway REST API.

  • Create Short URL Lambda: Processes incoming requests to create a short URL for a given original URL.
  • Redirect Short URL Lambda: Processes incoming requests to redirect from a short URL to its corresponding original URL.
  • Amazon DynamoDB: Stores the mapping between short URLs and original URLs.

Backend

flowchart TD
    A[API Gateway] -->|POST /create| B1[Lambda: CreateShortUrlHandler]
    A -->|GET /:shortUrl| B2[Lambda: RedirectToLongUrlHandler]
    B1 --> C[DynamoDB: UrlMappingsTable]
    B2 --> C

    subgraph Serverless Backend Stack
        A
        B1
        B2
        C
    end

Getting Started

Prerequisites

Deployment

  1. Clone the repository:
git clone https://github.com/your-username/url-shortener.git
cd url-shortener
  1. Install dependencies:
npm install
  1. Bootstrap the CDK app:
cdk bootstrap
  1. Deploy the URL Shortener application:
cdk deploy

The cdk deploy command will create and deploy the necessary AWS resources, including the Lambda functions, API Gateway, and DynamoDB table.

Usage

You can use any REST client (e.g., Postman, curl) to interact with the deployed API.

  • To create a short URL, send a POST request to the /create endpoint with a JSON payload containing the originalUrl. The API will return the short URL:
curl -X POST https://your-api-id.execute-api.your-region.amazonaws.com/prod/create \
  -H "Content-Type: application/json" \
  -d '{"originalUrl": "https://www.example.com"}'

To use the short URL, send a GET request to the short URL path:

curl -X GET -L https://your-api-id.execute-api.your-region.amazonaws.com/prod/{shortUrl}

The API will redirect you to the corresponding original URL. Note the -L flag is used to follow the redirect.

Clean Up

To remove the deployed resources, run the following command:

cdk destroy

About

A URL Shortener in AWS with the AWS CDK in Typescript - generated by GPT-4

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 81.7%
  • HTML 9.3%
  • JavaScript 8.7%
  • CSS 0.3%