From 99ca12c025f9cdd03a635a7cc39e574a5973c2d6 Mon Sep 17 00:00:00 2001 From: Perry Zhu Date: Tue, 23 Jun 2026 19:43:23 -0700 Subject: [PATCH] infra: rb pexels lambda --- apps/railgunbreaker/usw2dev/iam.tf | 22 +++-------- apps/railgunbreaker/usw2dev/lambda.tf | 38 +++++++++++++++++++ apps/railgunbreaker/usw2dev/outputs.tf | 10 ----- apps/railgunbreaker/usw2dev/remote_states.tf | 11 ++++++ .../usw2dev/terraform.auto.tfvars | 1 + apps/railgunbreaker/usw2dev/variables.tf | 12 +++++- 6 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 apps/railgunbreaker/usw2dev/lambda.tf diff --git a/apps/railgunbreaker/usw2dev/iam.tf b/apps/railgunbreaker/usw2dev/iam.tf index abfb129..e6f395c 100644 --- a/apps/railgunbreaker/usw2dev/iam.tf +++ b/apps/railgunbreaker/usw2dev/iam.tf @@ -72,25 +72,13 @@ resource "aws_iam_policy" "rb_gha" { ] }, { - Sid = "AllowECRGetToken" + Sid = "AllowInvokeLambda" Effect = "Allow" Action = [ - "ecr:GetAuthorizationToken" - ], - Resource = ["*"] - }, - # { - # Sid = "AllowECRPush" - # Effect = "Allow" - # Action = [ - # "ecr:BatchCheckLayerAvailability", - # "ecr:CompleteLayerUpload", - # "ecr:InitiateLayerUpload", - # "ecr:PutImage", - # "ecr:UploadLayerPart" - # ] - # Resource = [aws_ecr_repository.lambda_container_repo.arn] - # } + "lambda:InvokeFunction" + ] + Resource = [aws_lambda_function.pexels_image_scraper.arn] + } ] }) diff --git a/apps/railgunbreaker/usw2dev/lambda.tf b/apps/railgunbreaker/usw2dev/lambda.tf new file mode 100644 index 0000000..015fcbc --- /dev/null +++ b/apps/railgunbreaker/usw2dev/lambda.tf @@ -0,0 +1,38 @@ +resource "aws_lambda_function" "pexels_image_scraper" { + function_name = "${var.project_name}-pexels-image-scraper" + role = aws_iam_role.pexels_image_scraper_lambda_exec.arn + package_type = "Image" + image_uri = "${data.terraform_remote_state.portfolio_website.outputs.lambda_ecr_repository_url}:latest" + + memory_size = 1024 + timeout = 60 + + architectures = ["x86_64"] + + environment { + variables = { + PEXELS_FEATURED_UPLOADS_URL = var.pexels_url + } + } +} + +# EventBridge rule to trigger lambda every 3 days +resource "aws_cloudwatch_event_rule" "pexels_scraper_periodic" { + name = "${var.project_name}-pexels-scraper-periodic-trigger" + description = "Trigger Pexels image scraper lambda every 3 days" + schedule_expression = "rate(3 days)" +} + +resource "aws_cloudwatch_event_target" "pexels_scraper_lambda" { + rule = aws_cloudwatch_event_rule.pexels_scraper_periodic.name + target_id = "PexelsScraperLambda" + arn = aws_lambda_function.pexels_image_scraper.arn +} + +resource "aws_lambda_permission" "allow_eventbridge" { + statement_id = "AllowExecutionFromEventBridge" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.pexels_image_scraper.function_name + principal = "events.amazonaws.com" + source_arn = aws_cloudwatch_event_rule.pexels_scraper_periodic.arn +} diff --git a/apps/railgunbreaker/usw2dev/outputs.tf b/apps/railgunbreaker/usw2dev/outputs.tf index 9ed2feb..129d4ef 100644 --- a/apps/railgunbreaker/usw2dev/outputs.tf +++ b/apps/railgunbreaker/usw2dev/outputs.tf @@ -43,16 +43,6 @@ output "acm_certificate_subject_alternative_names" { value = aws_acm_certificate.rb_website.subject_alternative_names } -# output "lambda_ecr_repository_url" { -# description = "URL of the ECR repository for the lambda container image" -# value = aws_ecr_repository.lambda_container_repo.repository_url -# } - -# output "lambda_ecr_repository_arn" { -# description = "ARN of the ECR repository for the lambda container image" -# value = aws_ecr_repository.lambda_container_repo.arn -# } - # output "event_bridge_rule_name" { # description = "Name of the EventBridge rule that triggers the Pexels image scraper lambda" # value = aws_cloudwatch_event_rule.pexels_scraper_periodic.name diff --git a/apps/railgunbreaker/usw2dev/remote_states.tf b/apps/railgunbreaker/usw2dev/remote_states.tf index 3a2238a..fe72140 100644 --- a/apps/railgunbreaker/usw2dev/remote_states.tf +++ b/apps/railgunbreaker/usw2dev/remote_states.tf @@ -19,3 +19,14 @@ data "terraform_remote_state" "dns" { } } } + +data "terraform_remote_state" "portfolio_website" { + backend = "remote" + + config = { + organization = "perry-zhu-aws" + workspaces = { + name = "portfolio-website-usw2dev" + } + } +} diff --git a/apps/railgunbreaker/usw2dev/terraform.auto.tfvars b/apps/railgunbreaker/usw2dev/terraform.auto.tfvars index 312e1ba..f993269 100644 --- a/apps/railgunbreaker/usw2dev/terraform.auto.tfvars +++ b/apps/railgunbreaker/usw2dev/terraform.auto.tfvars @@ -1,2 +1,3 @@ env_name = "usw2dev" github_repo = "RailgunBreaker/RailgunBreaker.github.io" +pexels_url = "https://www.pexels.com/@railgunbreaker/featured-uploads/" diff --git a/apps/railgunbreaker/usw2dev/variables.tf b/apps/railgunbreaker/usw2dev/variables.tf index 56f4edb..1326104 100644 --- a/apps/railgunbreaker/usw2dev/variables.tf +++ b/apps/railgunbreaker/usw2dev/variables.tf @@ -36,4 +36,14 @@ variable "subdomain" { description = "The subdomain to use for the website" type = string default = "railgunbreaker.stage" -} \ No newline at end of file +} + +variable "project_name" { + description = "The name of the project" + type = string + default = "rb" +} + +variable "pexels_url" { + type = string +}