am leaning terraform , so I have written code that creates that creates 3 storage accounts with 1 container in each one. So now I need to modify it to create 3 storage accounts with 3 containers in each one.
1) main.tf file
# creation of storage accounts one for logs and another for custom scripts
# creation of resource group
resource "azurerm_resource_group" "sam_rg" {
name = var.resource_group_name
location = var.location
tags = local.tags
}
# using count feature to create 3 storage accounts
resource "azurerm_storage_account" "sampath-storage-accounts" {
count = length(var.storage-account_config)
name = "${count.index}${keys(var.storage-account_config)[count.index]}${lower(var.Environment)}"
location = azurerm_resource_group.sam_rg.location
resource_group_name = azurerm_resource_group.sam_rg.name
account_tier = var.storage-account_config[keys(var.storage-account_config)[count.index]].account_tier
account_kind = var.storage-account_config[keys(var.storage-account_config)[count.index]].account_kind
account_replication_type = var.storage-account_config[keys(var.storage-account_config)[count.index]].account_replication_type
access_tier = var.storage-account_config[keys(var.storage-account_config)[count.index]].access_tier
shared_access_key_enabled = var.storage-account_config[keys(var.storage-account_config)[count.index]].shared_access_key_enabled
min_tls_version = var.storage-account_config[keys(var.storage-account_config)[count.index]].min_tls_version
depends_on = [
azurerm_resource_group.sam_rg
]
tags = local.tags
}
#using count feature to create 3 containers in storage accounts
resource "azurerm_storage_container" "sampath-containers" {
count = length(var.storage_container-config)
name = keys(var.storage_container-config)[count.index]
storage_account_name = azurerm_storage_account.sampath-storage-accounts[count.index].name
container_access_type = var.storage_container-config[keys(var.storage_container-config)[count.index]].container_access_type
}
2).local.tf file
locals {
tags = {
"Environment" = "var.Environment"
"Project" = "var.Project"
"Team" = "Compliance"
"Location" = "data.azurerm_resource_group.sam-rg.location"
}
}
3). variable.tf file
variable "resource_group_name" {
type = string
description = "name of esource group whivh was exeisted already in resource group"
}
variable "location" {
type = string
}
variable "Environment" {
type = string
description = "creation of resources to which environment"
}
variable "Project" {
type = string
description = "reation of resources to which Project"
}
variable "storage-account_config" {
type = map(object({
account_tier = string
account_kind = string
account_replication_type = string
access_tier = string
shared_access_key_enabled = bool
min_tls_version = string
}))
description = "storage-account_config"
}
variable "storage_container-config" {
type = map(object({
container_access_type = string
}))
description = "storage_container-config"
}
4). variable.tfvars file
# name of esource group whivh was exeisted already in resource group
resource_group_name = "abp-rg-env_dev"
# location
location = "eastus"
# creation of resources to which environment
Environment = "Development"
# creation of resources to which Project
Project = "ABP-01"
# storage-account_config
storage-account_config = {
sampath = {
account_tier = "Standard"
account_kind = "StorageV2"
account_replication_type = "LRS"
access_tier = "Hot"
shared_access_key_enabled = "true"
min_tls_version = "TLS1_2"
}
sandeep = {
account_tier = "Premium"
account_kind = "StorageV2"
account_replication_type = "GRS"
access_tier = "Cool"
shared_access_key_enabled = false
min_tls_version = "TLS1_2"
}
aravind = {
account_tier = "Standard"
account_kind = "StorageV2"
account_replication_type = "ZRS"
access_tier = "Cool"
shared_access_key_enabled = false
min_tls_version = "TLS1_2"
}
}
storage_container-config = {
container01 = {
container_access_type = "private"
}
container02 = {
container_access_type = "blob"
}
container03 = {
container_access_type = "private"
}
}
so my point is that, is it possible to do with count with out using for_each , by adding for expression to loop, I couldn't figure out what to write in for expression, Please help me to create 3 storage accounts with 3 containers in each one
The following code worked for me using count .
variables.tf
variable "containers_list" {
type = list
default = [{ name = "sa1container1", access_type = "private" }, {name = "sa1container2", access_type = "private" },{name = "sa1container3", access_type = "private" }]
}
variable "Storage_list" {
type = list
default = ["kavvst1", "kavvst2","kavvst3"]
}
main.tf
#Define locals which is the product of storage accounts list and containers list number.
locals {
flat_list = setproduct(range(length(var.Storage_list)), var.containers_list)
}
resource "azurerm_storage_account" "storage_account" {
count = length(var.Storage_list)
name = var.Storage_list[count.index]
resource_group_name = data.azurerm_resource_group.example.name
location = data.azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
// Create containers according to container list for each of the storage account
resource "azurerm_storage_container" "container" {
count = length(local.flat_list)
name = local.flat_list[count.index][1].name
container_access_type = local.flat_list[count.index][1].access_type
storage_account_name = azurerm_storage_account.storage_account[local.flat_list[count.index][0]].name
}
Executed successfully on terraform apply
I have given Three storage accounts in variable list and they are created.
3 containers created in each of these storage accounts:
Also check this
How to create multiple storage accounts with specific container count in terraform - Stack Overflow for using foreach loop.