Skip to content

Commit 2eb0db4

Browse files
Copilotsayedimac
andcommitted
Add Terraform equivalents for all Bicep infrastructure code
Co-authored-by: sayedimac <25403967+sayedimac@users.noreply.github.com>
1 parent 42b594d commit 2eb0db4

11 files changed

Lines changed: 521 additions & 0 deletions

File tree

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,14 @@ FodyWeavers.xsd
396396

397397
# JetBrains Rider
398398
*.sln.iml
399+
400+
# Terraform
401+
.terraform/
402+
.terraform.lock.hcl
403+
*.tfstate
404+
*.tfstate.*
405+
*.tfvars
406+
*.tfplan
407+
.terraformrc
408+
terraform.rc
409+

azd/aca/main.tf

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "~> 3.0"
6+
}
7+
random = {
8+
source = "hashicorp/random"
9+
version = "~> 3.0"
10+
}
11+
}
12+
}
13+
14+
provider "azurerm" {
15+
features {}
16+
}
17+
18+
# Generate a unique resource token
19+
resource "random_string" "resource_token" {
20+
length = 13
21+
special = false
22+
upper = false
23+
numeric = true
24+
}
25+
26+
# Resource Group
27+
resource "azurerm_resource_group" "main" {
28+
name = "rg-${var.environment_name}"
29+
location = var.location
30+
31+
tags = {
32+
"azd-env-name" = var.environment_name
33+
}
34+
}
35+
36+
# Container Registry
37+
resource "azurerm_container_registry" "acr" {
38+
name = "acr${random_string.resource_token.result}"
39+
location = azurerm_resource_group.main.location
40+
resource_group_name = azurerm_resource_group.main.name
41+
sku = "Basic"
42+
admin_enabled = true
43+
}
44+
45+
# Container Apps Environment
46+
resource "azurerm_container_app_environment" "env" {
47+
name = "cae-${random_string.resource_token.result}"
48+
location = azurerm_resource_group.main.location
49+
resource_group_name = azurerm_resource_group.main.name
50+
}
51+
52+
# Container App
53+
resource "azurerm_container_app" "app" {
54+
name = "ca-${random_string.resource_token.result}"
55+
container_app_environment_id = azurerm_container_app_environment.env.id
56+
resource_group_name = azurerm_resource_group.main.name
57+
revision_mode = "Single"
58+
59+
registry {
60+
server = azurerm_container_registry.acr.login_server
61+
username = azurerm_container_registry.acr.admin_username
62+
password_secret_name = "registry-password"
63+
}
64+
65+
secret {
66+
name = "registry-password"
67+
value = azurerm_container_registry.acr.admin_password
68+
}
69+
70+
template {
71+
container {
72+
name = "ca-${random_string.resource_token.result}"
73+
image = var.container_image != "" ? "${azurerm_container_registry.acr.login_server}/${var.container_image}:latest" : "${azurerm_container_registry.acr.login_server}/docker-app:latest"
74+
cpu = var.cpu_cores
75+
memory = var.memory_in_gb
76+
}
77+
78+
min_replicas = 1
79+
max_replicas = 3
80+
81+
http_scale_rule {
82+
name = "http-scale-rule"
83+
concurrent_requests = "10"
84+
}
85+
}
86+
87+
ingress {
88+
external_enabled = true
89+
target_port = var.container_port
90+
traffic_weight {
91+
latest_revision = true
92+
percentage = 100
93+
}
94+
}
95+
}

azd/aca/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "AZURE_LOCATION" {
2+
value = var.location
3+
description = "Azure location where resources are deployed"
4+
}
5+
6+
output "AZURE_CONTAINER_REGISTRY_ENDPOINT" {
7+
value = azurerm_container_registry.acr.login_server
8+
description = "Container registry login server"
9+
}
10+
11+
output "AZURE_CONTAINER_REGISTRY_NAME" {
12+
value = azurerm_container_registry.acr.name
13+
description = "Container registry name"
14+
}
15+
16+
output "CONTAINER_APP_URL" {
17+
value = "https://${azurerm_container_app.app.latest_revision_fqdn}"
18+
description = "Container App URL"
19+
}

azd/aca/variables.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
variable "environment_name" {
2+
type = string
3+
description = "Name of the environment which is used to generate a short unique hash used in all resources."
4+
5+
validation {
6+
condition = length(var.environment_name) >= 1 && length(var.environment_name) <= 64
7+
error_message = "Environment name must be between 1 and 64 characters."
8+
}
9+
}
10+
11+
variable "location" {
12+
type = string
13+
description = "Primary location for all resources"
14+
15+
validation {
16+
condition = length(var.location) >= 1
17+
error_message = "Location must be specified."
18+
}
19+
}
20+
21+
variable "container_image" {
22+
type = string
23+
description = "The container image to deploy"
24+
default = ""
25+
}
26+
27+
variable "container_port" {
28+
type = number
29+
description = "Port the container listens on"
30+
default = 80
31+
}
32+
33+
variable "cpu_cores" {
34+
type = string
35+
description = "CPU cores allocated to the container"
36+
default = "0.5"
37+
}
38+
39+
variable "memory_in_gb" {
40+
type = string
41+
description = "Memory allocated to the container in GB"
42+
default = "1Gi"
43+
}

azd/aci/main.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "~> 3.0"
6+
}
7+
random = {
8+
source = "hashicorp/random"
9+
version = "~> 3.0"
10+
}
11+
}
12+
}
13+
14+
provider "azurerm" {
15+
features {}
16+
}
17+
18+
# Generate a unique resource token
19+
resource "random_string" "resource_token" {
20+
length = 13
21+
special = false
22+
upper = false
23+
numeric = true
24+
}
25+
26+
# Resource Group
27+
resource "azurerm_resource_group" "main" {
28+
name = "rg-${var.environment_name}"
29+
location = var.location
30+
31+
tags = {
32+
"azd-env-name" = var.environment_name
33+
}
34+
}
35+
36+
# Container Registry
37+
resource "azurerm_container_registry" "acr" {
38+
name = "acr${random_string.resource_token.result}"
39+
location = azurerm_resource_group.main.location
40+
resource_group_name = azurerm_resource_group.main.name
41+
sku = "Basic"
42+
admin_enabled = true
43+
}
44+
45+
# Container Instance
46+
resource "azurerm_container_group" "main" {
47+
name = "aci-${random_string.resource_token.result}"
48+
location = azurerm_resource_group.main.location
49+
resource_group_name = azurerm_resource_group.main.name
50+
ip_address_type = "Public"
51+
os_type = "Linux"
52+
restart_policy = "Always"
53+
54+
tags = {
55+
"azd-env-name" = var.environment_name
56+
}
57+
58+
container {
59+
name = "aci-${random_string.resource_token.result}"
60+
image = var.container_image != "" ? var.container_image : "mcr.microsoft.com/azuredocs/aci-helloworld"
61+
cpu = var.cpu_cores
62+
memory = var.memory_in_gb
63+
64+
ports {
65+
port = var.container_port
66+
protocol = "TCP"
67+
}
68+
}
69+
70+
image_registry_credential {
71+
server = azurerm_container_registry.acr.login_server
72+
username = azurerm_container_registry.acr.admin_username
73+
password = azurerm_container_registry.acr.admin_password
74+
}
75+
}

azd/aci/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "AZURE_LOCATION" {
2+
value = var.location
3+
description = "Azure location where resources are deployed"
4+
}
5+
6+
output "AZURE_CONTAINER_REGISTRY_ENDPOINT" {
7+
value = azurerm_container_registry.acr.login_server
8+
description = "Container registry login server"
9+
}
10+
11+
output "AZURE_CONTAINER_REGISTRY_NAME" {
12+
value = azurerm_container_registry.acr.name
13+
description = "Container registry name"
14+
}
15+
16+
output "ACI_URI" {
17+
value = "http://${azurerm_container_group.main.ip_address}:${var.container_port}"
18+
description = "Container Instance URI"
19+
}

azd/aci/variables.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
variable "environment_name" {
2+
type = string
3+
description = "Name of the environment which is used to generate a short unique hash used in all resources."
4+
5+
validation {
6+
condition = length(var.environment_name) >= 1 && length(var.environment_name) <= 64
7+
error_message = "Environment name must be between 1 and 64 characters."
8+
}
9+
}
10+
11+
variable "location" {
12+
type = string
13+
description = "Primary location for all resources"
14+
15+
validation {
16+
condition = length(var.location) >= 1
17+
error_message = "Location must be specified."
18+
}
19+
}
20+
21+
variable "container_image" {
22+
type = string
23+
description = "The container image to deploy"
24+
default = ""
25+
}
26+
27+
variable "container_port" {
28+
type = number
29+
description = "Port the container listens on"
30+
default = 80
31+
}
32+
33+
variable "cpu_cores" {
34+
type = string
35+
description = "CPU cores allocated to the container instance"
36+
default = "1.0"
37+
}
38+
39+
variable "memory_in_gb" {
40+
type = string
41+
description = "Memory allocated to the container instance in GB"
42+
default = "1.5"
43+
}

azd/acr/modules/acr.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Azure Container Registry
2+
resource "azurerm_container_registry" "acr" {
3+
name = var.registry_name
4+
location = var.location
5+
resource_group_name = var.resource_group_name
6+
sku = var.sku
7+
admin_enabled = var.admin_enabled
8+
}
9+
10+
output "acr_id" {
11+
value = azurerm_container_registry.acr.id
12+
description = "Container registry ID"
13+
}
14+
15+
output "acr_name" {
16+
value = azurerm_container_registry.acr.name
17+
description = "Container registry name"
18+
}
19+
20+
output "acr_login_server" {
21+
value = azurerm_container_registry.acr.login_server
22+
description = "Container registry login server"
23+
}
24+
25+
variable "registry_name" {
26+
type = string
27+
description = "Name of the container registry"
28+
}
29+
30+
variable "location" {
31+
type = string
32+
description = "Azure region location"
33+
default = null
34+
}
35+
36+
variable "resource_group_name" {
37+
type = string
38+
description = "Name of the resource group"
39+
default = null
40+
}
41+
42+
variable "sku" {
43+
type = string
44+
description = "SKU for the container registry"
45+
default = "Basic"
46+
}
47+
48+
variable "admin_enabled" {
49+
type = bool
50+
description = "Enable admin user for the registry"
51+
default = true
52+
}

0 commit comments

Comments
 (0)