Sample code for 30+ languages & platforms
Delphi DLL

Call an AWS Lambda Function

See more AWS Misc Examples

Demonstrates how to call an AWS Lambda function.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
authAws: HCkAuthAws;
json: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
sbResponseBody: HCkStringBuilder;
statusCode: Integer;

begin
success := False;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

rest := CkRest_Create();

//  Connect to the Amazon AWS REST server.
//  such as https://email.us-west-2.amazonaws.com/
bTls := True;
port := 443;
bAutoReconnect := True;

//  -------------------------------------------------------------------------------------------
//  Note: The source of the lambda function (hosted on AWS) is shown at the bottom of this page.
//  --------------------------------------------------------------------------------------------

//  If your lambda function URL is: https://itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws/
//  then use just the domain part here:
success := CkRest_Connect(rest,'itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws',port,bTls,bAutoReconnect);

//  Provide AWS credentials for the REST call.
authAws := CkAuthAws_Create();
CkAuthAws_putAccessKey(authAws,'AWS_ACCESS_KEY');
CkAuthAws_putSecretKey(authAws,'AWS_SECRET_KEY');
//  the region should match our domain above..
CkAuthAws_putRegion(authAws,'us-west-2');
CkAuthAws_putServiceName(authAws,'lambda');

CkRest_SetAuthAws(rest,authAws);

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'name','Benny');

CkRest_AddHeader(rest,'Content-Type','application/json');

sbRequestBody := CkStringBuilder_Create();
CkJsonObject_EmitSb(json,sbRequestBody);

sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/',sbRequestBody,sbResponseBody);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

statusCode := CkRest_getResponseStatusCode(rest);
if (statusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code: ' + IntToStr(statusCode));
    Memo1.Lines.Add('Response Body: ' + CkStringBuilder__getAsString(sbResponseBody));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponseBody));

CkRest_Dispose(rest);
CkAuthAws_Dispose(authAws);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbRequestBody);
CkStringBuilder_Dispose(sbResponseBody);

JavaScript
export const handler = async (event) => {

    let body = "Hello World";

    if (event.body) {
        const input = JSON.parse(event.body);
        if (input.name) {
            body = "Hello " + input.name;
        }
    }

    return {
        statusCode: 200,
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            message: body
        })
    };
};