Create a “Hello, World!” Lambda function
This function returns a greeting to the caller as a JSON object in the following format:
{
"greeting": "Good {time}
, {name}
of {city}
.[ Happy {day}
!]"
- Sign in to the Lambda console at https://console.aws.amazon.com/lambda.
- On the AWS navigation bar, choose a region (for example, US East (N. Virginia).
- Choose Functions in the navigation pane.
- Choose Create function.
- Choose Author from scratch.
- Under Basic information, do the following:
- In Function name, enter
GetStartedLambdaProxyIntegration
. - From the Runtime dropdown list, choose a supported Node.js runtime.
- Under Permissions, expand Choose or create an execution role. From the Execution role dropdown list, choose Create new role from AWS policy templates.
- In Role name, enter
GetStartedLambdaBasicExecutionRole
. - Leave the Policy templates field blank.
- Choose Create function.
- In Function name, enter
- Under Function code, in the inline code editor, copy/paste the following code:
'use strict'; console.log('Loading hello world function'); exports.handler = async (event) => { let name = "you"; let city = 'World'; let time = 'day'; let day = ''; let responseCode = 200; console.log("request: " + JSON.stringify(event)); if (event.queryStringParameters && event.queryStringParameters.name) { console.log("Received name: " + event.queryStringParameters.name); name = event.queryStringParameters.name; } if (event.queryStringParameters && event.queryStringParameters.city) { console.log("Received city: " + event.queryStringParameters.city); city = event.queryStringParameters.city; } if (event.headers && event.headers['day']) { console.log("Received day: " + event.headers.day); day = event.headers.day; } if (event.body) { let body = JSON.parse(event.body) if (body.time) time = body.time; } let greeting = `Good ${time}, ${name} of ${city}.`; if (day) greeting += ` Happy ${day}!`; let responseBody = { message: greeting, input: event }; // The output from a Lambda proxy integration must be // in the following JSON object. The 'headers' property // is for custom response headers in addition to standard // ones. The 'body' property must be a JSON string. For // base64-encoded payload, you must also set the 'isBase64Encoded' // property to 'true'. let response = { statusCode: responseCode, headers: { "x-custom-header" : "my custom header value" }, body: JSON.stringify(responseBody) }; console.log("response: " + JSON.stringify(response)) return response; };
- Choose Save
Comments are closed, but trackbacks and pingbacks are open.