MCPcopy Create free account
hub / github.com/aws-samples/aws-cdk-examples / create_short_url

Function create_short_url

python/url-shortener/lambda/handler.py:32–60  ·  view source on GitHub ↗
(event)

Source from the content-addressed store, hash-verified

30
31
32def create_short_url(event):
33 # Pull out the DynamoDB table name from environment
34 table_name = os.environ.get('TABLE_NAME')
35
36 # Parse targetUrl
37 target_url = event["queryStringParameters"]['targetUrl']
38
39 # Create a unique id (take first 8 chars)
40 id = str(uuid.uuid4())[0:8]
41
42 # Create item in DynamoDB
43 dynamodb = boto3.resource('dynamodb')
44 table = dynamodb.Table(table_name)
45 table.put_item(Item={
46 'id': id,
47 'target_url': target_url
48 })
49
50 # Create the redirect URL
51 url = "https://" \
52 + event["requestContext"]["domainName"] \
53 + event["requestContext"]["path"] \
54 + id
55
56 return {
57 'statusCode': 200,
58 'headers': {'Content-Type': 'text/plain'},
59 'body': "Created URL: %s" % url
60 }
61
62
63def read_short_url(event):

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected