-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
47 lines (35 loc) · 1.21 KB
/
index.php
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
43
44
45
46
47
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\Marshaler;
use Symfony\Component\Yaml\Yaml;
return function ($event) {
$config = Yaml::parseFile('config.yml');
$dynamoClient = new DynamoDbClient([
'region' => $config['AWS_REGION'],
'version' => $config['AWS_VERSION']
]);
// connect to DynamoDB, add the record, and remove from SQS
if (!empty($event['Records'])) {
foreach ($event['Records'] as $value) {
try {
$marshaller = new Marshaler();
$item = $marshaller->marshalJson(json_encode($value));
$params = [
'TableName' => $config['AWS_DYNAMODB_TABLE_NAME'],
'Item' => $item
];
$response = $dynamoClient->putItem($params);
echo "Added item!\n";
} catch (DynamoDbException $e) {
echo "Unable to add item:\n";
echo $e->getMessage() . "\n";
}
}
} else {
echo "No message in queue. \n";
}
return "Message moved to DynamoDB!";
};