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

Fix/tf container app #42

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 70 additions & 35 deletions infra/Terraform/container-app.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ locals {
}

# [Use multiple provisoners to run multiple commands in local exec](https://tinyurl.com/mrjw6rkf)
resource "null_resource" "build_docker_image" {
resource "null_resource" "publish_docker_image" {
triggers = {
always_run = "${timestamp()}"
}

provisioner "local-exec" {
environment = {
"IMAGE" = "${local.image}"
Expand Down Expand Up @@ -41,37 +45,68 @@ resource "null_resource" "build_docker_image" {
depends_on = [azurerm_container_registry.acr]
}

# resource "azurerm_container_app_environment" "env" {
# name = "ca-env-storeonwheels-prod-sea"
# location = azurerm_resource_group.rg.location
# resource_group_name = azurerm_resource_group.rg.name
# log_analytics_workspace_id = azurerm_log_analytics_workspace.analytics.id
# }

# resource "azurerm_container_app" "app" {
# name = "ca-storeonwheels-prod-sea"
# container_app_environment_id = azurerm_container_app_environment.env.id
# resource_group_name = azurerm_resource_group.rg.name
# revision_mode = "Single"

# template {
# container {
# name = "storeonwheelsserver"
# image = "${azurerm_container_registry.acr.login_server}/${local.image}"
# cpu = 0.5
# memory = "1Gi"
# }
# }

# ingress {
# allow_insecure_connections = false
# external_enabled = true
# target_port = 4000
# # ws connection not distributed to message queues yet
# traffic_weight {
# percentage = 100
# }
# }

# depends_on = [null_resource.push_docker_image]
# }
resource "azurerm_container_app_environment" "env" {
name = "ca-env-storeonwheels-prod-sea"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
log_analytics_workspace_id = azurerm_log_analytics_workspace.analytics.id

depends_on = [null_resource.publish_docker_image]
}

resource "azurerm_user_assigned_identity" "uami" {
name = "uami-storeonwheels-prod-sea"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location

depends_on = [null_resource.publish_docker_image]
}

resource "azurerm_role_assignment" "acr_pull" {
principal_id = azurerm_user_assigned_identity.uami.principal_id
role_definition_name = "AcrPull"
scope = azurerm_container_registry.acr.id
}

resource "azurerm_container_app" "app" {
name = "ca-storeonwheels-prod-sea"
container_app_environment_id = azurerm_container_app_environment.env.id
resource_group_name = azurerm_resource_group.rg.name
revision_mode = "Single"

# needed for authentication when pulling from private ACR
# https://azureway.cloud/azure-container-apps-creating-using-terraform-part-1/
registry {
server = azurerm_container_registry.acr.login_server
identity = azurerm_user_assigned_identity.uami.id
}

# needed for container app to access other Microsoft Entra protected resources
# https://learn.microsoft.com/en-us/azure/container-apps/managed-identity?tabs=portal%2Cdotnet
identity {
type = "UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.uami.id
]
}

template {
container {
name = "storeonwheelsserver"
image = "${azurerm_container_registry.acr.login_server}/${local.image}"
cpu = 0.5
memory = "1Gi"
}
}

ingress {
allow_insecure_connections = false
external_enabled = true
target_port = 4000
# ws connection not distributed to message queues yet
traffic_weight {
latest_revision = true
percentage = 100
}
}
}
Loading