-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbootstrap
executable file
·97 lines (85 loc) · 3.01 KB
/
bootstrap
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/opt/bin/perl
use strict;
use warnings;
use utf8;
use HTTP::Tiny;
use JSON::XS qw/decode_json encode_json/;
my $task_root;
BEGIN {
$task_root = $ENV{'LAMBDA_TASK_ROOT'};
}
use lib "${task_root}/local/lib/perl5"; # for vendoring
my $http = HTTP::Tiny->new;
my $aws_lambda_runtime_api = $ENV{'AWS_LAMBDA_RUNTIME_API'} // die '$AWS_LAMBDA_RUNTIME_API is not found';
my $next_event_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/invocation/next";
my ($handler, $func) = split(/[.]/, $ENV{'_HANDLER'}, 2);
eval {
require "${task_root}/${handler}.pl";
};
if ($@) {
# something went wrong on initiation
# ref: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-initerror
my $err_message = $@;
my $error_resp_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/init/error";
my $resp = $http->post($error_resp_url, {
headers => {
'Content-Type' => 'application/json',
'Lambda-Runtime-Function-Error-Type' => 'Unhandled',
},
content => encode_json({
errorMessage => $err_message,
errorType => "FunctionInitiationError",
}),
});
unless ($resp->{success}) {
die "failed to response of failure initiation";
}
die $err_message;
}
my $f = \&$func;
while (1) {
my $next_event_resp = $http->get($next_event_url);
unless ($next_event_resp->{success}) {
die "failed to retrieve the next event: $next_event_resp->{status} $next_event_resp->{reason}";
}
my $request_id = $next_event_resp->{headers}->{'lambda-runtime-aws-request-id'};
unless ($request_id) {
die 'cannot take the Lambda request ID';
}
my $payload = decode_json($next_event_resp->{content});
my $result;
eval {
$result = $f->($payload);
};
if ($@) {
# something went wrong on called function
# ref: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-invokeerror
my $err_message = $@;
my $error_resp_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/invocation/$request_id/error";
my $resp = $http->post($error_resp_url, {
headers => {
'Content-Type' => 'application/json',
'Lambda-Runtime-Function-Error-Type' => 'Unhandled',
},
content => encode_json({
errorMessage => $err_message,
errorType => "FunctionInvocationError",
}),
});
unless ($resp->{success}) {
die "failed to response of failure execution: $request_id";
}
next;
}
my $exec_resp_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/invocation/$request_id/response";
my $resp = $http->post($exec_resp_url, {
headers => {
'Content-Type' => 'application/json',
},
content => encode_json($result),
});
unless ($resp->{success}) {
die "failed to response of execution: $resp->{status} $resp->{reason}";
}
}
__END__