Skip to content

Commit e744e5f

Browse files
committed
Adding core source files
1 parent 0e4f19f commit e744e5f

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed

AWSCustomRuntime.lpi

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectOptions>
4+
<Version Value="11"/>
5+
<General>
6+
<Flags>
7+
<MainUnitHasCreateFormStatements Value="False"/>
8+
<MainUnitHasTitleStatement Value="False"/>
9+
<MainUnitHasScaledStatement Value="False"/>
10+
</Flags>
11+
<SessionStorage Value="InProjectDir"/>
12+
<MainUnit Value="0"/>
13+
<Title Value="AWSCustomRuntime"/>
14+
<UseAppBundle Value="False"/>
15+
<ResourceType Value="res"/>
16+
</General>
17+
<BuildModes Count="1">
18+
<Item1 Name="Linux" Default="True"/>
19+
</BuildModes>
20+
<PublishOptions>
21+
<Version Value="2"/>
22+
<UseFileFilters Value="True"/>
23+
</PublishOptions>
24+
<RunParams>
25+
<FormatVersion Value="2"/>
26+
<Modes Count="0"/>
27+
</RunParams>
28+
<Units Count="2">
29+
<Unit0>
30+
<Filename Value="AWSCustomRuntime.pas"/>
31+
<IsPartOfProject Value="True"/>
32+
</Unit0>
33+
<Unit1>
34+
<Filename Value=".gitignore.txt"/>
35+
<IsPartOfProject Value="True"/>
36+
</Unit1>
37+
</Units>
38+
</ProjectOptions>
39+
<CompilerOptions>
40+
<Version Value="11"/>
41+
<Target>
42+
<Filename Value="AWSCustomRuntime"/>
43+
</Target>
44+
<SearchPaths>
45+
<IncludeFiles Value="$(ProjOutDir)"/>
46+
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
47+
</SearchPaths>
48+
<CodeGeneration>
49+
<TargetCPU Value="x86_64"/>
50+
<TargetOS Value="linux"/>
51+
<Optimizations>
52+
<OptimizationLevel Value="2"/>
53+
</Optimizations>
54+
</CodeGeneration>
55+
<Linking>
56+
<Debugging>
57+
<GenerateDebugInfo Value="False"/>
58+
</Debugging>
59+
</Linking>
60+
</CompilerOptions>
61+
<Debugging>
62+
<Exceptions Count="3">
63+
<Item1>
64+
<Name Value="EAbort"/>
65+
</Item1>
66+
<Item2>
67+
<Name Value="ECodetoolError"/>
68+
</Item2>
69+
<Item3>
70+
<Name Value="EFOpenError"/>
71+
</Item3>
72+
</Exceptions>
73+
</Debugging>
74+
</CONFIG>

AWSCustomRuntime.lps

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectSession>
4+
<Version Value="11"/>
5+
<BuildModes Active="Linux"/>
6+
<Units Count="2">
7+
<Unit0>
8+
<Filename Value="AWSCustomRuntime.pas"/>
9+
<IsPartOfProject Value="True"/>
10+
<IsVisibleTab Value="True"/>
11+
<CursorPos X="4" Y="8"/>
12+
<UsageCount Value="21"/>
13+
<Loaded Value="True"/>
14+
</Unit0>
15+
<Unit1>
16+
<Filename Value=".gitignore.txt"/>
17+
<IsPartOfProject Value="True"/>
18+
<EditorIndex Value="-1"/>
19+
<CursorPos X="5"/>
20+
<UsageCount Value="21"/>
21+
<DefaultSyntaxHighlighter Value="None"/>
22+
</Unit1>
23+
</Units>
24+
<JumpHistory Count="11" HistoryIndex="10">
25+
<Position1>
26+
<Filename Value="AWSCustomRuntime.pas"/>
27+
<Caret Line="10" Column="42"/>
28+
</Position1>
29+
<Position2>
30+
<Filename Value="AWSCustomRuntime.pas"/>
31+
<Caret Line="11" Column="13"/>
32+
</Position2>
33+
<Position3>
34+
<Filename Value="AWSCustomRuntime.pas"/>
35+
<Caret Line="17" Column="7"/>
36+
</Position3>
37+
<Position4>
38+
<Filename Value="AWSCustomRuntime.pas"/>
39+
<Caret Line="14" Column="11"/>
40+
</Position4>
41+
<Position5>
42+
<Filename Value="AWSCustomRuntime.pas"/>
43+
<Caret Line="27" Column="12" TopLine="13"/>
44+
</Position5>
45+
<Position6>
46+
<Filename Value="AWSCustomRuntime.pas"/>
47+
<Caret Line="10" Column="56"/>
48+
</Position6>
49+
<Position7>
50+
<Filename Value="AWSCustomRuntime.pas"/>
51+
<Caret Line="27" Column="12"/>
52+
</Position7>
53+
<Position8>
54+
<Filename Value="AWSCustomRuntime.pas"/>
55+
<Caret Line="10" Column="54"/>
56+
</Position8>
57+
<Position9>
58+
<Filename Value="AWSCustomRuntime.pas"/>
59+
<Caret Line="40" TopLine="12"/>
60+
</Position9>
61+
<Position10>
62+
<Filename Value="AWSCustomRuntime.pas"/>
63+
<Caret Line="21" Column="7"/>
64+
</Position10>
65+
<Position11>
66+
<Filename Value="AWSCustomRuntime.pas"/>
67+
<Caret Line="10" Column="52"/>
68+
</Position11>
69+
</JumpHistory>
70+
<RunParams>
71+
<FormatVersion Value="2"/>
72+
<Modes Count="0" ActiveMode=""/>
73+
</RunParams>
74+
</ProjectSession>
75+
</CONFIG>

AWSCustomRuntime.pas

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
program AWSCustomRuntime;
2+
3+
{$mode objfpc}{$H+}
4+
5+
uses
6+
sysutils, classes, fphttpclient, fpjson, jsonparser;
7+
8+
var
9+
awsHost, awsBaseUrl, awsResponseUrl, awsErrorUrl, awsRequestId, awsEventBody: String;
10+
httpClient: TFPHTTPClient;
11+
awsEvent, awsError: TJSONObject;
12+
13+
begin
14+
awsEvent := TJSONObject.Create;
15+
awsError := TJSONObject.Create;
16+
httpClient := TFPHttpClient.Create(Nil);
17+
try
18+
// Get the runtime api awsHost
19+
awsHost := GetEnvironmentVariable('AWS_LAMBDA_RUNTIME_API');
20+
21+
// Create the base url
22+
awsBaseUrl := 'http://' + awsHost + '/2018-06-01/runtime/invocation/';
23+
24+
while true do begin
25+
try
26+
// Get the event
27+
awsEventBody := httpClient.get(awsBaseUrl + 'next');
28+
29+
// Get the JSON data and set the TJSONObject
30+
awsEvent := TJSONObject(GetJSON(awsEventBody));
31+
32+
// Pretty-print the event (Should be visible in logwatch)
33+
WriteLn(awsEvent.FormatJSON);
34+
35+
// Get the request id, used when responding
36+
awsRequestId := trim(httpClient.ResponseHeaders.Values['Lambda-Runtime-AWS-Request-Id']);
37+
38+
// Create the response url
39+
awsResponseUrl := awsBaseUrl + awsRequestId + '/response';
40+
41+
// Create error url
42+
awsErrorUrl := awsBaseUrl + awsRequestId + '/error';
43+
44+
// Send successful event response
45+
TFPHttpClient.SimpleFormPost(awsResponseUrl, awsEventBody);
46+
47+
{
48+
Error responses should follow the JSON format below, see here for details
49+
https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-invokeerror
50+
51+
Example
52+
-------
53+
54+
awsError.Strings['errorMessage'] := 'Something went horribly wrong';
55+
awsError.Strings['errorType'] := 'InvalidEventDataException';
56+
57+
TFPHttpClient.SimpleFormPost(awsErrorUrl, awsError.AsJSON);
58+
}
59+
except
60+
end;
61+
end;
62+
63+
finally
64+
httpClient.Free;
65+
awsEvent.Free;
66+
awsError.Free;
67+
end;
68+
end.

0 commit comments

Comments
 (0)