main.tf 2.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
terraform {
  required_providers {
    openstack = {
      source  = "terraform-provider-openstack/openstack"
      version = "~> 1.17"
    }
  }
}

Johan Ruuskanen's avatar
Johan Ruuskanen committed
10
provider "openstack" {
11
  cloud = "${var.cloud_name}"
Johan Ruuskanen's avatar
Johan Ruuskanen committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
}

resource "openstack_compute_keypair_v2" "create_keypair" {
  name = "${var.keypair_name}"
  public_key = "${file(var.public_key_path)}"
}

resource "openstack_networking_network_v2" "network" {
  name = "${var.network_name}"
}

resource "openstack_networking_subnet_v2" "subnet" {
  network_id = "${openstack_networking_network_v2.network.id}"
  cidr       = "${var.subnet_cidr}"
  ip_version = 4
}

resource "openstack_networking_router_v2" "router" {
  name = "${var.router_name}"
  external_network_id  = "${var.external_net_id}"
}

resource "openstack_networking_router_interface_v2" "router_interface_1" {
  router_id = "${openstack_networking_router_v2.router.id}"
  subnet_id = "${openstack_networking_subnet_v2.subnet.id}"
}


resource "openstack_networking_secgroup_v2" "secgroup" {
  name        = "${var.instance_name}-secgroup_ssh"
  description = "ssh"
}

resource "openstack_networking_secgroup_rule_v2" "ssh_rule" {
  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.secgroup.id}"
}

resource "openstack_networking_secgroup_rule_v2" "icmp_rule" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "icmp"
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = "${openstack_networking_secgroup_v2.secgroup.id}"
}

Johan Ruuskanen's avatar
Johan Ruuskanen committed
63
64
65
66
67
68
69
70
71
72
resource "openstack_networking_secgroup_rule_v2" "port_rule" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = 5000
  port_range_max    = 5000
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = "${openstack_networking_secgroup_v2.secgroup.id}"
}

Johan Ruuskanen's avatar
Johan Ruuskanen committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
resource "openstack_compute_instance_v2" "instance" {
  name            = "${var.instance_name}"
  image_id        = "${var.image_id}"
  flavor_name     = "${var.flavor_name}"
  key_pair        = "${openstack_compute_keypair_v2.create_keypair.name}"
  security_groups = ["${openstack_networking_secgroup_v2.secgroup.name}"]

  network {
    uuid = "${openstack_networking_subnet_v2.subnet.network_id}"
  }
}

resource "openstack_networking_floatingip_v2" "floating_ip" {
  pool = "internet"
}

resource "openstack_compute_floatingip_associate_v2" "associate_ip" {
  floating_ip = "${openstack_networking_floatingip_v2.floating_ip.address}"
  instance_id = "${openstack_compute_instance_v2.instance.id}"
}

output "ip_address" {
  value = "${openstack_networking_floatingip_v2.floating_ip.address}"
96
}