Created
August 7, 2023 21:41
-
-
Save lkrimphove/d577f7886874ae474fde7c6034e190a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### LAMBDA | |
module "lambda_function" { | |
source = "terraform-aws-modules/lambda/aws" | |
function_name = "outdoor-activities-generator" | |
description = "Generates a map containing your outdoor activities" | |
handler = "main.lambda_handler" | |
runtime = "python3.11" | |
timeout = 60 | |
source_path = "../src/lambda" | |
environment_variables = { | |
START_LATITUDE = var.start_latitude | |
START_LONGITUDE = var.start_longitude | |
ZOOM_START = var.zoom_start | |
INPUT_BUCKET = module.input_bucket.s3_bucket_id | |
OUTPUT_BUCKET = module.output_bucket.s3_bucket_id | |
S3_OBJECT_NAME = "map.html" | |
CLOUDFRONT_DISTRIBUTION_ID = module.cloudfront.cloudfront_distribution_id | |
} | |
layers = [ | |
module.lambda_layer.lambda_layer_arn, | |
] | |
attach_policy = true | |
policy = aws_iam_policy.lambda_policy.arn | |
} | |
module "lambda_layer" { | |
source = "terraform-aws-modules/lambda/aws" | |
create_function = false | |
create_layer = true | |
layer_name = "outdoor-activities-layer" | |
description = "Lambda layer containing everything for Outdoor Activities" | |
compatible_runtimes = ["python3.11"] | |
runtime = "python3.11" | |
source_path = [ | |
{ | |
path = "../src/lambda-layer" | |
pip_requirements = true | |
prefix_in_zip = "python" # required to get the path correct | |
} | |
] | |
} | |
resource "aws_iam_policy" "lambda_policy" { | |
name = "outdoor-activities-generator-policy" | |
policy = jsonencode({ | |
Version = "2012-10-17" | |
Statement = [ | |
{ | |
Action = "s3:GetObject" | |
Effect = "Allow" | |
Resource = "${module.input_bucket.s3_bucket_arn}/*" | |
}, | |
{ | |
Action = "s3:ListBucket" | |
Effect = "Allow" | |
Resource = module.input_bucket.s3_bucket_arn | |
}, | |
{ | |
Action = "s3:PutObject" | |
Effect = "Allow" | |
Resource = "${module.output_bucket.s3_bucket_arn}/*" | |
}, | |
{ | |
Action = "cloudfront:GetDistribution" | |
Effect = "Allow" | |
Resource = module.cloudfront.cloudfront_distribution_arn | |
}, | |
{ | |
Action = "cloudfront:CreateInvalidation" | |
Effect = "Allow" | |
Resource = module.cloudfront.cloudfront_distribution_arn | |
} | |
] | |
}) | |
} | |
resource "aws_lambda_permission" "allow_bucket" { | |
statement_id = "AllowExecutionFromS3Bucket" | |
action = "lambda:InvokeFunction" | |
function_name = module.lambda_function.lambda_function_arn | |
principal = "s3.amazonaws.com" | |
source_arn = module.input_bucket.s3_bucket_arn | |
} | |
resource "aws_s3_bucket_notification" "bucket_notification" { | |
bucket = module.input_bucket.s3_bucket_id | |
lambda_function { | |
lambda_function_arn = module.lambda_function.lambda_function_arn | |
events = ["s3:ObjectCreated:*"] | |
} | |
depends_on = [aws_lambda_permission.allow_bucket] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment