-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAWSCustomRuntime.pas
72 lines (54 loc) · 1.99 KB
/
AWSCustomRuntime.pas
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
program AWSCustomRuntime;
{$mode objfpc}{$H+}
uses
sysutils, classes, fphttpclient, fpjson, jsonparser;
var
awsHost, awsBaseUrl, awsResponseUrl, awsErrorUrl, awsRequestId, awsEventBody: String;
httpClient: TFPHTTPClient;
awsEvent, awsError: TJSONObject;
const
// Current AWS runtime API version
APIVERSION = '2018-06-01';
begin
awsEvent := TJSONObject.Create;
awsError := TJSONObject.Create;
httpClient := TFPHttpClient.Create(Nil);
try
// Get the runtime api awsHost
awsHost := GetEnvironmentVariable('AWS_LAMBDA_RUNTIME_API');
// Create the base url
awsBaseUrl := 'http://' + awsHost + '/' + APIVERSION + '/runtime/invocation/';
while true do begin
try
// Get the event
awsEventBody := httpClient.get(awsBaseUrl + 'next');
// Get the JSON data and set the TJSONObject
awsEvent := TJSONObject(GetJSON(awsEventBody));
// Pretty-print the event (Should be visible in logwatch)
WriteLn(awsEvent.FormatJSON);
// Get the request id, used when responding
awsRequestId := trim(httpClient.ResponseHeaders.Values['Lambda-Runtime-AWS-Request-Id']);
// Create the response url
awsResponseUrl := awsBaseUrl + awsRequestId + '/response';
// Create error url
awsErrorUrl := awsBaseUrl + awsRequestId + '/error';
// Send successful event response
TFPHttpClient.SimpleFormPost(awsResponseUrl, awsEventBody);
{
Error responses should follow the JSON format below, see here for details
https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-invokeerror
Example
-------
awsError.Strings['errorMessage'] := 'Something went horribly wrong';
awsError.Strings['errorType'] := 'InvalidEventDataException';
TFPHttpClient.SimpleFormPost(awsErrorUrl, awsError.AsJSON);
}
except
end;
end;
finally
httpClient.Free;
awsEvent.Free;
awsError.Free;
end;
end.