Skip to content

Commit

Permalink
enable scheduling from cloudwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethjmyers committed Apr 13, 2024
1 parent 0f47861 commit 52efdea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The purpose of this repo is to deploy AWS Lambda function that scrapes rising an

```sh
terraform init
terraform apply
terraform plan -var-file="dev.tfvars"
terraform apply -var-file="dev.tfvars"
```
If you don't want to apply the changes to your aws account you can instead run `terraform plan`.
If you don't want to apply the changes to your aws account then just run the `terraform plan` command to see what changes would be applied.
22 changes: 22 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,25 @@ resource "aws_lambda_function" "lambda_function" {
Project = "viral-reddit-posts"
}
}

# Attach event trigger to Lambda Function, see https://stackoverflow.com/questions/35895315/use-terraform-to-set-up-a-lambda-function-triggered-by-a-scheduled-event-source
resource "aws_cloudwatch_event_rule" "every_one_minute" {
name = "every-one-minute"
description = "Fires every one minute"
schedule_expression = "rate(1 minute)"
is_enabled = var.enable_schedule
}

resource "aws_cloudwatch_event_target" "scrape_reddit_every_minute" {
rule = aws_cloudwatch_event_rule.every_one_minute.name
target_id = "scrape_reddit"
arn = aws_lambda_function.lambda_function.arn
}

resource "aws_lambda_permission" "allow_cloudwatch_to_call_lambda_function" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_function.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.every_one_minute.arn
}
5 changes: 5 additions & 0 deletions variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "enable_schedule" {
type = bool
default = false
description = "whether or not the lambda function schedule is enabled or not"
}

0 comments on commit 52efdea

Please sign in to comment.