Sample code for 30+ languages & platforms
Delphi ActiveX

Call an AWS Lambda Function

See more AWS Misc Examples

Demonstrates how to call an AWS Lambda function.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
rest: TChilkatRest;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
authAws: TChilkatAuthAws;
json: TChilkatJsonObject;
sbRequestBody: TChilkatStringBuilder;
sbResponseBody: TChilkatStringBuilder;
statusCode: Integer;

begin
success := 0;

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

rest := TChilkatRest.Create(Self);

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

//  -------------------------------------------------------------------------------------------
//  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 := TChilkatAuthAws.Create(Self);
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.ControlInterface);

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

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

sbRequestBody := TChilkatStringBuilder.Create(Self);
json.EmitSb(sbRequestBody.ControlInterface);

sbResponseBody := TChilkatStringBuilder.Create(Self);
success := rest.FullRequestSb('POST','/',sbRequestBody.ControlInterface,sbResponseBody.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

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

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(sbResponseBody.GetAsString());
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
        })
    };
};