diff --git a/DevOps/AWS/AWS_Api_Gateway.md b/DevOps/AWS/AWS_Api_Gateway.md new file mode 100644 index 0000000..f59b567 --- /dev/null +++ b/DevOps/AWS/AWS_Api_Gateway.md @@ -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. + +![](/_img/gateway-services.png) + +See [using API Gateway as Lambda trigger](/DevOps/AWS/AWS_Lambda/Practical_walkthrough_Lambda_creation.md) for a basic example of usage. diff --git a/DevOps/AWS/AWS_Lambda/Lambda_examples.md b/DevOps/AWS/AWS_Lambda/Lambda_examples.md index b61795f..7ea6ec4 100644 --- a/DevOps/AWS/AWS_Lambda/Lambda_examples.md +++ b/DevOps/AWS/AWS_Lambda/Lambda_examples.md @@ -26,3 +26,11 @@ exports.handler = (event, context, callback) => { 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. diff --git a/DevOps/AWS/AWS_Lambda/Lambda_triggers.md b/DevOps/AWS/AWS_Lambda/Lambda_triggers.md index 56bd1d2..6be78d7 100644 --- a/DevOps/AWS/AWS_Lambda/Lambda_triggers.md +++ b/DevOps/AWS/AWS_Lambda/Lambda_triggers.md @@ -2,7 +2,7 @@ categories: - DevOps - Backend -tags: [AWS, aws-lambda, node-js, python] +tags: [AWS, aws-lambda] --- # AWS Lambda triggers diff --git a/DevOps/AWS/AWS_Lambda/Practical_walkthrough_Lambda_creation.md b/DevOps/AWS/AWS_Lambda/Practical_walkthrough_Lambda_creation.md new file mode 100644 index 0000000..82610b5 --- /dev/null +++ b/DevOps/AWS/AWS_Lambda/Practical_walkthrough_Lambda_creation.md @@ -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: + +![](/_img/lambda_func_one.png) + +This presents us with the function dashboard - a graphical representation of the Lambda showing [triggers]() as an input and destination as an output: + +![](/_img/lambda-func-two.png) + +Beneath this we have a code editor with the handler function with a basic boilerplate: + +![](/_img/lambda-func-three.png) + +## 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: + +![](/_img/api-gateway-trigger.png) + +Now we see this step displayed in the dashboard: + +![](/_img/lambda-overview.png) + +With the endpoint and other settings displayed: + +![](/_img/trigger-info.png) + +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: + +![](/_img/postman-lambda.png) diff --git a/_img/api-gateway-trigger.png b/_img/api-gateway-trigger.png new file mode 100644 index 0000000..7ab0e1a Binary files /dev/null and b/_img/api-gateway-trigger.png differ diff --git a/_img/gateway-services.png b/_img/gateway-services.png new file mode 100644 index 0000000..cd11117 Binary files /dev/null and b/_img/gateway-services.png differ diff --git a/_img/lambda-func-three.png b/_img/lambda-func-three.png new file mode 100644 index 0000000..a6fb7ea Binary files /dev/null and b/_img/lambda-func-three.png differ diff --git a/_img/lambda-func-two.png b/_img/lambda-func-two.png new file mode 100644 index 0000000..d57af7f Binary files /dev/null and b/_img/lambda-func-two.png differ diff --git a/_img/lambda-overview.png b/_img/lambda-overview.png new file mode 100644 index 0000000..8a522ca Binary files /dev/null and b/_img/lambda-overview.png differ diff --git a/_img/lambda_func_one.png b/_img/lambda_func_one.png new file mode 100644 index 0000000..11353a7 Binary files /dev/null and b/_img/lambda_func_one.png differ diff --git a/_img/postman-lambda.png b/_img/postman-lambda.png new file mode 100644 index 0000000..5afc07e Binary files /dev/null and b/_img/postman-lambda.png differ diff --git a/_img/trigger-info.png b/_img/trigger-info.png new file mode 100644 index 0000000..b478497 Binary files /dev/null and b/_img/trigger-info.png differ