Skip to content

Commit a110121

Browse files
committed
examples: add OpenStack configuration with networking
0 parents  commit a110121

5 files changed

Lines changed: 174 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Basic OpenStack architecture with networking
2+
3+
This provides a template for running a simple architecture on an OpenStack
4+
cloud.
5+
6+
To simplify the example, this intentionally ignores deploying and
7+
getting your application onto the servers. However, you could do so either via
8+
[provisioners](https://www.terraform.io/docs/provisioners/) and a configuration
9+
management tool, or by pre-baking configured images with
10+
[Packer](http://www.packer.io).
11+
12+
After you run `terraform apply` on this configuration, it will output the
13+
floating IP address assigned to the instance. After your instance started,
14+
this should respond with the default nginx web page.
15+
16+
First set the required environment variables for the OpenStack provider by
17+
sourcing the [credentials file](http://docs.openstack.org/cli-reference/content/cli_openrc.html).
18+
19+
```
20+
source openrc
21+
```
22+
23+
Afterwards run with a command like this:
24+
25+
```
26+
terraform apply \
27+
-var 'external_gateway=c1901f39-f76e-498a-9547-c29ba45f64df' \
28+
-var 'pool=public'
29+
```
30+
31+
To get a list of usable floating IP pools run this command:
32+
33+
```
34+
$ nova floating-ip-pool-list
35+
+--------+
36+
| name |
37+
+--------+
38+
| public |
39+
+--------+
40+
```
41+
42+
To get the UUID of the external gateway run this command:
43+
44+
```
45+
$ neutron net-show FLOATING_IP_POOL
46+
+---------------------------+--------------------------------------+
47+
| Field | Value |
48+
+---------------------------+--------------------------------------+
49+
| admin_state_up | True |
50+
| id | c1901f39-f76e-498a-9547-c29ba45f64df |
51+
| mtu | 0 |
52+
| name | public |
53+
| port_security_enabled | True |
54+
| provider:network_type | vxlan |
55+
| provider:physical_network | |
56+
| provider:segmentation_id | 1092 |
57+
| router:external | True |
58+
| shared | False |
59+
| status | ACTIVE |
60+
| subnets | 42b672ae-8d51-4a18-a028-ddae7859ec4c |
61+
| tenant_id | 1bde0a49d2ff44ffb44e6339a8cefe3a |
62+
+---------------------------+--------------------------------------+
63+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
resource "openstack_compute_keypair_v2" "terraform" {
2+
name = "terraform"
3+
public_key = "${file("${var.ssh_key_file}.pub")}"
4+
}
5+
6+
resource "openstack_networking_network_v2" "terraform" {
7+
name = "terraform"
8+
admin_state_up = "true"
9+
}
10+
11+
resource "openstack_networking_subnet_v2" "terraform" {
12+
name = "terraform"
13+
network_id = "${openstack_networking_network_v2.terraform.id}"
14+
cidr = "10.0.0.0/24"
15+
ip_version = 4
16+
dns_nameservers = ["8.8.8.8","8.8.4.4"]
17+
}
18+
19+
resource "openstack_networking_router_v2" "terraform" {
20+
name = "terraform"
21+
admin_state_up = "true"
22+
external_gateway = "${var.external_gateway}"
23+
}
24+
25+
resource "openstack_networking_router_interface_v2" "terraform" {
26+
router_id = "${openstack_networking_router_v2.terraform.id}"
27+
subnet_id = "${openstack_networking_subnet_v2.terraform.id}"
28+
}
29+
30+
resource "openstack_compute_secgroup_v2" "terraform" {
31+
name = "terraform"
32+
description = "Security group for the Terraform example instances"
33+
rule {
34+
from_port = 22
35+
to_port = 22
36+
ip_protocol = "tcp"
37+
cidr = "0.0.0.0/0"
38+
}
39+
rule {
40+
from_port = 80
41+
to_port = 80
42+
ip_protocol = "tcp"
43+
cidr = "0.0.0.0/0"
44+
}
45+
rule {
46+
from_port = -1
47+
to_port = -1
48+
ip_protocol = "icmp"
49+
cidr = "0.0.0.0/0"
50+
}
51+
}
52+
53+
resource "openstack_compute_floatingip_v2" "terraform" {
54+
pool = "${var.pool}"
55+
depends_on = ["openstack_networking_router_interface_v2.terraform"]
56+
}
57+
58+
resource "openstack_compute_instance_v2" "terraform" {
59+
name = "terraform"
60+
image_name = "${var.image}"
61+
flavor_name = "${var.flavor}"
62+
key_pair = "${openstack_compute_keypair_v2.terraform.name}"
63+
security_groups = [ "${openstack_compute_secgroup_v2.terraform.name}" ]
64+
floating_ip = "${openstack_compute_floatingip_v2.terraform.address}"
65+
network {
66+
uuid = "${openstack_networking_network_v2.terraform.id}"
67+
}
68+
provisioner "remote-exec" {
69+
connection {
70+
user = "${var.ssh_user_name}"
71+
key_file = "${var.ssh_key_file}"
72+
}
73+
inline = [
74+
"sudo apt-get -y update",
75+
"sudo apt-get -y install nginx",
76+
"sudo service nginx start"
77+
]
78+
}
79+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
export OS_AUTH_URL=http://KEYSTONE.ENDPOINT.URL:5000/v2.0
4+
export OS_TENANT_NAME=YOUR_TENANT_NAME
5+
export OS_USERNAME=YOUR_USERNAME
6+
export OS_PASSWORD=YOUR_PASSWORD
7+
export OS_REGION_NAME=YOUR_REGION_NAME
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "address" {
2+
value = "${openstack_compute_floatingip_v2.terraform.address}"
3+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
variable "image" {
2+
default = "Ubuntu 14.04"
3+
}
4+
5+
variable "flavor" {
6+
default = "m1.small"
7+
}
8+
9+
variable "ssh_key_file" {
10+
default = "~/.ssh/id_rsa.terraform"
11+
}
12+
13+
variable "ssh_user_name" {
14+
default = "ubuntu"
15+
}
16+
17+
variable "external_gateway" {
18+
}
19+
20+
variable "pool" {
21+
default = "public"
22+
}

0 commit comments

Comments
 (0)