Serverless Url Shortener apiGW Lambda dynamoDb
Build and deploy serverless short url project with apiGW, Lambda, and dynamoDb from scratch

Devops | 3x AWS Certified | CKA
Search for a command to run...
Build and deploy serverless short url project with apiGW, Lambda, and dynamoDb from scratch

Devops | 3x AWS Certified | CKA
No comments yet. Be the first to comment.
Introduction If you are moving to AWS Identity Center (formerly SSO) and want to keep your Google Workspace users and groups in sync, ssosync is the go-to tool. However, setting up the permissions can be tricky. If you’ve seen the error unauthorized_...

Introduction In this tutorial, I will guide you through establishing a SAML connection between Google Workspace and AWS Identity Center. Later, you will synchronize users from Google Workspace using SCIM. To verify that everything is configured corre...

Implementing Gang Scheduling for Distributed GPU Training on Amazon EKS

Quick Solution If you're in a hurry to solve deployment time issues, simply add the WEBSITE_RUN_FROM_PACKAGE environment variable to your Azure Web App settings and set its value to 1. This change can significantly reduce your deployment times. W...

General DevOps logic behind jiocinema


import json
import boto3
import string
import random
dynamodb = boto3.resource('dynamodb')
table_name = 'url-shortener-table'
table = dynamodb.Table(table_name)
def lambda_handler(event, context):
print(event)
http_method = event['requestContext']['http']['method']
if http_method == 'POST':
body = json.loads(event['body'])
long_url = body['long_url']
short_url = generate_short_url()
table.put_item(Item={'short_id': short_url, 'long_url': long_url})
response = {
'statusCode': 200,
'body': json.dumps({'short_url': short_url})
}
elif http_method == 'GET':
short_url = event['rawPath'][1:]
response = table.get_item(Key={'short_id': short_url})
if 'Item' in response:
long_url = response['Item']['long_url']
response = {
'statusCode': 301,
'headers': {
'Location': long_url
}
}
else:
response = {
'statusCode': 404,
'body': json.dumps({'error': 'Short URL not found'})
}
return response
def generate_short_url():
characters = string.ascii_letters + string.digits
short_url = ''.join(random.choice(characters) for _ in range(3))
return short_url
NOTE : Replace table_name with your dynamoDb table name once we create it later in this steps . and partitionKey should be short_id
url-shortener-table with short_id as the partition key and long_url as an attribute.Step 3: Create an API Gateway:
Lets create api first and later we can do configuration
Open the API Gateway console.
Click on "Create API" and select "HTTP API". Review and create api

Once we created API Define routes for POST and GET requests.

Select create and keep route empty and method POST then select create
ref following image

once created select POST like this

click on attach integration and go ahead with option
create and attach integration
Next select integration type as lambda function and then select lambda function created earlier for this task
Make sure this option is enabled and go ahead with create option

Cool we just created our first route , lets go and create second route which is GET
for redirecting shorturl we got in previous response
Lets create second route , now go back to Routes and select create


Make sure its /{short_id}
Now select GET Method attach integration and click on attach just right side option

From this option you can get your APIGW url to make request

These are the curls to test Replace url with actual url
Create Short Url : POST
curl --location 'https://4tr7c6bru76e.execute-api.us-east-1.amazonaws.com' \
--header 'Content-Type: application/json' \
--data '{"long_url": "https://cloudcdk.com"}'
Access Short Url : GET
curl --location 'https://4tr7c6bru76e.execute-api.us-east-1.amazonaws.com/{Replace_with_id_u_get_in_response_for_above_post_request}'
Follow me on Twitter for more : Twitter