Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
felipmiguel committed Aug 18, 2023
2 parents 6298718 + 2ab4421 commit 7cc6983
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 169 deletions.
5 changes: 5 additions & 0 deletions infra/core/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=psqlf-batec-ossrdbms-demo-uak-dev.postgres.database.azure.com;Database=psqlfdb-batec-ossrdbms-demo-dev;Ssl Mode=Require;Port=5432;Trust Server Certificate=true;User Id=fmiguel_outlook.com#EXT#@fmigueloutlook.onmicrosoft.com;"
}
}
32 changes: 20 additions & 12 deletions infra/core/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ terraform {
}

provider "azurerm" {
features {}
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}

locals {
Expand All @@ -39,17 +43,13 @@ resource "azurerm_resource_group" "main" {
}

module "application" {
source = "./modules/container-apps"
resource_group = azurerm_resource_group.main.name
application_name = var.application_name
environment = local.environment
location = var.location

pgsql_database_url = module.pgsql_database.database_url
pgsql_database_username = module.pgsql_database.database_username

mysql_database_url = module.mysql_database.database_url
mysql_database_username = module.mysql_database.database_username
source = "./modules/container-apps"
resource_group = azurerm_resource_group.main.name
application_name = var.application_name
environment = local.environment
location = var.location
pgsql_connection_string = module.pgsql_database.database_dotnet_connection_string
mysql_connection_string = module.mysql_database.database_dotnet_connection_string
}

module "pgsql_database" {
Expand All @@ -68,3 +68,11 @@ module "mysql_database" {
environment = local.environment
location = var.location
}

module "application_insights" {
source = "./modules/application-insights"
resource_group = azurerm_resource_group.main.name
application_name = var.application_name
environment = local.environment
location = var.location
}
8 changes: 8 additions & 0 deletions infra/core/modules/application-insights/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Terraform module for Azure Application Insights configuration

This module configures an Azure Application Insights instance with Terraform.

## Resources

[What is Azure Application Insights](https://aka.ms/nubesgen-app-insights)
[Terraform Azure Application Insights reference](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/application_insights)
26 changes: 26 additions & 0 deletions infra/core/modules/application-insights/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
terraform {
required_providers {
azurecaf = {
source = "aztfmod/azurecaf"
version = "1.2.24"
}
}
}

resource "azurecaf_name" "application_insights" {
name = var.application_name
resource_type = "azurerm_application_insights"
suffixes = [var.environment]
}

resource "azurerm_application_insights" "application_insights" {
name = azurecaf_name.application_insights.result
location = var.location
resource_group_name = var.resource_group
application_type = "other"

tags = {
"environment" = var.environment
"application-name" = var.application_name
}
}
9 changes: 9 additions & 0 deletions infra/core/modules/application-insights/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "azure_application_insights_instrumentation_key" {
value = azurerm_application_insights.application_insights.instrumentation_key
description = "The Azure Application Insights instrumentation key"
}

output "azure_application_insights_connection_string" {
value = azurerm_application_insights.application_insights.connection_string
description = "The Azure Application Insights connection string"
}
23 changes: 23 additions & 0 deletions infra/core/modules/application-insights/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "resource_group" {
type = string
description = "The resource group"
default = ""
}

variable "application_name" {
type = string
description = "The name of your application"
default = ""
}

variable "environment" {
type = string
description = "The environment (dev, test, prod...)"
default = "dev"
}

variable "location" {
type = string
description = "The Azure region where all resources in this example should be created"
default = ""
}
117 changes: 7 additions & 110 deletions infra/core/modules/container-apps/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ terraform {
}
}

locals {
database_login_name = var.application_name
mysql_connection_string = "${var.mysql_connection_string};UserID=${local.database_login_name};"
pgsql_connection_string = "${var.pgsql_connection_string};User Id=${local.database_login_name};"
}

resource "azurecaf_name" "container_registry" {
name = var.application_name
resource_type = "azurerm_container_registry"
random_length = 3
suffixes = [var.environment]
}

Expand Down Expand Up @@ -77,113 +84,3 @@ resource "azurecaf_name" "pgsql_application" {
resource_type = "azurerm_container_app"
suffixes = [var.environment, "pgsql"]
}

resource "azurerm_container_app" "pgsql_application" {
name = azurecaf_name.pgsql_application.result
container_app_environment_id = azurerm_container_app_environment.application.id
resource_group_name = var.resource_group
revision_mode = "Single"

lifecycle {
ignore_changes = [
template.0.container["image"]
]
}

identity {
type = "UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.msi_container_app.id
]
}

registry {
identity = azurerm_user_assigned_identity.msi_container_app.id
server = azurerm_container_registry.container_registry.login_server
}

ingress {
external_enabled = true
target_port = 8080
traffic_weight {
percentage = 100
latest_revision = true
}
}

template {
container {
name = azurecaf_name.pgsql_application.result
image = "ghcr.io/microsoft/nubesgen/nubesgen-native:main"
cpu = 0.25
memory = "0.5Gi"
env {
name = "DATABASE_URL"
value = var.pgsql_database_url
}
env {
name = "DATABASE_USERNAME"
value = var.pgsql_database_username
}
}
min_replicas = 1
}
}

resource "azurecaf_name" "mysql_application" {
name = var.application_name
resource_type = "azurerm_container_app"
suffixes = [var.environment, "mysql"]
}

resource "azurerm_container_app" "mysql_application" {
name = azurecaf_name.mysql_application.result
container_app_environment_id = azurerm_container_app_environment.application.id
resource_group_name = var.resource_group
revision_mode = "Single"

lifecycle {
ignore_changes = [
template.0.container["image"]
]
}

identity {
type = "UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.msi_container_app.id
]
}

registry {
identity = azurerm_user_assigned_identity.msi_container_app.id
server = azurerm_container_registry.container_registry.login_server
}

ingress {
external_enabled = true
target_port = 8080
traffic_weight {
percentage = 100
latest_revision = true
}
}

template {
container {
name = azurecaf_name.mysql_application.result
image = "ghcr.io/microsoft/nubesgen/nubesgen-native:main"
cpu = 0.25
memory = "0.5Gi"
env {
name = "DATABASE_URL"
value = var.mysql_database_url
}
env {
name = "DATABASE_USERNAME"
value = var.mysql_database_username
}
}
min_replicas = 1
}
}
32 changes: 23 additions & 9 deletions infra/core/modules/container-apps/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
output "application_pgsql_name" {
value = azurecaf_name.pgsql_application.result
description = "The application name generated by the Azure Cloud Adoption Framework."
}
# output "application_pgsql_name" {
# value = azurecaf_name.pgsql_application.result
# description = "The application name generated by the Azure Cloud Adoption Framework."
# }

output "application_mysql_name" {
value = azurecaf_name.mysql_application.result
description = "The application name generated by the Azure Cloud Adoption Framework."
}
# output "application_mysql_name" {
# value = azurecaf_name.mysql_application.result
# description = "The application name generated by the Azure Cloud Adoption Framework."
# }

output "container_registry_name" {
value = azurecaf_name.container_registry.result
value = azurerm_container_registry.container_registry.name
description = "The Docker Container Registry name generated by the Azure Cloud Adoption Framework."
}

output "container_apps_identity" {
value = azurerm_user_assigned_identity.msi_container_app.id
description = "The Managed Identity assigned to container apps"
}


output "database_login_name" {
value = local.database_login_name
}

output "container_environment_name" {
value = azurerm_container_app_environment.application.name
}
20 changes: 5 additions & 15 deletions infra/core/modules/container-apps/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,12 @@ variable "location" {
description = "The Azure region where all resources in this example should be created"
}

variable "pgsql_database_url" {
variable "pgsql_connection_string" {
type = string
description = "The URL to the database"
description = "The connection string to postgresql database"
}

variable "pgsql_database_username" {
variable "mysql_connection_string" {
type = string
description = "The database username"
}

variable "mysql_database_url" {
type = string
description = "The URL to the database"
}

variable "mysql_database_username" {
type = string
description = "The database username"
}
description = "The connection string to mysql database"
}
4 changes: 4 additions & 0 deletions infra/core/modules/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ resource "azurerm_user_assigned_identity" "mysql_umi" {
resource "azurecaf_name" "mysql_server" {
name = var.application_name
resource_type = "azurerm_mysql_server"
random_length = 3
suffixes = [var.environment]
}

Expand Down Expand Up @@ -80,6 +81,9 @@ resource "azurerm_mysql_flexible_database" "database" {
server_name = azurerm_mysql_flexible_server.database.name
charset = "utf8"
collation = "utf8_unicode_ci"
lifecycle {
ignore_changes = [charset, collation]
}
}

resource "azurecaf_name" "mysql_firewall_rule" {
Expand Down
20 changes: 17 additions & 3 deletions infra/core/modules/mysql/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
output "database_url" {
value = "${azurerm_mysql_flexible_server.database.name}.mysql.database.azure.com:3306/${azurerm_mysql_flexible_database.database.name}"
description = "The MySQL server URL."
output "database_fqdn" {
value = azurerm_mysql_flexible_server.database.fqdn
description = "The MySQL server FQDN."
}

output "database_server_name" {
value = azurerm_mysql_flexible_server.database.name
description = "The MySQL server name."
}

output "database_name" {
value = azurerm_mysql_flexible_database.database.name
description = "The MySQL database name."
}

output "database_username" {
value = azurerm_mysql_flexible_server_active_directory_administrator.aad_admin.login
description = "The MySQL server user name."
}

output "database_dotnet_connection_string" {
value= "Server=${azurerm_mysql_flexible_server.database.fqdn};Database=${azurerm_mysql_flexible_database.database.name};SslMode=Required"
}
1 change: 1 addition & 0 deletions infra/core/modules/postgresql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ data "azurerm_client_config" "current_client" {
resource "azurecaf_name" "postgresql_server" {
name = var.application_name
resource_type = "azurerm_postgresql_flexible_server"
random_length = 3
suffixes = [var.environment]
}

Expand Down
Loading

0 comments on commit 7cc6983

Please sign in to comment.