Skip to content

Commit

Permalink
Deploy API connection using Terraform (#20)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DrizzlyOwl authored Jul 29, 2024
1 parent 7731600 commit c0845d5
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 24 deletions.
24 changes: 24 additions & 0 deletions terraform/api.tf
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions terraform/data.tf
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
data "azurerm_subscription" "current" {}

data "azurerm_managed_api" "container_instance_group" {
name = "aci"
location = azurerm_resource_group.default.location
}
2 changes: 2 additions & 0 deletions terraform/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
79 changes: 79 additions & 0 deletions terraform/logicapp.tf
Original file line number Diff line number Diff line change
@@ -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 = <<BODY
{
"inputs": {
"host": {
"connection": {
"name": "@parameters('$connections')['${azurerm_api_connection.linkedservice[0].name}']['connectionId']"
}
},
"method": "post",
"path": "${azurerm_container_group.default.id}/start",
"queries": {
"x-ms-api-version": "2019-12-01"
}
},
"runAfter": {},
"type": "ApiConnection"
}
BODY
}
24 changes: 0 additions & 24 deletions terraform/terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,3 @@ resource "azurerm_user_assigned_identity" "default" {

tags = local.tags
}

resource "azurerm_logic_app_workflow" "logicapp" {
name = local.resource_prefix
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name

tags = local.tags
}

resource "azurerm_monitor_diagnostic_setting" "logicapp" {
name = local.resource_prefix
target_resource_id = azurerm_logic_app_workflow.logicapp.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
}
}
11 changes: 11 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ variable "slack_webhook_url" {
type = string
default = ""
}
variable "api_connection_client_id" {
description = "Service Principal Client ID used for authenticating with the Container Instance "
type = string
default = ""
}
variable "api_connection_client_secret" {
description = "Service Principal Client Secret used for authenticating with the Container Instance"
type = string
default = ""
sensitive = true
}

0 comments on commit c0845d5

Please sign in to comment.