Skip to content

Instantly share code, notes, and snippets.

@akiltipu
Created July 12, 2023 06:32
Show Gist options
  • Save akiltipu/399788037680b3a93e5adcac05210046 to your computer and use it in GitHub Desktop.
Save akiltipu/399788037680b3a93e5adcac05210046 to your computer and use it in GitHub Desktop.
Updated AWS Lambda Code for building End-to-End Web Application with AWS tutorial.
# import the JSON utility package
import json
# import the Python math library
import math
# import the AWS SDK (for Python the package name is boto3)
import boto3
# import two packages to help us with dates and date formatting
from time import gmtime, strftime
# create a DynamoDB object using the AWS SDK
dynamodb = boto3.resource('dynamodb')
# use the DynamoDB object to select our table
table = dynamodb.Table('PowerOfMathDatabase')
# store the current time in a human readable format in a variable
now = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
# define the handler function that the Lambda service will use an entry point
def lambda_handler(event, context):
# extract the two numbers from the Lambda service's event object
mathResult = math.pow(int(event['base']), int(event['exponent']))
# write result and time to the DynamoDB table using the object we instantiated and save response in a variable
response = table.put_item(
Item={
'id': str(mathResult),
'LatestGreetingTime':now
})
# return a properly formatted JSON object
return {
'statusCode': 200,
'body': json.dumps('Your result is ' + str(mathResult))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment