Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Call an AWS Lambda Function

See more AWS Misc Examples

Demonstrates how to call an AWS Lambda function.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.AuthAws,
  Chilkat.Rest,
  Chilkat.JsonObject,
  Chilkat.StringBuilder;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  rest: TRest;
  bTls: Boolean;
  port: Integer;
  bAutoReconnect: Boolean;
  authAws: TAuthAws;
  json: TJsonObject;
  sbRequestBody: TStringBuilder;
  sbResponseBody: TStringBuilder;
  statusCode: Integer;

begin
  success := False;

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

  rest := TRest.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 := rest.Connect('itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws',port,bTls,bAutoReconnect);

  //  Provide AWS credentials for the REST call.
  authAws := TAuthAws.Create;
  authAws.AccessKey := 'AWS_ACCESS_KEY';
  authAws.SecretKey := 'AWS_SECRET_KEY';
  //  the region should match our domain above..
  authAws.Region := 'us-west-2';
  authAws.ServiceName := 'lambda';

  rest.SetAuthAws(authAws);

  json := TJsonObject.Create;
  json.UpdateString('name','Benny');

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

  sbRequestBody := TStringBuilder.Create;
  json.EmitSb(sbRequestBody);

  sbResponseBody := TStringBuilder.Create;
  success := rest.FullRequestSb('POST','/',sbRequestBody,sbResponseBody);
  if (success = False) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  statusCode := rest.ResponseStatusCode;
  if (statusCode >= 400) then
    begin
      WriteLn('Response Status Code: ' + statusCode);
      WriteLn('Response Body: ' + sbResponseBody.GetAsString());
      WriteLn('Failed.');
      Exit;
    end;

  WriteLn('Response Body:');
  WriteLn(sbResponseBody.GetAsString());


  rest.Free;
  authAws.Free;
  json.Free;
  sbRequestBody.Free;
  sbResponseBody.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.