Autosave: 2023-04-10 19:36:55
14
DevOps/AWS/AWS_Api_Gateway.md
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- DevOps
|
||||||
|
- Backend
|
||||||
|
tags: [AWS, aws-api-api-gateway, APIs]
|
||||||
|
---
|
||||||
|
|
||||||
|
# AWS API Gateway
|
||||||
|
|
||||||
|
We can use Gateway as the front-door to our application. It will create an [HTTP](/Databases/REST/HTTP_request_types.md) endpoint that you can call from a client. In response to a client request you can then call a [lambda function](/DevOps/AWS/AWS_Lambda/Lambda_handler_function.md) that executes a backend process.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
See [using API Gateway as Lambda trigger](/DevOps/AWS/AWS_Lambda/Practical_walkthrough_Lambda_creation.md) for a basic example of usage.
|
|
@ -26,3 +26,11 @@ exports.handler = (event, context, callback) => {
|
||||||
return callback(null, request);
|
return callback(null, request);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Let's walk through the above example making reference to the general structure of the [AWS Lambda handler function](/DevOps/AWS/AWS_Lambda/Lambda_handler_function.md):
|
||||||
|
|
||||||
|
1. The `event` parameter is invoked to gain access to the event that triggered the function. This is a CloudFront request event which is targetted with `event.Records[0].cf.request`
|
||||||
|
2. We classify this request as the `olduri` since it doesn't have the form we want.
|
||||||
|
3. We mutate the original request by replacing `/` with `/index.html`
|
||||||
|
4. We then re-assign the `request.uri` value to the value of the new URI.
|
||||||
|
5. Finally we invoke the `callback` parameter to signal the completion of the transformation and return the new request as the return value.
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
categories:
|
categories:
|
||||||
- DevOps
|
- DevOps
|
||||||
- Backend
|
- Backend
|
||||||
tags: [AWS, aws-lambda, node-js, python]
|
tags: [AWS, aws-lambda]
|
||||||
---
|
---
|
||||||
|
|
||||||
# AWS Lambda triggers
|
# AWS Lambda triggers
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- DevOps
|
||||||
|
- Backend
|
||||||
|
tags: [AWS, aws-lambda, node-js]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Practical walkthrough of creating a Lambda function, trigger and subsequent events
|
||||||
|
|
||||||
|
## Basic set-up
|
||||||
|
|
||||||
|
First we name the function and accept the defaults:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
This presents us with the function dashboard - a graphical representation of the Lambda showing [triggers]() as an input and destination as an output:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Beneath this we have a code editor with the handler function with a basic boilerplate:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Adding a trigger
|
||||||
|
|
||||||
|
Next we need to add a trigger that execute the handler.
|
||||||
|
|
||||||
|
We will do this using [AWS API Gateway](/DevOps/AWS/AWS_Api_Gateway.md). We select "Add trigger" from the dashboard view and input basic settings:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Now we see this step displayed in the dashboard:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
With the endpoint and other settings displayed:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
|
## 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:
|
||||||
|
|
||||||
|
```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`
|
||||||
|
|
||||||
|
We get `Hello Thomas` as output.
|
||||||
|
|
||||||
|
For a more advanced API with multiple endpoints and parameters, it's easiest to use Postman:
|
||||||
|
|
||||||
|

|
BIN
_img/api-gateway-trigger.png
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
_img/gateway-services.png
Normal file
After Width: | Height: | Size: 56 KiB |
BIN
_img/lambda-func-three.png
Normal file
After Width: | Height: | Size: 50 KiB |
BIN
_img/lambda-func-two.png
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
_img/lambda-overview.png
Normal file
After Width: | Height: | Size: 32 KiB |
BIN
_img/lambda_func_one.png
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
_img/postman-lambda.png
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
_img/trigger-info.png
Normal file
After Width: | Height: | Size: 58 KiB |