Skip to content

Commit

Permalink
use for_each function instead of count in the fourth example
Browse files Browse the repository at this point in the history
  • Loading branch information
diodonfrost committed Dec 25, 2019
1 parent 199e37d commit 8afe060
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 32 deletions.
17 changes: 10 additions & 7 deletions 04-instance-with-loadbalancer/00-variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ variable "network_http" {
}
}

variable "desired_capacity_http" {
type = number
default = 2
variable "http_instance_names" {
type = set(string)
default = ["http-instance-1",
"http-instance-2",
"http-instance-3"]
}

#### VM DB parameters ####
Expand All @@ -56,8 +58,9 @@ variable "network_db" {
}
}

variable "desired_capacity_db" {
type = number
default = 3
variable "db_instance_names" {
type = set(string)
default = ["db-instance-1",
"db-instance-2",
"db-instance-3"]
}

10 changes: 5 additions & 5 deletions 04-instance-with-loadbalancer/060-instance_http.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Create instance
#
resource "openstack_compute_instance_v2" "http" {
count = var.desired_capacity_http
name = "http-${count.index}"
for_each = var.http_instance_names
name = each.key
image_name = var.image
flavor_name = var.flavor_http
key_pair = openstack_compute_keypair_v2.user_key.name
user_data = file("scripts/first-boot.sh")
network {
port = element(openstack_networking_port_v2.http.*.id, count.index)
port = openstack_networking_port_v2.http[each.key].id
}
}

# Create network port
resource "openstack_networking_port_v2" "http" {
count = var.desired_capacity_http
name = "port-http-${count.index}"
for_each = var.http_instance_names
name = "port-http-${each.key}"
network_id = openstack_networking_network_v2.generic.id
admin_state_up = true
security_group_ids = [
Expand Down
10 changes: 5 additions & 5 deletions 04-instance-with-loadbalancer/061-instance_db.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Create instance
#
resource "openstack_compute_instance_v2" "db" {
count = var.desired_capacity_db
name = "db-${count.index}"
for_each = var.db_instance_names
name = each.key
image_name = var.image
flavor_name = var.flavor_db
key_pair = openstack_compute_keypair_v2.user_key.name
user_data = file("scripts/first-boot.sh")
network {
port = element(openstack_networking_port_v2.db.*.id, count.index)
port = openstack_networking_port_v2.db[each.key].id
}
}

# Create network port
resource "openstack_networking_port_v2" "db" {
count = var.desired_capacity_db
name = "port-db-${count.index}"
for_each = var.db_instance_names
name = "port-db-${each.key}"
network_id = openstack_networking_network_v2.generic.id
admin_state_up = true
security_group_ids = [
Expand Down
11 changes: 4 additions & 7 deletions 04-instance-with-loadbalancer/070-loadbalancer.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ resource "openstack_lb_pool_v2" "http" {

# Add multip instances to pool
resource "openstack_lb_member_v2" "http" {
count = var.desired_capacity_http
address = element(
openstack_compute_instance_v2.http.*.access_ip_v4,
count.index,
)
for_each = var.http_instance_names
address = openstack_compute_instance_v2.http[each.key].access_ip_v4
protocol_port = 80
pool_id = openstack_lb_pool_v2.http.id
subnet_id = openstack_networking_subnet_v2.http.id
Expand Down Expand Up @@ -78,8 +75,8 @@ resource "openstack_lb_pool_v2" "db" {

# Add multip instances to pool
resource "openstack_lb_member_v2" "db" {
count = var.desired_capacity_db
address = element(openstack_compute_instance_v2.db.*.access_ip_v4, count.index)
for_each = var.db_instance_names
address = openstack_compute_instance_v2.db[each.key].access_ip_v4
protocol_port = 3306
pool_id = openstack_lb_pool_v2.db.id
subnet_id = openstack_networking_subnet_v2.db.id
Expand Down
8 changes: 0 additions & 8 deletions 04-instance-with-loadbalancer/provider.tf

This file was deleted.

0 comments on commit 8afe060

Please sign in to comment.