$ terraform -v
Terraform v0.14.6
I have a Terraform plan that sets up alarms for some of my AWS ECS services, and it looks like this
module "ecs_high_cpu_service_aaa_alarm" {
source = "../modules/cw_alarm"
alarm_name = "ecs-high-cpu-service-aaa-alarm"
service_name = "service-aaa"
// Other parameters
}
module "ecs_high_cpu_service_bbb_alarm" {
source = "../modules/cw_alarm"
alarm_name = "ecs-high-cpu-service-bbb-alarm"
service_name = "service-bbb"
// Other parameters
}
module "ecs_high_cpu_service_123_alarm" {
source = "../modules/cw_alarm"
alarm_name = "ecs-high-cpu-service-123-alarm"
service_name = "service-123"
// Other parameters
}
// More alarms with similar setup as above
As you can see, the modules are all set up similarly, differing only in name, alarm_name and service_name parameters. Is there a way to setup a for-loop that will loop over a map to set the modules up for the plan?
From Mark B's (thanks!) comment, this works.
variables.tf
------------
variable "service_map" {
type = map
default = {
service-aaa = "ecs-high-cpu-service-aaa-alarm"
service-bbb = "ecs-high-cpu-service-bbb-alarm"
service-123 = "ecs-high-cpu-service-123-alarm"
}
description = "Service map; key = service name, value = alarm name"
}
main.tf
-------
module "alarms" {
for_each = var.service_map
source = "../modules/cw_alarm"
service_name = each.key
alarm_name = each.value
// Other parameters
}
Related
I have this script which works great. It created 3 instances with the sepcified tags to identify them easily. But issue is i want to add a remote-exec provisioner (currently commented) to the code to install some packages. If i was using count, i could have looped over it to do the remote-exec over all the instances. I could not use count because i had to use for_each to loop over a local list. Since count and for_each cannot be used together, how do i loop over the instances to retrieve their IP addresses for using in the remote-exec provisioner.
On digital ocean and AWS, i was able to get it work using host = "${self.public_ip}"
But it does not work on vultr and gives the Unsupported attribute error
instance.tf
resource "vultr_ssh_key" "kubernetes" {
name = "kubernetes"
ssh_key = file("kubernetes.pub")
}
resource "vultr_instance" "kubernetes_instance" {
for_each = toset(local.expanded_names)
plan = "vc2-1c-2gb"
region = "sgp"
os_id = "387"
label = each.value
tag = each.value
hostname = each.value
enable_ipv6 = true
backups = "disabled"
ddos_protection = false
activation_email = false
ssh_key_ids = [vultr_ssh_key.kubernetes.id]
/* connection {
type = "ssh"
user = "root"
private_key = file("kubernetes")
timeout = "2m"
host = vultr_instance.kubernetes_instance[each.key].ipv4_address
}
provisioner "remote-exec" {
inline = "sudo hostnamectl set-hostname ${each.value}"
} */
}
locals {
expanded_names = flatten([
for name, count in var.host_name : [
for i in range(count) : format("%s-%02d", name, i + 1)
]
])
}
provider.tf
terraform {
required_providers {
vultr = {
source = "vultr/vultr"
version = "2.3.1"
}
}
}
provider "vultr" {
api_key = "***************************"
rate_limit = 700
retry_limit = 3
}
variables.tf
variable "host_name" {
type = map(number)
default = {
"Manager" = 1
"Worker" = 2
}
}
The property you are looking for is called main_ip instead of ip4_address or something like that. Specifically accessible via self.main_ip in your connection block.
I have the following code that will call a module and make target groups for me based off of the information I pass it in the locals variables. This works just fine, my issue being trying to get the arn of each target group it creates in an output.
locals {
targetgroups_beta = {
targetgroup1 = {
name = "example",
path = "/",
environment = "Beta"
}
targetgroup2 = {
name = "example2",
path = "/",
environment = "Beta"
}
}
}
module "target-groups"{
for_each = local.targetgroups_beta
source = ".//modules/targetgroups"
name-beta = each.value.name
path-beta = each.value.path
environment-beta = each.value.environment
vpc-id = "${module.vpc.vpc_id}"
}
The resource name in the module it is calling is target-group, so based off of what I read I should be able to refer to it by something like this:
output{
value = "${aws_lb_target_group.target-group[0].arn}"
}
When I try this I receive the following when running a terraform plan:
"Because aws_lb_target_group.target-group does not have "count" or "for_each" set, references to it must not include an index key. Remove the bracketed index to refer to the single instance of this resource."
My understanding of this is the module that the for_each is calling isn't running a for_each, so I cannot reference the resources in this way. If I do ""${aws_lb_target_group.target-group.arn}" that reference works technically, but includes the arn for every target group and I plan on adding a lot more. Is there a way to take each of these arns out of this list as its own output?
Code in the module that it is calling for reference:
resource "aws_lb_target_group" "target-group" {
name = "example-${var.name-beta}"
port = 80
protocol = "HTTP"
vpc_id = var.vpc-id
deregistration_delay = 5
tags = {
Environment = "${var.environment-beta}"
}
health_check{
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 10
interval = 15
path = var.path-beta
}
}
If I correctly understand, you are using for_each in your target-groups module. If so to get the outputs, you would have to use something like the following in your main.tf file:
module.target-groups[*].arn
The for_each will create multiple modules, not multiple resources in a single module.
Here is good info on using for_each and count with modules in terraform 0.13.
Update for one module
If you want to use only one module, you can do the following:
module "target-groups"{
target_groups_to_create = local.targetgroups_beta
source = ".//modules/targetgroups"
name-beta = each.value.name
path-beta = each.value.path
environment-beta = each.value.environment
vpc-id = "${module.vpc.vpc_id}"
}
Then in the module:
variable "target_groups_to_create" {}
resource "aws_lb_target_group" "target-group" {
for_each = var.target_groups_to_create
name = "example-${each.value.name}"
port = 80
protocol = "HTTP"
vpc_id = var.vpc-id
deregistration_delay = 5
tags = {
Environment = "${each.value.environment}"
}
health_check{
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 10
interval = 15
path = each.value.path
}
}
My vpc state file is at dev/vpc/main/terraform.tfstate. I would like to prevision mariaDB using the private subnets in vpc.
I got errors:
Error: Error running plan: 1 error(s) occurred:
module.csc_db_sbnet_group.var.db_subnet_group_ids: element: element() may not be used with an empty list in:
${element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,0)}
MariaDB code (note: MariaDB code has its own terrafrom state file):
data_sources.tf:
data "terraform_remote_state" "vpc_subnets_ids" {
backend = "s3"
config {
bucket = "dev-terraform-state"
key = "dev/vpc/main/terraform.tfstate"
region = "us-west-2"
}
}
resources.tf:
module "csc_db_sbnet_group" {
source = "modules/rds-subnet-group"
db_subnet_group_name = "${var.db_subnet_group_name}"
db_subnet_group_ids = ["${element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,0)}", "${element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,1)}"]
}
VPC code:
resources.tf:
module "vpc" {
source = "modules/vpc"
aws_region = "${var.region}"
vpc_tag_name = "${var.vpc_name}"
vpc_cidr = "${var.vpccidr}"
private-subnet-mapping = ["${var.private_az_subnets_cidr}"]
public-subnet-mapping = ["${var.public_az_subnets_cidr}"]
}
resource "aws_subnet" "add_private_subnets" {
count = "${length(var.private-subnet-mapping)}"
cidr_block = "${lookup(var.private-subnet-mapping[count.index], "cidr")}"
vpc_id = "${module.vpc.vpc_id}"
availability_zone = "${lookup(var.private-subnet-mapping[count.index], "az")}"
tags = {
Name = "${lookup(var.private-subnet-mapping[count.index], "name")}"
BU = "${lookup(var.private-subnet-mapping[count.index], "BU")}"
}
}
outputs.tf
output "vpc_private_subnets_ids" {
value = ["${aws_subnet.add_private_subnets.*.id}"]
}
output "vpc_private_subnets_cidrs" {
value = ["${aws_subnet.add_private_subnets.*.cidr_block}"]
}
When I provision MariaDB, I would like to use the private subnets provisioned in VPC code. But, it complains,
module.csc_db_sbnet_group.var.db_subnet_group_ids: element: element() may not be used with an empty list in:
${element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,0)}
I added code below for VPC and solved the problem.
data "aws_subnet_ids" "private_subnet_ids" {
vpc_id = "${module.vpc.vpc_id}"
}
data "aws_subnet" "private_subnet" {
count = "${length(data.aws_subnet_ids.private_subnet_ids.ids)}"
id = "${data.aws_subnet_ids.private_subnet_ids.ids[count.index]}"
}
I'm starting to use (and learn) terraform, for now, I need to create multiple DO droplets and attach them to the aws route53 zone, what I'm trying to do:
My DO terraform file:
# Configure the DigitalOcean Provider
provider "digitalocean" {
token = var.do_token
}
# Create a new tag
resource "digitalocean_tag" "victor" {
name = "victor-fee1good22"
}
resource "digitalocean_droplet" "web" {
count = 2
image = var.do_config["image"]
name = "web-${count.index}"
region = var.do_config["region"]
size = var.do_config["size"]
ssh_keys = [var.public_ssh_key, var.pv_ssh_key]
tags = [digitalocean_tag.victor.name]
}
My route53 file:
provider "aws" {
version = "~> 2.0"
region = "us-east-1"
access_key = var.aws_a_key
secret_key = var.aws_s_key
}
data "aws_route53_zone" "selected" {
name = "devops.rebrain.srwx.net"
}
resource "aws_route53_record" "www" {
сount = length(digitalocean_droplet.web)
zone_id = data.aws_route53_zone.selected.zone_id
name = "web_${count.index}"
type = "A"
ttl = "300"
records = [digitalocean_droplet.web[count.index].ipv4_address]
}
But I always get The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set. error, what did I wrong?
Thanks!
UPDATE:
I've resolved this one like — add сount = 2 instead of сount = length(digitalocean_droplet.web)
It works but would be better to have the dynamic variable instead of constant count. :)
you want to get number of services, that not yet created. Terraform couldn't do that.
As I think simplest way use common var with the number of droplets.
resource "digitalocean_droplet" "test" {
count = var.number_of_vps
image = "ubuntu-18-04-x64"
name = "test-1"
region = data.digitalocean_regions.available.regions[0].slug
size = "s-1vcpu-1gb"
}
resource "aws_route53_record" "test" {
count = var.number_of_vps
zone_id = data.aws_route53_zone.primary.zone_id
name = "${local.login}-${count.index}.${data.aws_route53_zone.primary.name}"
type = "A"
ttl = "300"
records = [digitalocean_droplet.test[count.index].ipv4_address]
}
This trick helped - https://github.com/hashicorp/terraform/issues/12570#issuecomment-291517239
resource "aws_route53_record" "dns" {
count = "${length(var.ips) > 0 ? length(var.domains) : 0}"
// ...
}
I am trying to work with terraform modules to create event subscription pointing to storage queue as an endpoint to it.
Below is the module
resource "azurerm_eventgrid_event_subscription" "events" {
name = var.name
scope = var.scope
subject_filter = var.subject_filter
storage_queue_endpoint = var.storage_queue_endpoint
}
and terraform is
module "storage_account__event_subscription" {
source = "../modules/event"
name = "testevent"
scope = test
subject_filter = {
subject_begins_with = "/blobServices/default/containers/test/blobs/in"
}
storage_queue_endpoint = {
storage_account_id = test
queue_name = test
}
}
Error message:
: subject_filter {
Blocks of type "subject_filter" are not expected here.
Error: Unsupported block type
on azure.tf line 90, in module "storage_account__event_subscription":
: storage_queue_endpoint {
Blocks of type "storage_queue_endpoint" are not expected here.
How do i parse the optional fields properly in terraform modules ?
In you module:
resource "azurerm_eventgrid_event_subscription" "events" {
name = var.name
scope = var.scope
subject_filter = {
subject_begins_with = var.subject_begins_with
}
storage_queue_endpoint = var.storage_queue_endpoint
}
Formatting is off here so make sure to run terraform fmt to account for my poor formatting. Also add the variable to the variables.tf file.
Your Terraform file:
module "storage_account__event_subscription" {
source = "../modules/event"
name = "testevent"
scope = test
subject_begins_with = "/blobServices/default/containers/test/blobs/in"
storage_queue_endpoint = {
storage_account_id = test
queue_name = test
}
}
You create the full structure in the module and then you assign the variables in the terraform file.
Anything that will have the same, or generally the same, value can have a default value set in the variables.tf as well so that you get smaller chunks in the TF file.