diff --git a/DevOps/AWS/AWS_Lambda.md b/DevOps/AWS/AWS_Lambda.md deleted file mode 100644 index c87bc31..0000000 --- a/DevOps/AWS/AWS_Lambda.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -categories: - - DevOps -tags: [AWS] ---- - -# AWS Lambda diff --git a/DevOps/AWS/AWS_Lambda/AWS_Lambda_examples.md b/DevOps/AWS/AWS_Lambda/AWS_Lambda_examples.md new file mode 100644 index 0000000..b61795f --- /dev/null +++ b/DevOps/AWS/AWS_Lambda/AWS_Lambda_examples.md @@ -0,0 +1,28 @@ +--- +categories: + - DevOps +tags: [AWS, aws-lambda, backend] +--- + +# AWS Lambda examples + +## Reconstruct CloudFront URLs + +```js +exports.handler = (event, context, callback) => { + // Extract the request from the CloudFront event that is sent to Lambda@Edge + var request = event.Records[0].cf.request; + + // Extract the URI from the request + var olduri = request.uri; + + // Match any '/' that occurs at the end of a URI. Replace it with a default index + var newuri = olduri.replace(/\/$/, "/index.html"); + + // Replace the received URI with the URI that includes the index page + request.uri = newuri; + + // Return to CloudFront + return callback(null, request); +}; +``` diff --git a/DevOps/AWS/AWS_Lambda/AWS_Lambda_programming_model.md b/DevOps/AWS/AWS_Lambda/AWS_Lambda_programming_model.md new file mode 100644 index 0000000..827be6f --- /dev/null +++ b/DevOps/AWS/AWS_Lambda/AWS_Lambda_programming_model.md @@ -0,0 +1,83 @@ +--- +categories: + - DevOps +tags: [AWS, aws-lambda, backend] +--- + +# AWS Lambda programming model + +The overall architecure consists in the following three processes: + +1. Triggers +2. Handler function +3. Code + +## Triggers + +See [AWS Lambda triggers](/DevOps/AWS/AWS_Lambda/AWS_Lambda_triggers.md) + +## Handler function + +Every Lambda function begins the same way with a handler function. In NodeJS: + +```js +module.exports.handler = function(event, context, callback){...} +``` + +The handler is the entry point for the Lambda. It is a function that Lambda can call to start the execution of your code when an event triggers your Lambda function. + +You may put your whole code in the body of the handler, or you may call other modules and keep the handler clean and focused on bootstrapping. + +The `module.exports` part is just the standard way of exporting a module in Node. `handler` is a property that is being exported. + +You always have access to the parameters `event`, `context` and `callback`. + +- `event` + - The triggering event of the Lambda function stored as an object. The object's contents will be different depending on the trigger. It could be any AWS service that serves a trigger (S3, DynamoDB, API Gateway etc). + - For example, if the triggering function is a request to API Gateway, the `event` object will contain information about the HTTP request such as headers, path, and the request body. +- `context` + - contains information about the runtime environment and the current execution of the Lambda function. The context object provides properties and methods that you can use to interact with the AWS Lambda service and the current invocation. +- `callback` + - an optional parameter that represents a callback function provided by AWS Lambda. The callback function allows you to signal the completion of your Lambda function's execution and return a response or an error. The callback function accepts two arguments: error and result. If the first argument is not null, AWS Lambda treats it as an error, and the Lambda function invocation fails. If the first argument is null, AWS Lambda treats the second argument as the result and considers the invocation successful. Using the callback function is more common in non-async handler functions. When you use async functions, you can return a response or throw an error directly, without using the callback function. + +### Full example: + +In JavaScript: + +```js +module.exports.handler = async (event, context) => { + // Your logic to handle the incoming event + // For example, you can call other AWS services, process the event data, etc. + + // Return a response + return { + statusCode: 200, + body: JSON.stringify({ + message: "Your Lambda function has successfully executed!", + }), + }; +}; +``` + +In Python: + +```py +import json + +def handler(event, context): + # Your logic to handle the incoming event + # For example, you can call other AWS services, process the event data, etc. + + # Return a response + response = { + 'statusCode': 200, + 'body': json.dumps({ + 'message': 'Your Lambda function has successfully executed!' + }) + } + return response +``` + +## Code + +This is just the stuff that runs in the body of the handler or that is called from the handler body. diff --git a/DevOps/AWS/AWS_Lambda/AWS_Lambda_triggers.md b/DevOps/AWS/AWS_Lambda/AWS_Lambda_triggers.md new file mode 100644 index 0000000..56bd1d2 --- /dev/null +++ b/DevOps/AWS/AWS_Lambda/AWS_Lambda_triggers.md @@ -0,0 +1,30 @@ +--- +categories: + - DevOps + - Backend +tags: [AWS, aws-lambda, node-js, python] +--- + +# AWS Lambda triggers + +Here are some common trigger patterns + +### API Gateway trigger + +- an HTTP request is sent to AWS API Gateway +- API Gateway triggers a Lambda function to execute + +### DynamoDB table change trigger + +- a record in a DynamoDB table is modified +- DynamoDB triggers a lambda function to execute + +### S3 trigger + +- a new file is created in S3 +- S3 triggers a Lambda function to execute + +### SQS trigger + +- a new message is added to an [SQS](/DevOps/AWS/AWS_Messaging_services.md#sqs) queue +- SQS triggers a Lambda function to execute diff --git a/DevOps/AWS/AWS_Lambda/What_is_AWS_Lambda.md b/DevOps/AWS/AWS_Lambda/What_is_AWS_Lambda.md deleted file mode 100644 index e69de29..0000000