-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda_function.py
42 lines (39 loc) · 1.42 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import json
import boto3
dynamodb = boto3.client('dynamodb')
table_name = 'visitor_count'
def lambda_handler(event, context):
if event['httpMethod'] == 'OPTIONS':
# Handle preflight request
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type,Authorization'
}
}
if event['httpMethod'] == 'GET':
try:
# Increment the visitor count in DynamoDB
response = dynamodb.update_item(
TableName=table_name,
Key={'CounterID': {'S': '1'}},
UpdateExpression='SET VisitorCount = VisitorCount + :val',
ExpressionAttributeValues={':val': {'N': '1'}},
ReturnValues='UPDATED_NEW'
)
updated_count = response['Attributes']['VisitorCount']
# Return the numeric value directly in the response body
return {
'statusCode': 200,
'body': json.dumps(updated_count),
'headers': {
'Content-Type': 'application/json'
}
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}