Reputation: 13
I would like to define and declare same resources of different resource groups in terraform.
The end goal is to get all the resources created which are part of the specific resource group which I select from the command line when I run terraform plan. Below is the terraform script I tried to run but not sure whether the below definition an declaration is correct.Please suggest me how to write or format this script. I am new to terraform ad explore many blogs to create the below script but need clarification.
Here is the below 2 resource groups with resource variable names. var.tf:
variable "properties" {
type = object( {
resourcegroup_name = string
location = string
keyvault_name = string
Networksecuritygroup_name = string
Storageaccount_name = string
Storageaccount_dev = string
APImanagement_name = string
APP-serviceplan_name = string
webappservice_name = string
SQLDatabase_name = string
NetworkInterface_name = string
VirtualMachine_name = string
EndPoint = string
CDNProfile = string
ApplicationInsigt_name = string
TrafficManagerProfile_name = string
SQLServer_name = string
Subnet_name = string
})
variable "resourcegroup1" {
default = [{
resourcegroup_name = "001-rg"
location = "UK South"
keyvault_name = "kv-001"
TrafficManagerProfile_name = "tmp-001"
Storageaccount_name = "stg001"
APImanagement_name = "apm-001"
APPserviceplan_name = "asp-001"
webappservice_name = "was-001"
Storageaccount_dev = "devstg001"
SQLDatabase_name = "DB-001"
NetworkInterface_name = "NIC-01"
VirtualMachine_name = "VM001"
EndPoint1 = "stg001 (stg001/stg001)"
CDNProfile1 = "cdn_001"
EndPoint2 = "devstg001 (devstg001/devstg001)"
CDNProfile2 = "cdn_dev001"
ApplicationInsigt_name = "AI-001"
SQLServer_name = "Db-server-001"
Subnet_name = "subnet001"
}]
},
variable "resourcegroup2" {
default = [{
resourcegroup_name = "002-rg"
location = "UK South"
keyvault_name = "kv-002"
TrafficManagerProfile_name = "tmp-002"
Storageaccount_name = "stg002"
APImanagement_name = "apm-002"
APPserviceplan_name = "asp-002"
webappservice_name = "was-002"
Storageaccount_dev = "devstg002"
SQLDatabase_name = "DB-002"
NetworkInterface_name = "NIC-02"
VirtualMachine_name = "VM002"
EndPoint1 = "stg002 (stg002/stg002)"
CDNProfile1 = "cdn_002"
EndPoint2 = "devstg002 (devstg002/devstg002)"
CDNProfile2 = "cdn_dev002"
ApplicationInsigt_name = "AI-002"
SQLServer_name = "Db-server-002"
Subnet_name = "subnet002"
}]
}
main.tf:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.90.0"
}
}
}
provider "azurerm" {
# Configuration options
features{}
}
data "azurerm_client_config" "current" {}
resource "azurerm_key_vault" "keyvault" {
name = "${var.keyvault_name}"
location = "${var.location}"
resource_group_name = "${var.resourcegroup_name}"
enabled_for_disk_encryption = true
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
sku_name = "standard"
access_policy {
tenant_id = "987654"
object_id = "123456"
key_permissions = [
"Get",
]
secret_permissions = [
"Get",
]
storage_permissions = [
"Get",
]
}
}
#---------------------------------------API Management Serrvice---------------------------------------
variable "apimAdminEmail"{
default= "[email protected]"
}
variable "apimTier"{
default= "Developer_1"
}
variable "apimCapacity"{
default= "1"
}
variable "hostName"{
default= "api3"
}
resource "azurerm_api_management" "APIM" {
name = "${var.APImanagement_name}"
location = "${var.location}"
resource_group_name = "${var.resourcegroup_name}"
publisher_name = "API"
publisher_email = "${var.apimAdminEmail}"
sku_name = "${var.apimTier}"
#capacity = "${var.apimCapacity}"
identity {
type = "SystemAssigned"
}
}
Upvotes: 0
Views: 1202
Reputation: 116
I am not quite sure about your question but I am assuming that you want two deploy two different environments using a single main.tf ( common template for two different environments) example: Dev and Stage environment.
If that is your case, Configure two separate variable.tf files for each environment and pass the variable.tf file to when initializing the terraform.
Please check the below Example
dev.tfvars
resourcegroup_name = "002-rg"
location = "UK South"
keyvault_name = "kv-002"
TrafficManagerProfile_name = "tmp-002"
stage.tfvars
resourcegroup_name = "001-rg"
location = "UK South"
keyvault_name = "kv-001"
TrafficManagerProfile_name = "tmp-001"
variables.tf
variable "resourcegroup_name" {
default = ""
}
variable "location" {
default = ""
}
....
terraform init
terraform plan -var-file="dev.tfvars"
terraform apply -var-file="dev.tfvars"
Upvotes: 1