From ed2c9f649321dcf3eecb2703c68c3647f6c7f4cf Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Fri, 15 Mar 2024 15:22:56 +0800 Subject: [PATCH 1/3] Update Signed-off-by: SpiritZhou --- terraform/main.tf | 36 ++++++++++++++++++ terraform/modules/azure/event-grid/main.tf | 37 +++++++++++++++++++ terraform/modules/azure/event-grid/outputs.tf | 7 ++++ terraform/modules/azure/event-grid/vars.tf | 25 +++++++++++++ terraform/modules/azure/service-bus/main.tf | 6 +++ .../modules/azure/service-bus/outputs.tf | 8 ++++ 6 files changed, 119 insertions(+) create mode 100644 terraform/modules/azure/event-grid/main.tf create mode 100644 terraform/modules/azure/event-grid/outputs.tf create mode 100644 terraform/modules/azure/event-grid/vars.tf diff --git a/terraform/main.tf b/terraform/main.tf index 52b8dc5..99c0d2c 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -213,6 +213,26 @@ module "azure_servicebus_namespace_alternative" { tags = local.tags } +module "azure_servicebus_namespace_event_grid" { + source = "./modules/azure/service-bus" + resource_group_name = var.azure_resource_group_name + unique_project_name = var.unique_project_name + service_bus_suffix = "-event-grid" + service_bus_admin_identities = [ + module.azuread_applications.identity_1 + ] + tags = local.tags +} + +module "azurerm_eventgrid_topic" { + source = "./modules/azure/event-grid" + resource_group_name = var.azure_resource_group_name + unique_project_name = var.unique_project_name + service_bus_topic_id = module.azure_servicebus_namespace_event_grid.event_grid_receive_topic_id + + tags = local.tags +} + module "azure_storage_account" { source = "./modules/azure/storage-account" resource_group_name = var.azure_resource_group_name @@ -378,6 +398,22 @@ module "github_secrets" { name = "TF_AZURE_RABBIT_API_APPLICATION_ID" value = module.azure_rabbitmq_app_registration.application_id }, + { + name = "TF_AZURE_SERVICE_BUS_EVENTGRID_CONNECTION_STRING" + value = module.azure_servicebus_namespace_event_grid.connection_string + }, + { + name = "TF_AZURE_EVENT_GRID_ENDPOINT" + value = module.azurerm_eventgrid_topic.endpoint + }, + { + name = "TF_AZURE_EVENT_GRID_KEY" + value = module.azurerm_eventgrid_topic.key + }, + { + name = "TF_AZURE_SB_EVENT_GRID_RECEIVE_TOPIC" + value = module.azure_servicebus_namespace_event_grid.event_grid_receive_topic + }, ] } diff --git a/terraform/modules/azure/event-grid/main.tf b/terraform/modules/azure/event-grid/main.tf new file mode 100644 index 0000000..96e14f4 --- /dev/null +++ b/terraform/modules/azure/event-grid/main.tf @@ -0,0 +1,37 @@ +provider "azurerm" { + features {} + skip_provider_registration = true +} + +locals { + event_grid_name = "${var.unique_project_name}-e2e-event-grid" + event_grid_subscription_name = "${var.unique_project_name}-e2e-event-grid-subscription" +} + +data "azurerm_resource_group" "rg" { + name = var.resource_group_name +} + +resource "azurerm_eventgrid_topic" "eventgrid" { + name = local.event_grid_name + location = data.azurerm_resource_group.rg.location + resource_group_name = data.azurerm_resource_group.rg.name + input_schema = "CloudEventSchemaV1_0" + + tags = var.tags +} + +resource "azurerm_eventgrid_event_subscription" "eventsubscription" { + name = local.event_grid_subscription_name + scope = azurerm_eventgrid_topic.eventgrid.id + event_delivery_schema = "CloudEventSchemaV1_0" + + service_bus_topic_endpoint_id = var.service_bus_topic_id +} + +resource "azurerm_role_assignment" "roles" { + count = length(var.event_grid_admin_identities) + scope = azurerm_eventgrid_topic.eventgrid.id + role_definition_name = "Azure Event Grid Owner" + principal_id = var.event_grid_admin_identities[count.index].principal_id +} \ No newline at end of file diff --git a/terraform/modules/azure/event-grid/outputs.tf b/terraform/modules/azure/event-grid/outputs.tf new file mode 100644 index 0000000..827c577 --- /dev/null +++ b/terraform/modules/azure/event-grid/outputs.tf @@ -0,0 +1,7 @@ +output "endpoint" { + value = azurerm_eventgrid_topic.eventgrid.endpoint +} + +output "key" { + value = azurerm_eventgrid_topic.eventgrid.primary_access_key +} \ No newline at end of file diff --git a/terraform/modules/azure/event-grid/vars.tf b/terraform/modules/azure/event-grid/vars.tf new file mode 100644 index 0000000..5d9730a --- /dev/null +++ b/terraform/modules/azure/event-grid/vars.tf @@ -0,0 +1,25 @@ +variable "resource_group_name" { + type = string + description = "Resource group name where event hub will be placed" +} + +variable "unique_project_name" { + type = string + description = "Value to make unique every resource name generated" +} + +variable "service_bus_topic_id" { + type = string + description = "Service bus topic to subscription events" +} + +variable "tags" { + type = map(any) + description = "Tags to apply on every resource" +} + +variable "event_grid_admin_identities" { + type = list(any) + description = "Azure Service Bus Data Owner identities" + default = [] +} \ No newline at end of file diff --git a/terraform/modules/azure/service-bus/main.tf b/terraform/modules/azure/service-bus/main.tf index 08ecaa0..2185384 100644 --- a/terraform/modules/azure/service-bus/main.tf +++ b/terraform/modules/azure/service-bus/main.tf @@ -6,6 +6,7 @@ provider "azurerm" { locals { service_bus_namespace_name = "${var.unique_project_name}-e2e-servicebus-namespace${var.service_bus_suffix}" service_bus_authorization_rule = "${var.unique_project_name}-e2e-manage" + service_bus_topic_name = "${var.unique_project_name}-e2e-receive-event-grid-topic" } data "azurerm_resource_group" "rg" { @@ -20,6 +21,11 @@ resource "azurerm_servicebus_namespace" "namespace" { tags = var.tags } +resource "azurerm_servicebus_topic" "topic" { + name = local.service_bus_topic_name + namespace_id = azurerm_servicebus_namespace.namespace.id +} + resource "azurerm_servicebus_namespace_authorization_rule" "manage" { name = local.service_bus_authorization_rule namespace_id = azurerm_servicebus_namespace.namespace.id diff --git a/terraform/modules/azure/service-bus/outputs.tf b/terraform/modules/azure/service-bus/outputs.tf index 88aca17..eabd677 100644 --- a/terraform/modules/azure/service-bus/outputs.tf +++ b/terraform/modules/azure/service-bus/outputs.tf @@ -1,3 +1,11 @@ output "connection_string" { value = azurerm_servicebus_namespace_authorization_rule.manage.primary_connection_string +} + +output "event_grid_receive_topic_id" { + value = azurerm_servicebus_topic.topic.id +} + +output "event_grid_receive_topic" { + value = azurerm_servicebus_topic.topic.name } \ No newline at end of file From 9e7d091b3259592772244a87b936fd9e386195cb Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Mon, 18 Mar 2024 11:38:37 +0800 Subject: [PATCH 2/3] Fix format Signed-off-by: SpiritZhou --- terraform/main.tf | 6 +++--- terraform/modules/azure/event-grid/main.tf | 8 ++++---- terraform/modules/azure/service-bus/main.tf | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/terraform/main.tf b/terraform/main.tf index 99c0d2c..ecec7ac 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -225,9 +225,9 @@ module "azure_servicebus_namespace_event_grid" { } module "azurerm_eventgrid_topic" { - source = "./modules/azure/event-grid" - resource_group_name = var.azure_resource_group_name - unique_project_name = var.unique_project_name + source = "./modules/azure/event-grid" + resource_group_name = var.azure_resource_group_name + unique_project_name = var.unique_project_name service_bus_topic_id = module.azure_servicebus_namespace_event_grid.event_grid_receive_topic_id tags = local.tags diff --git a/terraform/modules/azure/event-grid/main.tf b/terraform/modules/azure/event-grid/main.tf index 96e14f4..0eb254c 100644 --- a/terraform/modules/azure/event-grid/main.tf +++ b/terraform/modules/azure/event-grid/main.tf @@ -4,7 +4,7 @@ provider "azurerm" { } locals { - event_grid_name = "${var.unique_project_name}-e2e-event-grid" + event_grid_name = "${var.unique_project_name}-e2e-event-grid" event_grid_subscription_name = "${var.unique_project_name}-e2e-event-grid-subscription" } @@ -16,14 +16,14 @@ resource "azurerm_eventgrid_topic" "eventgrid" { name = local.event_grid_name location = data.azurerm_resource_group.rg.location resource_group_name = data.azurerm_resource_group.rg.name - input_schema = "CloudEventSchemaV1_0" + input_schema = "CloudEventSchemaV1_0" tags = var.tags } resource "azurerm_eventgrid_event_subscription" "eventsubscription" { - name = local.event_grid_subscription_name - scope = azurerm_eventgrid_topic.eventgrid.id + name = local.event_grid_subscription_name + scope = azurerm_eventgrid_topic.eventgrid.id event_delivery_schema = "CloudEventSchemaV1_0" service_bus_topic_endpoint_id = var.service_bus_topic_id diff --git a/terraform/modules/azure/service-bus/main.tf b/terraform/modules/azure/service-bus/main.tf index 2185384..2958c1a 100644 --- a/terraform/modules/azure/service-bus/main.tf +++ b/terraform/modules/azure/service-bus/main.tf @@ -6,7 +6,7 @@ provider "azurerm" { locals { service_bus_namespace_name = "${var.unique_project_name}-e2e-servicebus-namespace${var.service_bus_suffix}" service_bus_authorization_rule = "${var.unique_project_name}-e2e-manage" - service_bus_topic_name = "${var.unique_project_name}-e2e-receive-event-grid-topic" + service_bus_topic_name = "${var.unique_project_name}-e2e-receive-event-grid-topic" } data "azurerm_resource_group" "rg" { From 7072575751b29885a8df9a0d218e3040f2e37ca6 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Mon, 18 Mar 2024 15:41:01 +0800 Subject: [PATCH 3/3] Update name Signed-off-by: SpiritZhou --- terraform/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terraform/main.tf b/terraform/main.tf index ecec7ac..e298381 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -403,11 +403,11 @@ module "github_secrets" { value = module.azure_servicebus_namespace_event_grid.connection_string }, { - name = "TF_AZURE_EVENT_GRID_ENDPOINT" + name = "TF_AZURE_EVENT_GRID_TOPIC_ENDPOINT" value = module.azurerm_eventgrid_topic.endpoint }, { - name = "TF_AZURE_EVENT_GRID_KEY" + name = "TF_AZURE_EVENT_GRID_TOPIC_KEY" value = module.azurerm_eventgrid_topic.key }, {