Zig
Zig
Call an AWS Lambda Function
See more AWS Misc Examples
Demonstrates how to call an AWS Lambda function.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the Amazon AWS REST server.
// such as https://email.us-west-2.amazonaws.com/
const b_tls = true;
const port = 443;
const 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:
try rest.connect("itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws", port, b_tls, b_auto_reconnect);
// Provide AWS credentials for the REST call.
const auth_aws = try chilkat.AuthAws.init();
defer auth_aws.deinit();
auth_aws.setAccessKey("AWS_ACCESS_KEY");
auth_aws.setSecretKey("AWS_SECRET_KEY");
// the region should match our domain above..
auth_aws.setRegion("us-west-2");
auth_aws.setServiceName("lambda");
rest.setAuthAws(auth_aws) catch {};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "Benny") catch {};
rest.addHeader("Content-Type", "application/json") catch {};
const sb_request_body = try chilkat.StringBuilder.init();
defer sb_request_body.deinit();
json.emitSb(sb_request_body) catch {};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestSb("POST", "/", sb_request_body, sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const status_code = rest.getResponseStatusCode();
if (status_code >= 400) {
std.debug.print("Response Status Code: {d}\n", .{status_code});
std.debug.print("Response Body: {s}\n", .{try sb_response_body.getAsString(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
}
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
})
};
};