Created
July 15, 2020 18:20
-
-
Save gmmephisto/4c3e245d3b97a34c9946b3ebb695ad45 to your computer and use it in GitHub Desktop.
compute.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "ext_network_name" { | |
default = "ext-net" | |
} | |
variable "instances_count" { | |
default = 1 | |
} | |
resource "random_pet" test { | |
prefix = "test" | |
} | |
data "openstack_networking_network_v2" "internet" { | |
name = var.ext_network_name | |
external = true | |
} | |
resource "openstack_networking_network_v2" "private" { | |
count = var.instances_count | |
name = format("%s-%02d", random_pet.test.id, count.index + 1) | |
admin_state_up = "true" | |
} | |
resource "openstack_networking_subnet_v2" "private" { | |
count = var.instances_count | |
name = format("%s-%02d", random_pet.test.id, count.index + 1) | |
network_id = openstack_networking_network_v2.private[count.index].id | |
cidr = cidrsubnet("10.10.0.0/16", 8, count.index) | |
ip_version = 4 | |
} | |
resource "openstack_networking_secgroup_v2" "mgmt" { | |
name = random_pet.test.id | |
description = random_pet.test.id | |
} | |
resource "openstack_networking_secgroup_rule_v2" "icmp" { | |
direction = "ingress" | |
ethertype = "IPv4" | |
protocol = "icmp" | |
remote_ip_prefix = "0.0.0.0/0" | |
security_group_id = openstack_networking_secgroup_v2.mgmt.id | |
} | |
resource "openstack_networking_secgroup_rule_v2" "ssh" { | |
direction = "ingress" | |
ethertype = "IPv4" | |
protocol = "tcp" | |
port_range_min = 22 | |
port_range_max = 22 | |
remote_ip_prefix = "0.0.0.0/0" | |
security_group_id = openstack_networking_secgroup_v2.mgmt.id | |
} | |
resource "openstack_networking_secgroup_rule_v2" "web" { | |
direction = "ingress" | |
ethertype = "IPv4" | |
protocol = "tcp" | |
port_range_min = 80 | |
port_range_max = 80 | |
remote_ip_prefix = "0.0.0.0/0" | |
security_group_id = openstack_networking_secgroup_v2.mgmt.id | |
} | |
data "openstack_compute_keypair_v2" "default" { | |
name = var.keypair_name | |
} | |
resource "openstack_compute_instance_v2" "instance" { | |
count = var.instances_count | |
name = format("%s-%02d", random_pet.test.id, count.index + 1) | |
image_name = var.image_name | |
flavor_name = var.flavor_name | |
key_pair = data.openstack_compute_keypair_v2.default.name | |
availability_zone = var.az | |
security_groups = [ | |
openstack_networking_secgroup_v2.mgmt.name | |
] | |
network { | |
# NOTE(m.ushanov): https://github.com/terraform-providers/terraform-provider-openstack/issues/796 | |
uuid = openstack_networking_subnet_v2.private[count.index].network_id | |
} | |
} | |
resource "openstack_networking_floatingip_v2" "fip" { | |
count = var.instances_count | |
pool = data.openstack_networking_network_v2.internet.name | |
} | |
resource "openstack_networking_router_v2" "router" { | |
name = random_pet.test.id | |
admin_state_up = true | |
external_network_id = data.openstack_networking_network_v2.internet.id | |
} | |
resource "openstack_networking_router_interface_v2" "router_backends_iface" { | |
count = var.instances_count | |
router_id = openstack_networking_router_v2.router.id | |
subnet_id = openstack_networking_subnet_v2.private[count.index].id | |
} | |
resource "openstack_compute_floatingip_associate_v2" "fip" { | |
count = var.instances_count | |
floating_ip = openstack_networking_floatingip_v2.fip[count.index].address | |
instance_id = openstack_compute_instance_v2.instance[count.index].id | |
} | |
resource "null_resource" "test_instances_connection" { | |
count = var.instances_count | |
connection { | |
type = "ssh" | |
user = var.image_user | |
host = openstack_compute_floatingip_associate_v2.fip[count.index].floating_ip | |
} | |
provisioner "remote-exec" { | |
inline = ["ping -c 4 -W 15 8.8.8.8"] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment