-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
64 lines (52 loc) · 1.93 KB
/
index.php
File metadata and controls
64 lines (52 loc) · 1.93 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php declare(strict_types=1);
namespace AwsPhpLambdaSam;
require_once __DIR__ . '/vendor/autoload.php';
use Bref\Context\Context;
use Bref\Event\Handler;
use Bref\Monolog\CloudWatchFormatter;
use DateTimeZone;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
class LambdaHandler implements Handler
{
private Logger $logger;
public function __construct()
{
$this->logger = new Logger('aws-php-lambda-sam');
$this->logger->setTimezone(new DateTimeZone('Europe/Budapest'));
$this->logger->useMicrosecondTimestamps(true);
$handler = new StreamHandler('php://stderr', Level::Debug);
$handler->setFormatter(new CloudWatchFormatter());
$this->logger->pushHandler($handler);
}
public function handle(mixed $event, Context $context): array
{
$startTime = microtime(true);
$this->logger->info('AWS PHP Lambda SAM function started', [
'requestId' => $context->getAwsRequestId(),
'event' => $event,
'php_version' => PHP_VERSION,
'environment' => [
'STAGE' => $_ENV['STAGE'] ?? 'unknown',
'AWS_REGION' => $_ENV['AWS_REGION'] ?? 'unknown',
],
]);
$duration = round((microtime(true) - $startTime) * 1000, 2);
$this->logger->info('AWS PHP Lambda SAM function executed successfully', [
'duration' => $duration . 'ms',
'requestId' => $context->getAwsRequestId(),
'php_version' => PHP_VERSION,
]);
return [
'status' => 'ok',
'message' => 'AWS PHP Lambda SAM function executed successfully',
'requestId' => $context->getAwsRequestId(),
'duration' => $duration . 'ms',
'received' => $event,
'timestamp' => date('Y-m-d H:i:s'),
'php_version' => PHP_VERSION,
];
}
}
return new LambdaHandler();