Xbase++
Xbase++
Call an AWS Lambda Function
See more AWS Misc Examples
Demonstrates how to call an AWS Lambda function.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL oJson
LOCAL oSbRequestBody
LOCAL oSbResponseBody
LOCAL nStatusCode
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oRest := CreateObject("Chilkat.Rest")
// Connect to the Amazon AWS REST server.
// such as https://email.us-west-2.amazonaws.com/
nBTls := 1
nPort := 443
nBAutoReconnect := 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:
nSuccess := oRest:Connect("itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws", nPort, nBTls, nBAutoReconnect)
// Provide AWS credentials for the REST call.
oAuthAws := CreateObject("Chilkat.AuthAws")
oAuthAws:AccessKey := "AWS_ACCESS_KEY"
oAuthAws:SecretKey := "AWS_SECRET_KEY"
// the region should match our domain above..
oAuthAws:Region := "us-west-2"
oAuthAws:ServiceName := "lambda"
oRest:SetAuthAws(oAuthAws)
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("name", "Benny")
oRest:AddHeader("Content-Type", "application/json")
oSbRequestBody := CreateObject("Chilkat.StringBuilder")
oJson:EmitSb(oSbRequestBody)
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestSb("POST", "/", oSbRequestBody, oSbResponseBody)
IF (nSuccess == 0)
? oRest:LastErrorText
oRest:destroy()
oAuthAws:destroy()
oJson:destroy()
oSbRequestBody:destroy()
oSbResponseBody:destroy()
RETURN
ENDIF
nStatusCode := oRest:ResponseStatusCode
IF (nStatusCode >= 400)
? "Response Status Code: " + Str(nStatusCode)
? "Response Body: " + oSbResponseBody:GetAsString()
? "Failed."
oRest:destroy()
oAuthAws:destroy()
oJson:destroy()
oSbRequestBody:destroy()
oSbResponseBody:destroy()
RETURN
ENDIF
? "Response Body:"
? oSbResponseBody:GetAsString()
oRest:destroy()
oAuthAws:destroy()
oJson:destroy()
oSbRequestBody:destroy()
oSbResponseBody:destroy()
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
})
};
};