Created
June 27, 2023 16:10
-
-
Save alexjeen/799c90704ef955f1906d1ada6a69699d to your computer and use it in GitHub Desktop.
Using the EventBridge modules multiple times
This file contains 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
variable "env_level" { | |
description = "Environment level" | |
default = "dev" | |
} | |
module "eventbridge_price" { | |
source = "terraform-aws-modules/eventbridge/aws" | |
create_bus = false | |
create_connections = true | |
create_api_destinations = true | |
attach_api_destination_policy = true | |
rules = { | |
"test_room_price_${var.env_level}" = { | |
description = "Trigger test room price cron" | |
schedule_expression = "cron(20 4 * * ? *)" | |
enabled = true | |
event_pattern = null | |
} | |
} | |
targets = { | |
"test_room_price_${var.env_level}" = [ | |
{ | |
name = "send-request-to-test-room-price-${var.env_level}" | |
destination = "test_room_price_${var.env_level}" | |
attach_role_arn = true | |
} | |
] | |
} | |
connections = { | |
"test_room_price_${var.env_level}" = { | |
authorization_type = "API_KEY" | |
auth_parameters = { | |
api_key = { | |
key = "Authorization" | |
value = "Bearer testtoken" | |
} | |
} | |
} | |
} | |
api_destinations = { | |
"test_room_price_${var.env_level}" = { | |
description = "my test room price endpoint" | |
invocation_endpoint = "https://example.com/generate_cts/*" | |
http_method = "POST" | |
invocation_rate_limit_per_second = 10 | |
} | |
} | |
} | |
module "eventbridge_details" { | |
source = "terraform-aws-modules/eventbridge/aws" | |
create_bus = false | |
create_connections = true | |
create_api_destinations = true | |
attach_api_destination_policy = true | |
rules = { | |
"test_room_details_${var.env_level}" = { | |
description = "Trigger test room details cron" | |
schedule_expression = "cron(50 3 * * ? *)" | |
enabled = true | |
event_pattern = null | |
} | |
} | |
targets = { | |
"test_room_details_${var.env_level}" = [ | |
{ | |
name = "send-request-to-test-room-details-${var.env_level}" | |
destination = "test_room_details_${var.env_level}" | |
attach_role_arn = true | |
} | |
] | |
} | |
connections = { | |
"test_room_details_${var.env_level}" = { | |
authorization_type = "API_KEY" | |
auth_parameters = { | |
api_key = { | |
key = "Authorization" | |
value = "Bearer testtoken" | |
} | |
} | |
} | |
} | |
api_destinations = { | |
"test_room_details_${var.env_level}" = { | |
description = "XXX" | |
invocation_endpoint = "http://example.com/generate_cts/*" | |
http_method = "POST" | |
invocation_rate_limit_per_second = 10 | |
} | |
} | |
} |
You shouldnt really use the module from the GIT repo like that. It will cause issues later on (with your statefile) its best to use it like I mentioned:
module "eventbridge_details" {
source = "terraform-aws-modules/eventbridge/aws"
You can use a variable for the connection, like this, you can then just re-use it for the connections part so you can manage it in a central place (because how this module works, it maps by name, it needs to be there for the connection):
variable "env_level" {
description = "Environment level"
default = "dev"
}
locals {
connection = {
authorization_type = "API_KEY"
auth_parameters = {
api_key = {
key = "Authorization"
value = "Bearer testtoken"
}
}
}
}
module "eventbridge" {
source = "terraform-aws-modules/eventbridge/aws"
create_bus = false
create_connections = true
create_api_destinations = true
attach_api_destination_policy = true
rules = {
// you have two rules here, you can see the rules point at the targets below
"test_room_price_${var.env_level}" = {
description = "Trigger test room price cron"
schedule_expression = "cron(20 4 * * ? *)"
enabled = true
event_pattern = null
}
"test_room_details_${var.env_level}" = {
description = "Trigger test room details cron"
schedule_expression = "cron(50 3 * * ? *)"
enabled = true
event_pattern = null
}
}
targets = {
// you have two targets here, you can see the targets point at the api_destinations below
"test_room_price_${var.env_level}" = [
{
name = "send-request-to-test-room-price-${var.env_level}"
destination = "test_room_price_${var.env_level}"
attach_role_arn = true
}
]
"test_room_details_${var.env_level}" = [
{
name = "send-request-to-test-room-details-${var.env_level}"
destination = "test_room_details_${var.env_level}"
attach_role_arn = true
}
]
}
connections = {
"test_room_price_${var.env_level}" = local.connection
"test_room_details_${var.env_level}" = local.connection
}
api_destinations = {
// you need two destinations here, so we add two destinations
"test_room_price_${var.env_level}" = {
description = "my test room price endpoint"
invocation_endpoint = "https://example.com/generate_cts/*"
http_method = "POST"
invocation_rate_limit_per_second = 10
},
"test_room_details_${var.env_level}" = {
description = "XXX"
invocation_endpoint = "https://example.com/generate_cts/*"
http_method = "POST"
invocation_rate_limit_per_second = 10
}
}
}
The solution should be scalable.
I really appreciate your effort, sir. Thanks a lot 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is currently what I have after splitting the changes on
env.tfvars
,variables.tf
andeventbridge.tf
, I am still using module from the git repo. What I have uncertain right nowterraform validate
andterraform plan
test_info
variable?env.tfvars
variables.tf
eventbridge.tf