eolas/zk/Practical_walkthrough_Lambda_creation_within_AWS.md

78 lines
1.8 KiB
Markdown
Raw Permalink Normal View History

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
![](/img/lambda_func_one.png)
2023-08-31 20:40:30 +01: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
![](/img/lambda-func-two.png)
2023-08-31 20:40:30 +01: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
![](/img/lambda-func-three.png)
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
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
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
![](/img/api-gateway-trigger.png)
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
![](/img/lambda-overview.png)
2023-08-31 20:40:30 +01:00
With the endpoint and other settings displayed:
2024-02-16 16:14:01 +00:00
![](/img/trigger-info.png)
2023-08-31 20:40:30 +01: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
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)
}
};
```
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.
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
![](/img/postman-lambda.png)