Skip to content

Commit 7fb67af

Browse files
committed
terraform keyvault fix
1 parent f736d51 commit 7fb67af

4 files changed

Lines changed: 20 additions & 72 deletions

File tree

samples/aci-blob-storage/python/terraform/deploy.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fi
2323

2424
# =============================================================================
2525
# Build and push the Docker image before Terraform deployment
26-
# (Terraform creates the ACI group referencing the image in ACR)
26+
# (Terraform references the pre-created ACR as a data source)
2727
# =============================================================================
2828

2929
# Create resource group and ACR first so we can push the image
@@ -94,15 +94,19 @@ echo "Image pushed to ACR successfully."
9494
# Terraform init, plan, and apply
9595
# =============================================================================
9696

97-
TF_VARS="-var prefix=$PREFIX -var suffix=$SUFFIX -var location=$LOCATION -var image_name=$IMAGE_NAME -var image_tag=$IMAGE_TAG"
98-
9997
echo "Initializing Terraform..."
10098
terraform init -upgrade
10199

102-
# Import the resource group and ACR that were pre-created for the image push
103-
echo "Importing pre-created resources into Terraform state..."
104-
terraform import $TF_VARS azurerm_resource_group.example "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/${RESOURCE_GROUP_NAME}" 2>/dev/null || true
105-
terraform import $TF_VARS azurerm_container_registry.example "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/${RESOURCE_GROUP_NAME}/providers/Microsoft.ContainerRegistry/registries/${ACR_NAME}" 2>/dev/null || true
100+
# Import the resource group that was pre-created for the image push
101+
echo "Importing pre-created resource group into Terraform state..."
102+
terraform import \
103+
-var "prefix=$PREFIX" \
104+
-var "suffix=$SUFFIX" \
105+
-var "location=$LOCATION" \
106+
-var "image_name=$IMAGE_NAME" \
107+
-var "image_tag=$IMAGE_TAG" \
108+
azurerm_resource_group.example \
109+
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/${RESOURCE_GROUP_NAME}" 2>/dev/null || true
106110

107111
# Run terraform plan and check for errors
108112
echo "Planning Terraform deployment..."
@@ -125,7 +129,6 @@ fi
125129
# Get the output values
126130
RESOURCE_GROUP_NAME=$(terraform output -raw resource_group_name)
127131
STORAGE_ACCOUNT_NAME=$(terraform output -raw storage_account_name)
128-
KEY_VAULT_NAME=$(terraform output -raw key_vault_name)
129132
ACR_NAME=$(terraform output -raw acr_name)
130133
ACI_GROUP_NAME=$(terraform output -raw aci_group_name)
131134
FQDN=$(terraform output -raw fqdn)
@@ -136,7 +139,6 @@ echo "Deployment Complete!"
136139
echo "============================================================"
137140
echo "Resource Group: $RESOURCE_GROUP_NAME"
138141
echo "Storage Account: $STORAGE_ACCOUNT_NAME"
139-
echo "Key Vault: $KEY_VAULT_NAME"
140142
echo "ACR: $ACR_NAME"
141143
echo "ACI Container: $ACI_GROUP_NAME"
142144
echo "FQDN: $FQDN"

samples/aci-blob-storage/python/terraform/main.tf

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
locals {
33
resource_group_name = "${var.prefix}-aci-rg"
44
storage_account_name = "${var.prefix}acistorage${var.suffix}"
5-
key_vault_name = "${var.prefix}acikv${var.suffix}"
65
acr_name = "${var.prefix}aciacr${var.suffix}"
76
aci_group_name = "${var.prefix}-aci-planner-${var.suffix}"
87
}
@@ -38,48 +37,10 @@ resource "azurerm_storage_container" "example" {
3837
container_access_type = "private"
3938
}
4039

41-
# Create Key Vault
42-
resource "azurerm_key_vault" "example" {
43-
name = local.key_vault_name
44-
resource_group_name = azurerm_resource_group.example.name
45-
location = azurerm_resource_group.example.location
46-
tenant_id = data.azurerm_client_config.current.tenant_id
47-
sku_name = "standard"
48-
enable_rbac_authorization = true
49-
purge_protection_enabled = false
50-
soft_delete_retention_days = 7
51-
tags = var.tags
52-
53-
lifecycle {
54-
ignore_changes = [
55-
tags
56-
]
57-
}
58-
}
59-
60-
data "azurerm_client_config" "current" {}
61-
62-
# Store the storage connection string in Key Vault
63-
resource "azurerm_key_vault_secret" "storage_conn" {
64-
name = "storage-conn"
65-
value = "DefaultEndpointsProtocol=http;AccountName=${azurerm_storage_account.example.name};AccountKey=${azurerm_storage_account.example.primary_access_key};BlobEndpoint=${azurerm_storage_account.example.primary_blob_endpoint}"
66-
key_vault_id = azurerm_key_vault.example.id
67-
}
68-
69-
# Create Container Registry
70-
resource "azurerm_container_registry" "example" {
40+
# Reference the pre-created ACR (created by deploy.sh before terraform apply)
41+
data "azurerm_container_registry" "example" {
7142
name = local.acr_name
7243
resource_group_name = azurerm_resource_group.example.name
73-
location = azurerm_resource_group.example.location
74-
sku = var.acr_sku
75-
admin_enabled = true
76-
tags = var.tags
77-
78-
lifecycle {
79-
ignore_changes = [
80-
tags
81-
]
82-
}
8344
}
8445

8546
# Create Container Instance
@@ -93,14 +54,14 @@ resource "azurerm_container_group" "example" {
9354
tags = var.tags
9455

9556
image_registry_credential {
96-
server = azurerm_container_registry.example.login_server
97-
username = azurerm_container_registry.example.admin_username
98-
password = azurerm_container_registry.example.admin_password
57+
server = data.azurerm_container_registry.example.login_server
58+
username = data.azurerm_container_registry.example.admin_username
59+
password = data.azurerm_container_registry.example.admin_password
9960
}
10061

10162
container {
10263
name = local.aci_group_name
103-
image = "${azurerm_container_registry.example.login_server}/${var.image_name}:${var.image_tag}"
64+
image = "${data.azurerm_container_registry.example.login_server}/${var.image_name}:${var.image_tag}"
10465
cpu = var.cpu_cores
10566
memory = var.memory_in_gb
10667

@@ -115,7 +76,7 @@ resource "azurerm_container_group" "example" {
11576
}
11677

11778
secure_environment_variables = {
118-
AZURE_STORAGE_CONNECTION_STRING = azurerm_key_vault_secret.storage_conn.value
79+
AZURE_STORAGE_CONNECTION_STRING = "DefaultEndpointsProtocol=http;AccountName=${azurerm_storage_account.example.name};AccountKey=${azurerm_storage_account.example.primary_access_key};BlobEndpoint=${azurerm_storage_account.example.primary_blob_endpoint}"
11980
}
12081
}
12182

samples/aci-blob-storage/python/terraform/outputs.tf

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ output "storage_account_name" {
66
value = azurerm_storage_account.example.name
77
}
88

9-
output "key_vault_name" {
10-
value = azurerm_key_vault.example.name
11-
}
12-
139
output "acr_name" {
14-
value = azurerm_container_registry.example.name
10+
value = data.azurerm_container_registry.example.name
1511
}
1612

1713
output "acr_login_server" {
18-
value = azurerm_container_registry.example.login_server
14+
value = data.azurerm_container_registry.example.login_server
1915
}
2016

2117
output "aci_group_name" {

samples/aci-blob-storage/python/terraform/variables.tf

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,6 @@ variable "blob_container_name" {
6666
}
6767
}
6868

69-
variable "acr_sku" {
70-
description = "(Optional) Specifies the SKU for the container registry."
71-
type = string
72-
default = "Basic"
73-
74-
validation {
75-
condition = contains(["Basic", "Standard", "Premium"], var.acr_sku)
76-
error_message = "The acr_sku must be one of: Basic, Standard, Premium."
77-
}
78-
}
79-
8069
variable "image_name" {
8170
description = "(Optional) Specifies the name of the container image."
8271
type = string

0 commit comments

Comments
 (0)