React Native
React Native
Call an AWS Lambda Function
See more AWS Misc Examples
Demonstrates how to call an AWS Lambda function.Chilkat React Native Downloads
import { AuthAws, JsonObject, Rest, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = new Rest();
// Connect to the Amazon AWS REST server.
// such as https://email.us-west-2.amazonaws.com/
const bTls = true;
const port = 443;
const 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:
await rest.connectAsync('itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws', port, bTls, bAutoReconnect);
// Provide AWS credentials for the REST call.
const authAws = new AuthAws();
authAws.accessKey = 'AWS_ACCESS_KEY';
authAws.secretKey = 'AWS_SECRET_KEY';
// the region should match our domain above..
authAws.region = 'us-west-2';
authAws.serviceName = 'lambda';
rest.setAuthAws(authAws);
const json = new JsonObject();
json.updateString('name', 'Benny');
rest.addHeader('Content-Type', 'application/json');
const sbRequestBody = new StringBuilder();
json.emitSb(sbRequestBody);
const sbResponseBody = new StringBuilder();
try {
await rest.fullRequestSbAsync('POST', '/', sbRequestBody, sbResponseBody);
} catch {
console.log(rest.lastErrorText);
return;
}
const statusCode = rest.responseStatusCode;
if (statusCode >= 400) {
console.log(`Response Status Code: ${statusCode}`);
console.log(`Response Body: ${sbResponseBody.getAsString()}`);
console.log('Failed.');
return;
}
console.log('Response Body:');
console.log(sbResponseBody.getAsString());
}
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
})
};
};