Sample code for 30+ languages & platforms
Lianja

Call an AWS Lambda Function

See more AWS Misc Examples

Demonstrates how to call an AWS Lambda function.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

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

loRest = createobject("CkRest")

// Connect to the Amazon AWS REST server.
// such as https://email.us-west-2.amazonaws.com/
llBTls = .T.
lnPort = 443
llBAutoReconnect = .T.

// -------------------------------------------------------------------------------------------
// 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:
llSuccess = loRest.Connect("itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws",lnPort,llBTls,llBAutoReconnect)

// Provide AWS credentials for the REST call.
loAuthAws = createobject("CkAuthAws")
loAuthAws.AccessKey = "AWS_ACCESS_KEY"
loAuthAws.SecretKey = "AWS_SECRET_KEY"
// the region should match our domain above..
loAuthAws.Region = "us-west-2"
loAuthAws.ServiceName = "lambda"

loRest.SetAuthAws(loAuthAws)

loJson = createobject("CkJsonObject")
loJson.UpdateString("name","Benny")

loRest.AddHeader("Content-Type","application/json")

loSbRequestBody = createobject("CkStringBuilder")
loJson.EmitSb(loSbRequestBody)

loSbResponseBody = createobject("CkStringBuilder")
llSuccess = loRest.FullRequestSb("POST","/",loSbRequestBody,loSbResponseBody)
if (llSuccess = .F.) then
    ? loRest.LastErrorText
    release loRest
    release loAuthAws
    release loJson
    release loSbRequestBody
    release loSbResponseBody
    return
endif

lnStatusCode = loRest.ResponseStatusCode
if (lnStatusCode >= 400) then
    ? "Response Status Code: " + str(lnStatusCode)
    ? "Response Body: " + loSbResponseBody.GetAsString()
    ? "Failed."
    release loRest
    release loAuthAws
    release loJson
    release loSbRequestBody
    release loSbResponseBody
    return
endif

? "Response Body:"
? loSbResponseBody.GetAsString()


release loRest
release loAuthAws
release loJson
release loSbRequestBody
release loSbResponseBody