diff --git a/terraform/README.md b/terraform/README.md new file mode 100644 index 0000000..d670649 --- /dev/null +++ b/terraform/README.md @@ -0,0 +1,26 @@ +# Using Terraform with OpenStack + +These files were created to allow users to use an example Terraform ([http://terraform.io](http://terraform.io)) configuration with OpenStack. These files require a working Terraform installation and a working OpenStack environment. + +## Contents + +* **README.md**: This file you're currently reading. + +* **tf-example**: This directory contains an example Terraform configuration written in Terraform format. + +* **tf-json-example**: This directory contains an example Terraform configuration written in JSON format. + +## Instructions + +These instructions assume you've already installed Terraform. These instructions also assume that you have a working OpenStack environment against which to run the Terraform configurations. + +1. Edit `provider.tf` or `provider.tf.json` with the correct username, tenant, password, and authentication URL for your OpenStack environment. +2. Edit `vars.tf` or `vars.tf.json` to provide the correct values for image name, flavor, external network (for the logical router), SSH key pair, and floating IP pool. +3. From either the `tf-example` or `tf-json-example` directory, run `terraform plan` to see what changes will be made. +4. If you are happy with the output of #3, run `terraform apply`. + +Enjoy! + +## License + +This material is licensed under the MIT License. diff --git a/terraform/tf-example/main.tf b/terraform/tf-example/main.tf new file mode 100644 index 0000000..089f6df --- /dev/null +++ b/terraform/tf-example/main.tf @@ -0,0 +1,40 @@ +resource "openstack_networking_network_v2" "tf-net" { + name = "tf-net" + admin_state_up = "true" +} + +resource "openstack_networking_subnet_v2" "tf-subnet" { + name = "tf-subnet" + network_id = "${openstack_networking_network_v2.tf-net.id}" + cidr = "192.168.200.0/24" + ip_version = 4 + dns_nameservers = ["8.8.8.8","8.8.4.4"] +} + +resource "openstack_networking_router_v2" "tf-router" { + name = "tf-router" + admin_state_up = "true" + external_gateway = "${var.external_gateway}" +} + +resource "openstack_networking_router_interface_v2" "tf-router-interface" { + router_id = "${openstack_networking_router_v2.tf-router.id}" + subnet_id = "${openstack_networking_subnet_v2.tf-subnet.id}" +} + +resource "openstack_compute_floatingip_v2" "tf-fip" { + pool = "${var.pool}" + depends_on = ["openstack_networking_router_interface_v2.tf-router-interface"] +} + +resource "openstack_compute_instance_v2" "tf-instance" { + name = "tf-instance" + image_name = "${var.image}" + flavor_name = "${var.flavor}" + key_pair = "${var.key_pair}" + security_groups = ["default"] + floating_ip = "${openstack_compute_floatingip_v2.tf-fip.address}" + network { + uuid = "${openstack_networking_network_v2.tf-net.id}" + } +} diff --git a/terraform/tf-example/output.tf b/terraform/tf-example/output.tf new file mode 100644 index 0000000..f04b3e1 --- /dev/null +++ b/terraform/tf-example/output.tf @@ -0,0 +1,3 @@ +output "address" { + value = "${openstack_compute_floatingip_v2.tf-fip.address}" +} diff --git a/terraform/tf-example/provider.tf b/terraform/tf-example/provider.tf new file mode 100644 index 0000000..efd8ce0 --- /dev/null +++ b/terraform/tf-example/provider.tf @@ -0,0 +1,6 @@ +provider "openstack" { + user_name = "demo" + tenant_name = "demo" + password = "password" + auth_url = "http://openstack.domain.net:5000/v2.0" +} diff --git a/terraform/tf-example/vars.tf b/terraform/tf-example/vars.tf new file mode 100644 index 0000000..821b1b2 --- /dev/null +++ b/terraform/tf-example/vars.tf @@ -0,0 +1,19 @@ +variable "image" { + default = "" +} + +variable "flavor" { + default = "m1.small" +} + +variable "external_gateway" { + default = "" +} + +variable "key_pair" { + default = "" +} + +variable "pool" { + default = "" +} diff --git a/terraform/tf-json-example/main.tf.json b/terraform/tf-json-example/main.tf.json new file mode 100644 index 0000000..bef9631 --- /dev/null +++ b/terraform/tf-json-example/main.tf.json @@ -0,0 +1,71 @@ +{ + "resource": { + "openstack_networking_network_v2": { + "tf-net": { + "name": "tf-net", + "admin_state_up": "true" + } + } + }, + + "resource": { + "openstack_networking_subnet_v2": { + "tf-subnet": { + "name": "tf-subnet", + "network_id": "${openstack_networking_network_v2.tf-net.id}", + "cidr": "192.168.200.0/24", + "ip_version": 4, + "dns_nameservers": [ + "8.8.8.8", + "8.8.4.4" + ] + } + } + }, + + "resource": { + "openstack_networking_router_v2": { + "tf-router": { + "name": "tf-router", + "admin_state_up": "true", + "external_gateway": "${var.external_gateway}" + } + } + }, + + "resource": { + "openstack_networking_router_interface_v2": { + "tf-router-iface": { + "router_id": "${openstack_networking_router_v2.tf-router.id}", + "subnet_id": "${openstack_networking_subnet_v2.tf-subnet.id}" + } + } + }, + + "resource": { + "openstack_compute_floatingip_v2": { + "tf-fip": { + "pool": "${var.pool}", + "depends_on": [ + "openstack_networking_router_interface_v2.tf-router-iface" + ] + } + } + }, + + "resource": { + "openstack_compute_instance_v2": { + "tf-instance": { + "name": "tf-instance", + "image_name": "${var.image}", + "flavor_name": "${var.flavor}", + "key_pair": "${var.key_pair}", + "floating_ip": "${openstack_compute_floatingip_v2.tf-fip.address}", + "security_groups": ["default"], + "network": { + "uuid": "${openstack_networking_network_v2.tf-net.id}" + } + } + } + } +} \ No newline at end of file diff --git a/terraform/tf-json-example/output.tf.json b/terraform/tf-json-example/output.tf.json new file mode 100644 index 0000000..b60399c --- /dev/null +++ b/terraform/tf-json-example/output.tf.json @@ -0,0 +1,7 @@ +{ + "output": { + "address": { + "value": "${openstack_compute_floatingip_v2.tf-fip.address}" + } + } +} \ No newline at end of file diff --git a/terraform/tf-json-example/provider.tf.json b/terraform/tf-json-example/provider.tf.json new file mode 100644 index 0000000..bce1d49 --- /dev/null +++ b/terraform/tf-json-example/provider.tf.json @@ -0,0 +1,10 @@ +{ + "provider": { + "openstack": { + "user_name": "demo", + "tenant_name": "demo", + "password": "password", + "auth_url": "http://openstack.domain.net:5000/v2.0" + } + } +} diff --git a/terraform/tf-json-example/vars.tf.json b/terraform/tf-json-example/vars.tf.json new file mode 100644 index 0000000..4c645b3 --- /dev/null +++ b/terraform/tf-json-example/vars.tf.json @@ -0,0 +1,31 @@ +{ + "variable": { + "image": { + "default": "INSERT YOUR VALUE HERE" + } + }, + + "variable": { + "flavor": { + "default": "m1.small" + } + }, + + "variable": { + "external_gateway": { + "default": "INSERT YOUR VALUE HERE" + } + }, + + "variable": { + "key_pair": { + "default": "INSERT YOUR VALUE HERE" + } + }, + + "variable": { + "pool": { + "default": "INSERT YOUR VALUE HERE" + } + } +}