Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support existing container apps environment #38

Merged
merged 16 commits into from
Dec 20, 2023
113 changes: 113 additions & 0 deletions examples/existing-env/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
resource "random_id" "rg_name" {
byte_length = 8
}

resource "random_id" "env_name" {
byte_length = 8
}

resource "random_id" "container_name" {
byte_length = 4
}

resource "azurerm_resource_group" "test" {
location = var.location
name = "example-container-app-${random_id.rg_name.hex}"
}

locals {
counting_app_name = "counting-${random_id.container_name.hex}"
dashboard_app_name = "dashboard-${random_id.container_name.hex}"
}

resource "azurerm_container_app_environment" "this" {
name = "test-env"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

module "container_apps" {
source = "../.."
resource_group_name = azurerm_resource_group.test.name
location = var.location
container_app_environment_name = azurerm_container_app_environment.this.name

container_app_environment = {
name = azurerm_container_app_environment.this.name
resource_group_name = azurerm_container_app_environment.this.resource_group_name
}

container_apps = {
counting = {
name = local.counting_app_name
revision_mode = "Single"

template = {
containers = [
{
name = "countingservicetest1"
memory = "0.5Gi"
cpu = 0.25
image = "docker.io/hashicorp/counting-service:0.0.2"
env = [
{
name = "PORT"
value = "9001"
}
]
},
]
}

ingress = {
allow_insecure_connections = true
external_enabled = true
target_port = 9001
traffic_weight = {
latest_revision = true
percentage = 100
}
}
},
dashboard = {
name = local.dashboard_app_name
revision_mode = "Single"

template = {
containers = [
{
name = "testdashboard"
memory = "1Gi"
cpu = 0.5
image = "docker.io/hashicorp/dashboard-service:0.0.4"
env = [
{
name = "PORT"
value = "8080"
},
{
name = "COUNTING_SERVICE_URL"
value = "http://${local.counting_app_name}"
}
]
},
]
}

ingress = {
allow_insecure_connections = false
target_port = 8080
external_enabled = true

traffic_weight = {
latest_revision = true
percentage = 100
}
}
identity = {
type = "SystemAssigned"
}
},
}
log_analytics_workspace_name = "testlaws"
}
3 changes: 3 additions & 0 deletions examples/existing-env/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "dashboard_url" {
value = module.container_apps.container_app_fqdn["dashboard"]
}
3 changes: 3 additions & 0 deletions examples/existing-env/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "location" {
default = "eastus"
}
18 changes: 18 additions & 0 deletions examples/existing-env/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_version = ">= 1.2"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.11, < 4.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}

provider "azurerm" {
features {}
}
19 changes: 16 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
locals {
container_app_environment_id = try(data.azurerm_container_app_environment.container_env[0].id, azurerm_container_app_environment.container_env[0].id)
}

resource "azurerm_log_analytics_workspace" "laws" {
count = var.log_analytics_workspace == null ? 1 : 0

Expand All @@ -16,7 +20,16 @@ resource "azurerm_log_analytics_workspace" "laws" {
tags = var.log_analytics_workspace_tags
}

data "azurerm_container_app_environment" "container_env" {
count = var.container_app_environment != null ? 1 : 0

name = var.container_app_environment.name
resource_group_name = var.container_app_environment.resource_group_name
}

resource "azurerm_container_app_environment" "container_env" {
count = var.container_app_environment == null ? 1 : 0

location = var.location
name = var.container_app_environment_name
resource_group_name = var.resource_group_name
Expand All @@ -30,7 +43,7 @@ resource "azurerm_container_app_environment_dapr_component" "dapr" {
for_each = var.dapr_component

component_type = each.value.component_type
container_app_environment_id = azurerm_container_app_environment.container_env.id
container_app_environment_id = local.container_app_environment_id
name = each.value.name
version = each.value.version
ignore_errors = each.value.ignore_errors
Expand Down Expand Up @@ -62,15 +75,15 @@ resource "azurerm_container_app_environment_storage" "storage" {
access_key = var.environment_storage_access_key[each.key]
access_mode = each.value.access_mode
account_name = each.value.account_name
container_app_environment_id = azurerm_container_app_environment.container_env.id
container_app_environment_id = local.container_app_environment_id
name = each.value.name
share_name = each.value.share_name
}

resource "azurerm_container_app" "container_app" {
for_each = var.container_apps

container_app_environment_id = azurerm_container_app_environment.container_env.id
container_app_environment_id = local.container_app_environment_id
name = each.value.name
resource_group_name = var.resource_group_name
revision_mode = each.value.revision_mode
Expand Down
9 changes: 7 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
output "container_app_environment_id" {
description = "The ID of the Container App Environment within which this Container App should exist."
value = azurerm_container_app_environment.container_env.id
value = local.container_app_environment_id
}

output "container_app_fqdn" {
Expand All @@ -10,5 +10,10 @@ output "container_app_fqdn" {

output "container_app_ips" {
description = "The IPs of the Latest Revision of the Container App."
value = azurerm_container_app_environment.container_env.static_ip_address
value = try(azurerm_container_app_environment.container_env[0].static_ip_address, data.azurerm_container_app_environment.container_env[0].static_ip_address)
}

output "identity_ids" {
description = "The identities of the Container App."
value = { for name, container in azurerm_container_app.container_app : name => container.identity }
}
13 changes: 13 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ variable "container_app_environment_name" {
nullable = false
}

variable "container_app_environment" {
type = object({
name = string
resource_group_name = string
})
description = "Reference to existing container apps environment to use."
default = null
davidkarlsen marked this conversation as resolved.
Show resolved Hide resolved
validation {
condition = var.container_app_environment == null ? true : var.container_app_environment.name != null && var.container_app_environment.resource_group_name != null
error_message = "`name` and `resource_group_name` cannot be null"
}
}

variable "container_apps" {
type = map(object({
name = string
Expand Down
Loading