├── 01-single-instance ├── bootstrap.sh ├── deploy.tf └── variables.tf ├── 04-multiple-instances ├── bootstrap.sh ├── variables.tf └── deploy.tf ├── 02-single-instance-from-volume ├── bootstrap.sh ├── variables.tf └── deploy.tf ├── 03-single-instance-multiple-volumes ├── bootstrap.sh ├── variables.tf └── deploy.tf ├── 05-multiple-instances-from-volume ├── bootstrap.sh ├── variables.tf └── deploy.tf ├── 06-multiple-instances-multiple-volumes ├── bootstrap.sh ├── variables.tf └── deploy.tf ├── openstack.rc └── README.md /01-single-instance/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Your init script 3 | -------------------------------------------------------------------------------- /04-multiple-instances/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Your init script 3 | -------------------------------------------------------------------------------- /02-single-instance-from-volume/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Your init script 3 | -------------------------------------------------------------------------------- /03-single-instance-multiple-volumes/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Your init script 3 | -------------------------------------------------------------------------------- /05-multiple-instances-from-volume/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Your init script 3 | -------------------------------------------------------------------------------- /06-multiple-instances-multiple-volumes/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Your init script 3 | -------------------------------------------------------------------------------- /openstack.rc: -------------------------------------------------------------------------------- 1 | export OS_ENDPOINT_TYPE=internalURL 2 | export OS_INTERFACE=internalURL 3 | export OS_USERNAME= 4 | export OS_PASSWORD= 5 | export OS_PROJECT_NAME= 6 | export OS_TENANT_NAME= 7 | export OS_AUTH_TYPE=password 8 | export OS_AUTH_URL=http://IP:5000/v3 9 | export OS_NO_CACHE=1 10 | export OS_USER_DOMAIN_NAME=Default 11 | export OS_PROJECT_DOMAIN_NAME=Default 12 | export OS_REGION_NAME=RegionOne 13 | export OS_IDENTITY_API_VERSION=3 14 | export OS_AUTH_VERSION=3 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # terraform-openstack 2 | 3 | OpenStack examples using terraform. 4 | 5 | # Usage 6 | Source your openstack rc file 7 | ```sh 8 | source admin.rc 9 | ``` 10 | then edit `variables.tf` with your favorite editor and follow these steps 11 | 12 | ```sh 13 | terraform init 14 | terraform plan 15 | terraform apply 16 | ``` 17 | to check the output: 18 | ```sh 19 | terraform output 20 | ``` 21 | ### Development 22 | Contribute to this project to add more examples and save the time for future, feel 23 | free to open PR 24 | -------------------------------------------------------------------------------- /01-single-instance/deploy.tf: -------------------------------------------------------------------------------- 1 | resource "openstack_compute_instance_v2" "Instance" { 2 | name = var.instance_name 3 | flavor_id = var.flavor_id 4 | image_id = var.image_id 5 | key_pair = var.keypair_name 6 | security_groups = var.security_groups 7 | network { 8 | name = var.network_name 9 | } 10 | user_data = file("bootstrap.sh") 11 | } 12 | 13 | resource "openstack_networking_floatingip_v2" "fip" { 14 | pool = var.floating_ip_pool 15 | } 16 | 17 | resource "openstack_compute_floatingip_associate_v2" "fip" { 18 | floating_ip = openstack_networking_floatingip_v2.fip.address 19 | instance_id = openstack_compute_instance_v2.Instance.id 20 | } 21 | 22 | output "instance_ip" { 23 | value = openstack_compute_instance_v2.Instance.access_ip_v4 24 | } 25 | 26 | output "float_ip" { 27 | value = openstack_networking_floatingip_v2.fip.address 28 | } 29 | -------------------------------------------------------------------------------- /04-multiple-instances/variables.tf: -------------------------------------------------------------------------------- 1 | variable "keypair_name" { 2 | description = "The keypair to be used." 3 | default = "" 4 | } 5 | 6 | variable "network_name" { 7 | description = "The network to be used." 8 | default = "" 9 | } 10 | 11 | variable "instance_name" { 12 | description = "The Instance Name to be used." 13 | default = "" 14 | } 15 | 16 | variable "image_id" { 17 | description = "The image ID to be used." 18 | default = "" 19 | } 20 | 21 | variable "flavor_id" { 22 | description = "The flavor id to be used." 23 | default = "" 24 | } 25 | 26 | variable "instance_num" { 27 | description = "The Number of instances to be created." 28 | default = 29 | } 30 | 31 | variable "floating_ip_pool" { 32 | description = "The pool to be used to get floating ip" 33 | default = "" 34 | } 35 | 36 | variable "security_groups" { 37 | description = "List of security group" 38 | type = list 39 | default = ["default"] 40 | } 41 | -------------------------------------------------------------------------------- /01-single-instance/variables.tf: -------------------------------------------------------------------------------- 1 | variable "keypair_name" { 2 | description = "The keypair to be used." 3 | default = "" 4 | } 5 | 6 | variable "network_name" { 7 | description = "The network to be used." 8 | default = "" 9 | } 10 | 11 | variable "instance_name" { 12 | description = "The Instance Name to be used." 13 | default = "" 14 | } 15 | 16 | variable "image_id" { 17 | description = "The image ID to be used." 18 | default = "" 19 | } 20 | 21 | variable "flavor_id" { 22 | description = "The flavor id to be used." 23 | default = "" 24 | } 25 | 26 | variable "floating_ip_pool" { 27 | description = "The pool to be used to get floating ip" 28 | default = "" 29 | } 30 | 31 | variable "volume_size" { 32 | description = "The size of volume used to instantiate the instance" 33 | default = "" 34 | } 35 | 36 | variable "security_groups" { 37 | description = "List of security group" 38 | type = list 39 | default = ["default"] 40 | } 41 | -------------------------------------------------------------------------------- /02-single-instance-from-volume/variables.tf: -------------------------------------------------------------------------------- 1 | variable "keypair_name" { 2 | description = "The keypair to be used." 3 | default = "" 4 | } 5 | 6 | variable "network_name" { 7 | description = "The network to be used." 8 | default = "" 9 | } 10 | 11 | variable "instance_name" { 12 | description = "The Instance Name to be used." 13 | default = "" 14 | } 15 | 16 | variable "image_id" { 17 | description = "The image ID to be used." 18 | default = "" 19 | } 20 | 21 | variable "flavor_id" { 22 | description = "The flavor id to be used." 23 | default = "" 24 | } 25 | 26 | variable "floating_ip_pool" { 27 | description = "The pool to be used to get floating ip" 28 | default = "" 29 | } 30 | 31 | variable "volume_size" { 32 | description = "The size of volume used to instantiate the instance" 33 | default = 34 | } 35 | 36 | variable "security_groups" { 37 | description = "List of security group" 38 | type = list 39 | default = ["default"] 40 | } 41 | -------------------------------------------------------------------------------- /02-single-instance-from-volume/deploy.tf: -------------------------------------------------------------------------------- 1 | resource "openstack_compute_instance_v2" "Instance" { 2 | name = var.instance_name 3 | flavor_id = var.flavor_id 4 | key_pair = var.keypair_name 5 | security_groups = var.security_groups 6 | network { 7 | name = var.network_name 8 | } 9 | block_device { 10 | uuid = var.image_id 11 | source_type = "image" 12 | volume_size = var.volume_size 13 | boot_index = 0 14 | destination_type = "volume" 15 | delete_on_termination = false 16 | } 17 | user_data = file("bootstrap.sh") 18 | } 19 | 20 | resource "openstack_networking_floatingip_v2" "fip" { 21 | pool = var.floating_ip_pool 22 | } 23 | 24 | resource "openstack_compute_floatingip_associate_v2" "fip" { 25 | floating_ip = openstack_networking_floatingip_v2.fip.address 26 | instance_id = openstack_compute_instance_v2.Instance.id 27 | } 28 | 29 | output "instance_ip" { 30 | value = openstack_compute_instance_v2.Instance.access_ip_v4 31 | } 32 | 33 | output "float_ip" { 34 | value = openstack_networking_floatingip_v2.fip.address 35 | } 36 | -------------------------------------------------------------------------------- /05-multiple-instances-from-volume/variables.tf: -------------------------------------------------------------------------------- 1 | variable "keypair_name" { 2 | description = "The keypair to be used." 3 | default = "" 4 | } 5 | 6 | variable "network_name" { 7 | description = "The network to be used." 8 | default = "" 9 | } 10 | 11 | variable "instance_name" { 12 | description = "The Instance Name to be used." 13 | default = "" 14 | } 15 | 16 | variable "image_id" { 17 | description = "The image ID to be used." 18 | default = "" 19 | } 20 | 21 | variable "flavor_id" { 22 | description = "The flavor id to be used." 23 | default = "" 24 | } 25 | 26 | variable "instance_num" { 27 | description = "The Number of instances to be created." 28 | default = 29 | } 30 | 31 | variable "floating_ip_pool" { 32 | description = "The pool to be used to get floating ip" 33 | default = "" 34 | } 35 | 36 | variable "volume_size" { 37 | description = "The size of volume used to instantiate the instance" 38 | default = 39 | } 40 | 41 | variable "security_groups" { 42 | description = "List of security group" 43 | type = list 44 | default = ["default"] 45 | } 46 | -------------------------------------------------------------------------------- /04-multiple-instances/deploy.tf: -------------------------------------------------------------------------------- 1 | resource "openstack_compute_instance_v2" "Instance" { 2 | count = var.instance_num 3 | name = format("%s-%02d", var.instance_name, count.index+1) 4 | image_id = var.image_id 5 | flavor_id = var.flavor_id 6 | key_pair = var.keypair_name 7 | security_groups = var.security_groups 8 | network { 9 | name = var.network_name 10 | } 11 | user_data = file("bootstrap.sh") 12 | } 13 | 14 | resource "openstack_networking_floatingip_v2" "fip" { 15 | count = var.instance_num 16 | pool = var.floating_ip_pool 17 | } 18 | 19 | resource "openstack_compute_floatingip_associate_v2" "fip" { 20 | count = var.instance_num 21 | floating_ip = element(openstack_networking_floatingip_v2.fip.*.address, count.index) 22 | instance_id = element(openstack_compute_instance_v2.Instance.*.id, count.index) 23 | } 24 | 25 | output "instance_ips" { 26 | value = { 27 | for instance in openstack_compute_instance_v2.Instance: 28 | instance.name => instance.access_ip_v4 29 | } 30 | } 31 | 32 | output "float_ips" { 33 | value = { 34 | for fip in openstack_networking_floatingip_v2.fip: 35 | fip.fixed_ip => fip.address 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /05-multiple-instances-from-volume/deploy.tf: -------------------------------------------------------------------------------- 1 | resource "openstack_compute_instance_v2" "Instance" { 2 | count = var.instance_num 3 | name = format("%s-%02d", var.instance_name, count.index+1) 4 | flavor_id = var.flavor_id 5 | key_pair = var.keypair_name 6 | security_groups = var.security_groups 7 | network { 8 | name = var.network_name 9 | } 10 | block_device { 11 | uuid = var.image_id 12 | source_type = "image" 13 | volume_size = var.volume_size 14 | boot_index = 0 15 | destination_type = "volume" 16 | delete_on_termination = false 17 | } 18 | user_data = file("bootstrap.sh") 19 | } 20 | 21 | resource "openstack_networking_floatingip_v2" "fip" { 22 | count = var.instance_num 23 | pool = var.floating_ip_pool 24 | } 25 | 26 | resource "openstack_compute_floatingip_associate_v2" "fip" { 27 | count = var.instance_num 28 | floating_ip = element(openstack_networking_floatingip_v2.fip.*.address, count.index) 29 | instance_id = element(openstack_compute_instance_v2.Instance.*.id, count.index) 30 | } 31 | 32 | output "instance_ips" { 33 | value = { 34 | for instance in openstack_compute_instance_v2.Instance: 35 | instance.name => instance.access_ip_v4 36 | } 37 | } 38 | 39 | output "float_ips" { 40 | value = { 41 | for fip in openstack_networking_floatingip_v2.fip: 42 | fip.fixed_ip => fip.address 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /03-single-instance-multiple-volumes/variables.tf: -------------------------------------------------------------------------------- 1 | variable "keypair_name" { 2 | description = "The keypair to be used." 3 | default = "" 4 | } 5 | 6 | variable "network_name" { 7 | description = "The network to be used." 8 | default = "" 9 | } 10 | 11 | variable "instance_name" { 12 | description = "The Instance Name to be used." 13 | default = "" 14 | } 15 | 16 | variable "image_id" { 17 | description = "The image ID to be used." 18 | default = "" 19 | } 20 | 21 | variable "flavor_id" { 22 | description = "The flavor id to be used." 23 | default = "" 24 | } 25 | 26 | variable "floating_ip_pool" { 27 | description = "The pool to be used to get floating ip" 28 | default = "" 29 | } 30 | 31 | variable "volume_size" { 32 | description = "The size of volume used to instantiate the instance" 33 | default = 34 | } 35 | 36 | variable "security_groups" { 37 | description = "List of security group" 38 | type = list 39 | default = ["default"] 40 | } 41 | 42 | variable "volume_list" { 43 | description = "List of Volumes to be created" 44 | default = [ 45 | { 46 | boot_index = 1, 47 | size = 20 48 | }, 49 | { 50 | boot_index = 2, 51 | size = 100 52 | }, 53 | { 54 | boot_index = 3, 55 | size = 100 56 | }, 57 | { 58 | boot_index = 4, 59 | size = 100 60 | }, 61 | { 62 | boot_index = 5, 63 | size = 100 64 | }, 65 | ] 66 | } 67 | -------------------------------------------------------------------------------- /03-single-instance-multiple-volumes/deploy.tf: -------------------------------------------------------------------------------- 1 | resource "openstack_compute_instance_v2" "Instance" { 2 | name = var.instance_name 3 | flavor_id = var.flavor_id 4 | key_pair = var.keypair_name 5 | security_groups = var.security_groups 6 | network { 7 | name = var.network_name 8 | } 9 | block_device { 10 | uuid = var.image_id 11 | source_type = "image" 12 | volume_size = var.volume_size 13 | boot_index = 0 14 | destination_type = "volume" 15 | delete_on_termination = false 16 | } 17 | dynamic block_device { 18 | for_each = [for volume in var.volume_list: { 19 | boot_index = volume.boot_index 20 | size = volume.size 21 | }] 22 | content { 23 | source_type = "blank" 24 | volume_size = block_device.value.size 25 | boot_index = block_device.value.boot_index 26 | destination_type = "volume" 27 | delete_on_termination = false 28 | } 29 | } 30 | user_data = file("bootstrap.sh") 31 | } 32 | 33 | resource "openstack_networking_floatingip_v2" "fip" { 34 | pool = var.floating_ip_pool 35 | } 36 | 37 | resource "openstack_compute_floatingip_associate_v2" "fip" { 38 | floating_ip = openstack_networking_floatingip_v2.fip.address 39 | instance_id = openstack_compute_instance_v2.Instance.id 40 | } 41 | 42 | output "instance_ip" { 43 | value = openstack_compute_instance_v2.Instance.access_ip_v4 44 | } 45 | 46 | output "float_ip" { 47 | value = openstack_networking_floatingip_v2.fip.address 48 | } 49 | -------------------------------------------------------------------------------- /06-multiple-instances-multiple-volumes/variables.tf: -------------------------------------------------------------------------------- 1 | variable "keypair_name" { 2 | description = "The keypair to be used." 3 | default = "" 4 | } 5 | 6 | variable "network_name" { 7 | description = "The network to be used." 8 | default = "" 9 | } 10 | 11 | variable "instance_name" { 12 | description = "The Instance Name to be used." 13 | default = "" 14 | } 15 | 16 | variable "image_id" { 17 | description = "The image ID to be used." 18 | default = "" 19 | } 20 | 21 | variable "flavor_id" { 22 | description = "The flavor id to be used." 23 | default = "" 24 | } 25 | 26 | variable "instance_num" { 27 | description = "The Number of instances to be created." 28 | default = 29 | } 30 | 31 | variable "floating_ip_pool" { 32 | description = "The pool to be used to get floating ip" 33 | default = "" 34 | } 35 | 36 | variable "volume_size" { 37 | description = "The size of volume used to instantiate the instance" 38 | default = 39 | } 40 | 41 | variable "security_groups" { 42 | description = "List of security group" 43 | type = list 44 | default = ["default"] 45 | } 46 | 47 | variable "volume_list" { 48 | description = "List of Volumes to be created" 49 | default = [ 50 | { 51 | boot_index = 1, 52 | size = 20 53 | }, 54 | { 55 | boot_index = 2, 56 | size = 100 57 | }, 58 | { 59 | boot_index = 3, 60 | size = 100 61 | }, 62 | { 63 | boot_index = 4, 64 | size = 100 65 | }, 66 | { 67 | boot_index = 5, 68 | size = 100 69 | }, 70 | ] 71 | } 72 | -------------------------------------------------------------------------------- /06-multiple-instances-multiple-volumes/deploy.tf: -------------------------------------------------------------------------------- 1 | resource "openstack_compute_instance_v2" "Instance" { 2 | count = var.instance_num 3 | name = format("%s-%02d", var.instance_name, count.index+1) 4 | flavor_id = var.flavor_id 5 | key_pair = var.keypair_name 6 | security_groups = var.security_groups 7 | network { 8 | name = var.network_name 9 | } 10 | block_device { 11 | uuid = var.image_id 12 | source_type = "image" 13 | volume_size = var.volume_size 14 | boot_index = 0 15 | destination_type = "volume" 16 | delete_on_termination = false 17 | } 18 | dynamic block_device { 19 | for_each = [for volume in var.volume_list: { 20 | boot_index = volume.boot_index 21 | size = volume.size 22 | }] 23 | content { 24 | source_type = "blank" 25 | volume_size = block_device.value.size 26 | boot_index = block_device.value.boot_index 27 | destination_type = "volume" 28 | delete_on_termination = false 29 | } 30 | } 31 | user_data = file("bootstrap.sh") 32 | } 33 | 34 | resource "openstack_networking_floatingip_v2" "fip" { 35 | count = var.instance_num 36 | pool = var.floating_ip_pool 37 | } 38 | 39 | resource "openstack_compute_floatingip_associate_v2" "fip" { 40 | count = var.instance_num 41 | floating_ip = element(openstack_networking_floatingip_v2.fip.*.address, count.index) 42 | instance_id = element(openstack_compute_instance_v2.Instance.*.id, count.index) 43 | } 44 | 45 | output "instance_ips" { 46 | value = { 47 | for instance in openstack_compute_instance_v2.Instance: 48 | instance.name => instance.access_ip_v4 49 | } 50 | } 51 | 52 | output "float_ips" { 53 | value = { 54 | for fip in openstack_networking_floatingip_v2.fip: 55 | fip.fixed_ip => fip.address 56 | } 57 | } 58 | --------------------------------------------------------------------------------