2023-08-31 20:40:30 +01:00
|
|
|
---
|
|
|
|
tags: [AWS, aws-lambda, node-js]
|
|
|
|
---
|
|
|
|
|
|
|
|
# Practical walkthrough of creating a Lambda function via the AWS Management Console
|
|
|
|
|
|
|
|
## Basic set-up
|
|
|
|
|
|
|
|
First we name the function and accept the defaults:
|
|
|
|
|
2024-02-16 16:14:01 +00:00
|
|
|

|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
This presents us with the function dashboard - a graphical representation of the
|
|
|
|
Lambda showing [triggers]() as an input and destination as an output:
|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-16 16:14:01 +00:00
|
|
|

|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Beneath this we have a code editor with the handler function with a basic
|
|
|
|
boilerplate:
|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-16 16:14:01 +00:00
|
|
|

|
2023-08-31 20:40:30 +01:00
|
|
|
|
|
|
|
## Adding a trigger
|
|
|
|
|
2024-02-17 11:57:44 +00:00
|
|
|
Next we need to add a [trigger](Lambda_triggers.md) that
|
2024-02-02 15:58:13 +00:00
|
|
|
execute the handler.
|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-17 11:57:44 +00:00
|
|
|
We will do this using [AWS API Gateway](AWS_API_Gateway.md). We
|
2024-02-02 15:58:13 +00:00
|
|
|
select "Add trigger" from the dashboard view and input basic settings:
|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-16 16:14:01 +00:00
|
|
|

|
2023-08-31 20:40:30 +01:00
|
|
|
|
|
|
|
Now we see this step displayed in the dashboard:
|
|
|
|
|
2024-02-16 16:14:01 +00:00
|
|
|

|
2023-08-31 20:40:30 +01:00
|
|
|
|
|
|
|
With the endpoint and other settings displayed:
|
|
|
|
|
2024-02-16 16:14:01 +00:00
|
|
|

|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
If we go to the endpoint URL
|
|
|
|
(`https://4kjqwbvd7g.execute-api.us-east-1.amazonaws.com/default/myFirstFunction`),
|
|
|
|
we will see the output: `Hello from Lambda`.
|
2023-08-31 20:40:30 +01:00
|
|
|
|
|
|
|
## Handling parameters
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
We can make the example more realistic by expanding the handler to accept query
|
|
|
|
parameters. We do this by accessing the value `queryStringParameters` on the
|
|
|
|
`event` object:
|
2023-08-31 20:40:30 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
exports.handler = async (event) => {
|
|
|
|
const name = event.queryStringParameters && event.queryStringParameters.name;
|
|
|
|
|
|
|
|
let message = "Hello Lambda";
|
|
|
|
|
|
|
|
if (name !== null) {
|
|
|
|
message = `Hello ${name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response {
|
|
|
|
statusCode: 200,
|
|
|
|
body: JSON.stringify(message)
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
If we now access
|
|
|
|
`https://4kjqwbvd7g.execute-api.us-east-1.amazonaws.com/default/myFirstFunction?name=Thomas`
|
2023-08-31 20:40:30 +01:00
|
|
|
|
|
|
|
We get `Hello Thomas` as output.
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
For a more advanced API with multiple endpoints and parameters, it's easiest to
|
|
|
|
use Postman:
|
2023-08-31 20:40:30 +01:00
|
|
|
|
2024-02-16 16:14:01 +00:00
|
|
|

|