import boto3
from boto3.dynamodb.conditions import Key
from boto3.dynamodb.types import TypeDeserializer
import json
print('Loading function')
tableName = 'fizzbuzz'
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(tableName)
queueUrl='https://sqs.us-east-1.amazonaws.com/478193779867/FizzBuzzQ'
sqs = boto3.client('sqs')
deserializer = TypeDeserializer()
def lambda_handler(event, context):
'''Demonstrates a simple HTTP endpoint using API Gateway. You have full
access to the request and response payload, including headers and
status code.
To scan a DynamoDB table, make a GET request with the TableName as a
query string parameter. To put, update, or delete an item, make a POST,
PUT, or DELETE request respectively, passing in the payload to the
DynamoDB API as a JSON body.
'''
output = table.get_item(
Key={
'key' : "fizzBuzzNTimes",
}
)
n = int(str(output['Item']['number']))
#n = deserializer.deserialize(output) # ['Item']['number']
print(n)
for i in range(1, n+1): # make output
sqs.send_message(QueueUrl=queueUrl, MessageBody=str(i))
return output
console.log('Loading function');
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
ScanCommand,
PutCommand,
GetCommand,
DeleteCommand,
} from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const dynamo = DynamoDBDocumentClient.from(client);
const tableName = "fizzbuzz_output";
export const handler = async (event) => {
let update = {}
//console.log('Received event:', JSON.stringify(event, null, 2));
for (const { messageId, body } of event.Records) {
console.log('SQS message %s: %j', messageId, body);
let num = body;
let output = "";
if (num === 0) {
output = "0";
}
else if (num % 3 === 0 && num % 5 === 0) {
output = "fizzbuzz";
}
else if (num % 3 === 0) {
output = "fizz";
}
else if (num % 5 === 0) {
output = "buzz";
}
else {
output = num;
}
update[num] = output
await dynamo.send(
new PutCommand({
TableName: tableName,
Item: {
"key": num,
"value": output
}
})
);
} // end for loop
console.log(update)
return `Successfully processed ${event.Records.length} messages.`;
};