Added lambda trigger

This commit is contained in:
2024-04-18 19:09:46 +02:00
parent 4c85bbeb90
commit 0f116c4839
5 changed files with 60 additions and 5 deletions

2
.gitignore vendored
View File

@@ -196,3 +196,5 @@ override.tf.json
.terraformrc .terraformrc
terraform.rc terraform.rc
# custom builds
*.zip

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"python.analysis.typeCheckingMode": "basic"
}

View File

@@ -1,12 +1,23 @@
import boto3
s3_client = None
s3_bucket = None
def lambda_handler(event, context): def lambda_handler(event, context):
global s3_client
global s3_bucket
if s3_client is None:
s3_client = boto3.client('s3')
s3_bucket = s3_client.Bucket('standout-data')
return { return {
"statusCode": 301, "statusCode": 301,
"headers": { "headers": {
"Cache-Control": "no-cache", "Cache-Control": "no-cache",
#"Location": "https://standout-data.s3.eu-west-1.amazonaws.com/funnel-rotation-circles.jpg" #"Location": "https://standout-data.s3.eu-west-1.amazonaws.com/funnel-rotation-circles.jpg"
#"Location": "https://www.instagram.com/cosebrutteimpaginatebelle/?e=ec081328-9f83-4745-b6b7-aed1e87963df&g=5" "Location": "https://www.instagram.com/cosebrutteimpaginatebelle/?e=ec081328-9f83-4745-b6b7-aed1e87963df&g=5"
#"Location": "https://standout-data.s3.eu-west-1.amazonaws.com/Meter-Seneca.pdf" #"Location": "https://standout-data.s3.eu-west-1.amazonaws.com/Meter-Seneca.pdf"
#"Location": "https://standout-data.s3.eu-west-1.amazonaws.com/Lisa+Varano.vcf" #"Location": "https://standout-data.s3.eu-west-1.amazonaws.com/Lisa+Varano.vcf"
} }

View File

@@ -74,7 +74,6 @@ data "aws_iam_policy_document" "s3_standout_allow_lambda" {
} }
# create a redirect lambda function # create a redirect lambda function
data "aws_iam_policy_document" "lambda_role" { data "aws_iam_policy_document" "lambda_role" {
statement { statement {
effect = "Allow" effect = "Allow"
@@ -96,16 +95,16 @@ resource "aws_iam_role" "iam_for_lambda" {
data "archive_file" "lambda_standout_code" { data "archive_file" "lambda_standout_code" {
type = "zip" type = "zip"
source_file = "./lambda_redirect/lambda_redirect.py" source_file = "./lambda_redirect/lambda_redirect.py"
output_path = "standout_lambda_function.zip" output_path = "./lambda_redirect/standout_lambda_function.zip"
} }
resource "aws_lambda_function" "lambda_standout_redirect" { resource "aws_lambda_function" "lambda_standout_redirect" {
# If the file is not in the current working directory you will need to include a # If the file is not in the current working directory you will need to include a
# path.module in the filename. # path.module in the filename.
filename = "standout_lambda_function.zip" filename = "./lambda_redirect/standout_lambda_function.zip"
function_name = "standout-redirect" function_name = "standout-redirect"
role = aws_iam_role.iam_for_lambda.arn role = aws_iam_role.iam_for_lambda.arn
handler = "lambda_handler" handler = "lambda_redirect.lambda_handler"
source_code_hash = data.archive_file.lambda_standout_code.output_base64sha256 source_code_hash = data.archive_file.lambda_standout_code.output_base64sha256
@@ -118,5 +117,45 @@ resource "aws_lambda_function" "lambda_standout_redirect" {
#} #}
} }
# create API gateway for lambda triger and connect
resource "aws_apigatewayv2_api" "api_standout_gateway" {
name = "standout-api"
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_integration" "api_standout_integration" {
api_id = aws_apigatewayv2_api.api_standout_gateway.id
integration_type = "AWS_PROXY"
connection_type = "INTERNET"
description = "Lambda example"
integration_method = "POST"
integration_uri = aws_lambda_function.lambda_standout_redirect.invoke_arn
passthrough_behavior = "WHEN_NO_MATCH"
}
resource "aws_apigatewayv2_stage" "api_standout_lambda_stage" {
api_id = aws_apigatewayv2_api.api_standout_gateway.id
name = "dev"
auto_deploy = true
}
resource "aws_apigatewayv2_route" "api_standout_route" {
api_id = aws_apigatewayv2_api.api_standout_gateway.id
route_key = "GET /api"
target = "integrations/${aws_apigatewayv2_integration.api_standout_integration.id}"
}
resource "aws_lambda_permission" "api_lambda_permission" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_standout_redirect.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.api_standout_gateway.execution_arn}/*/*"
}
# create a route 53 configuration # create a route 53 configuration

Binary file not shown.