Unleashing the Power of the Cloud (AWS): The Epic Journey of the Cloud Resume Challenge-
Part 5: Lambda almost drove me mad, but I conquered it! — The Cloud Resume Challenge continues.

Steve Murimi
4 min readAug 1, 2023

--

AWS Cloud resume challenge.

Lambda

To utilize Lambda, we need to create a function.

NB: Make sure to enable Function URL, this will give us a URL which we can use to activate the function.

Lastly, ‘Tags’ are essential!

At this point we now need to trigger a ‘POST’ function to increment the webpage view count every time the webpage is loaded and a ‘GET’ function so as to display an updated number of visitors as per the requirements of the challenge.

I considered dividing the two functions which would definitely help in decoupling the application and possibly result in a faster response rate in running the lambda code. However, this is a very simple lambda and therefore the cost of dividing the functions would be minimal and possibly negatively impact the utility of the functions. The function below runs in about 300ms which was acceptable.

Below is an example of the code you could use in your lambda function to get and receive data from your dynamoDB table.

import boto3
import json

# DynamoDB Table name
TABLE_NAME = '<cloud-resume-challenge-table>'

# Creating the DynamoDB Table Resource
dynamodb = boto3.resource('dynamodb', region_name='<insert region here>')
table = dynamodb.Table(TABLE_NAME)

# Lambda function handler
def lambda_handler(event, context):
# Get the item from DynamoDB
response = table.get_item(
Key={
'id': '0'
}
)

# Check if the item exists in DynamoDB
if 'Item' in response:
item = response['Item']
views = item.get('view_count', 0)
views += 1

table.update_item(
Key={
'id': '0'
},
UpdateExpression='SET view_count = :val1',
ExpressionAttributeValues={
':val1': views
}
)

return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
},
'body': json.dumps({'Visit_Count': str(views)})
}
else:
return {
'statusCode': 404,
'headers': {
'Content-Type': 'application/json',
},
'body': json.dumps({'error': 'Item not found'})
}

API Gateway.

We can now setup API Gateway to act as a go between our webpage and lambda to trigger our newly created function.

In the lambda console, create a REST-API with a Get method that points to the lambda function. Enable Lambda proxy integration and CORS to ensure smooth sailing, once this is done deploy the API to the stage name of your choice such as ‘Prod’.

API deployments steps:

Because the API will be linked to our lambda function, one can copy the lambda functions arn and proceed with the rest of the steps on the API console.

Insert the create details as below:

Create a method(GET):

Upon creating the GET method, you will get output similar to the following:

Once done, confirm that your lambda code appears as intended and configure a test.

Test configuration:

Test deployment:

Once your test is succesful, take a break, unwind and celebrate having conquered API gateway and Lambda.

(PS: Going for a swim aint a bad idea!)

Photo by Stas Ostrikov on Unsplash

See you on the next one as we dive into Unleashing the Power of the Cloud (AWS): The Epic Journey of the Cloud Resume Challenge-
Part 6: Route53 and ACM. The Cloud Resume Challenge continues.

--

--