Rust
Rust
Call an AWS Lambda Function
See more AWS Misc Examples
Demonstrates how to call an AWS Lambda function.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rest = chilkat::Rest::new();
// Connect to the Amazon AWS REST server.
// such as https://email.us-west-2.amazonaws.com/
let b_tls = true;
let port = 443;
let b_auto_reconnect = 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:
let _ = rest.connect("itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws", port, b_tls, b_auto_reconnect).is_ok();
// Provide AWS credentials for the REST call.
let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
// the region should match our domain above..
auth_aws.set_region("us-west-2");
auth_aws.set_service_name("lambda");
let _ = rest.set_auth_aws(&auth_aws);
let json = chilkat::JsonObject::new();
let _ = json.update_string("name", "Benny");
let _ = rest.add_header("Content-Type", "application/json");
let sb_request_body = chilkat::StringBuilder::new();
let _ = json.emit_sb(&sb_request_body);
let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/", &sb_request_body, &sb_response_body).is_err() {
println!("{}", rest.last_error_text());
return;
}
let status_code = rest.response_status_code();
if status_code >= 400 {
println!("Response Status Code: {}", status_code);
println!("Response Body: {}", sb_response_body.get_as_string().unwrap_or_default());
println!("Failed.");
return;
}
println!("Response Body:");
println!("{}", sb_response_body.get_as_string().unwrap_or_default());
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
})
};
};