Lua
Lua
Call an AWS Lambda Function
See more AWS Misc Examples
Demonstrates how to call an AWS Lambda function.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local rest = chilkat.Rest.new()
-- Connect to the Amazon AWS REST server.
-- such as https://email.us-west-2.amazonaws.com/
local bTls = true
local port = 443
local 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:
local success = rest:connect("itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws", port, bTls, bAutoReconnect)
-- Provide AWS credentials for the REST call.
local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
-- the region should match our domain above..
authAws.region = "us-west-2"
authAws.service_name = "lambda"
rest:set_auth_aws(authAws)
local json = chilkat.JsonObject.new()
json:update_string("name", "Benny")
rest:add_header("Content-Type", "application/json")
local sbRequestBody = chilkat.StringBuilder.new()
json:emit_sb(sbRequestBody)
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/", sbRequestBody, sbResponseBody)
if not success then
print(rest.last_error_text)
return
end
local statusCode = rest.response_status_code
if statusCode >= 400 then
print("Response Status Code: " .. statusCode)
print("Response Body: " .. sbResponseBody:get_as_string())
print("Failed.")
return
end
print("Response Body:")
print(sbResponseBody:get_as_string())
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
})
};
};