From c0845d50cd7547ca2bbc7f06cdddf83b0fcf2050 Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:45:56 +0100 Subject: [PATCH] Deploy API connection using Terraform (#20) * The API Connection is linked to the Logic App Workflow * Deploys a scheduled timer with a linked API connection so that the Container Instances can be automatically rebooted every day --- terraform/api.tf | 24 +++++++++++++ terraform/data.tf | 5 +++ terraform/locals.tf | 2 ++ terraform/logicapp.tf | 79 ++++++++++++++++++++++++++++++++++++++++++ terraform/terraform.tf | 24 ------------- terraform/variables.tf | 11 ++++++ 6 files changed, 121 insertions(+), 24 deletions(-) create mode 100644 terraform/api.tf create mode 100644 terraform/logicapp.tf diff --git a/terraform/api.tf b/terraform/api.tf new file mode 100644 index 0000000..5987ad2 --- /dev/null +++ b/terraform/api.tf @@ -0,0 +1,24 @@ +resource "azurerm_api_connection" "linkedservice" { + count = (local.api_connection_client_id != "" && local.api_connection_client_secret != "") ? 1 : 0 + + name = "aci" + resource_group_name = azurerm_resource_group.default.name + managed_api_id = data.azurerm_managed_api.container_instance_group.id + display_name = "${local.resource_prefix}-job" + + parameter_values = { + "token:clientId" : local.api_connection_client_id, + "token:clientSecret" : local.api_connection_client_secret, + "token:TenantId" : data.azurerm_subscription.current.tenant_id, + "token:grantType" : "client_credentials" + } + + lifecycle { + # NOTE: Az API does not return sensitive values so there will always be a diff without this + ignore_changes = [ + parameter_values + ] + } + + tags = local.tags +} diff --git a/terraform/data.tf b/terraform/data.tf index 634e52c..fd162cc 100644 --- a/terraform/data.tf +++ b/terraform/data.tf @@ -1 +1,6 @@ data "azurerm_subscription" "current" {} + +data "azurerm_managed_api" "container_instance_group" { + name = "aci" + location = azurerm_resource_group.default.location +} diff --git a/terraform/locals.tf b/terraform/locals.tf index 8483b18..d907261 100644 --- a/terraform/locals.tf +++ b/terraform/locals.tf @@ -16,5 +16,7 @@ locals { key_vault_access_ipv4 = var.key_vault_access_ipv4 tfvars_filename = var.tfvars_filename slack_webhook_url = var.slack_webhook_url + api_connection_client_id = var.api_connection_client_id + api_connection_client_secret = var.api_connection_client_secret tags = var.tags } diff --git a/terraform/logicapp.tf b/terraform/logicapp.tf new file mode 100644 index 0000000..b65131d --- /dev/null +++ b/terraform/logicapp.tf @@ -0,0 +1,79 @@ +resource "azurerm_logic_app_workflow" "logicapp" { + count = (local.api_connection_client_id != "" && local.api_connection_client_secret != "") ? 1 : 0 + + name = local.resource_prefix + location = azurerm_resource_group.default.location + resource_group_name = azurerm_resource_group.default.name + + parameters = { "$connections" = jsonencode({ + "${azurerm_api_connection.linkedservice[0].name}" = { + connectionId = azurerm_api_connection.linkedservice[0].id + connectionName = azurerm_api_connection.linkedservice[0].name + id = data.azurerm_managed_api.container_instance_group.id + } + }) } + + workflow_parameters = { "$connections" = jsonencode({ + defaultValue = {} + type = "Object" + }) } + + tags = local.tags +} + +resource "azurerm_monitor_diagnostic_setting" "logicapp" { + count = (local.api_connection_client_id != "" && local.api_connection_client_secret != "") ? 1 : 0 + + name = local.resource_prefix + target_resource_id = azurerm_logic_app_workflow.logicapp[0].id + log_analytics_workspace_id = azurerm_log_analytics_workspace.default.id + + enabled_log { + category = "WorkflowRuntime" + } + + # The below metrics are kept in to avoid a diff in the Terraform Plan output + metric { + category = "AllMetrics" + enabled = false + } +} + +resource "azurerm_logic_app_trigger_recurrence" "start" { + count = (local.api_connection_client_id != "" && local.api_connection_client_secret != "") ? 1 : 0 + + name = "scheduled-start" + time_zone = "W. Europe Standard Time" + logic_app_id = azurerm_logic_app_workflow.logicapp[0].id + frequency = "Day" + interval = 1 + + schedule { + at_these_hours = [06] + at_these_minutes = [30] + } +} + +resource "azurerm_logic_app_action_custom" "start" { + name = "start-aci" + logic_app_id = azurerm_logic_app_workflow.logicapp[0].id + + body = <