Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active January 9, 2023 04:52
Show Gist options
  • Save harisrozak/c1d61a5989e242ed8ea1fac71bf5315a to your computer and use it in GitHub Desktop.
Save harisrozak/c1d61a5989e242ed8ea1fac71bf5315a to your computer and use it in GitHub Desktop.
Sample AWS Lambda code with NodeJS
export const handler = async (event) => {
const body = JSON.parse(event.body);
const queryStrings = event.queryStringParameters;
let capitalCity = 'none';
const message = !!queryStrings?.message ? queryStrings.message : 'Hello';
switch (body.nation) {
case 'Indonesia':
capitalCity = 'Jakarta';
break;
case 'US':
capitalCity = 'Washington, D.C.';
break;
case 'Germany':
capitalCity = 'Berlin';
break;
default:
// code
}
const response = {
statusCode: 200,
body: {
nation: body.nation,
capital: capitalCity,
message: message,
}
};
return response;
};
@harisrozak
Copy link
Author

The event JSON will be:

{
  "nation": "Indonesia"
}

@harisrozak
Copy link
Author

harisrozak commented Jan 9, 2023

Sample POST target endpoint with Function URL:

https://qwertyjr5b2tm245h3fp2broki0djdda.lambda-url.us-west-2.on.aws/?message=HelloWorld

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment