You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.3 KiB

  1. # Define CloudComp group number
  2. variable "group_number" {
  3. type = string
  4. default = "20"
  5. }
  6. ## OpenStack credentials can be used in a more secure way by using
  7. ## cloud.yaml from https://private-cloud.informatik.hs-fulda.de/project/api_access/clouds.yaml/
  8. # or by using env vars exported from openrc here,
  9. # e.g., using 'export TF_VAR_os_password=$OS_PASSWORD'
  10. # Define OpenStack credentials, project config etc.
  11. locals {
  12. auth_url = "https://private-cloud.informatik.hs-fulda.de:5000/v3"
  13. user_name = "CloudComp${var.group_number}"
  14. user_password = "<password of your group here, private-cloud is only reachable via vpn>"
  15. tenant_name = "CloudComp${var.group_number}"
  16. #network_name = "CloudComp${var.group_number}-net"
  17. router_name = "CloudComp${var.group_number}-router"
  18. image_name = "Ubuntu 20.04 - Focal Fossa - 64-bit - Cloud Based Image"
  19. flavor_name = "m1.small"
  20. region_name = "RegionOne"
  21. }
  22. # Define OpenStack provider
  23. terraform {
  24. required_version = ">= 0.14.0"
  25. required_providers {
  26. openstack = {
  27. source = "terraform-provider-openstack/openstack"
  28. version = ">= 1.47.0"
  29. }
  30. }
  31. }
  32. # Configure the OpenStack Provider
  33. provider "openstack" {
  34. user_name = local.user_name
  35. tenant_name = local.tenant_name
  36. password = local.user_password
  37. auth_url = local.auth_url
  38. region = local.region_name
  39. use_octavia = true
  40. }
  41. ###########################################################################
  42. #
  43. # create keypair
  44. #
  45. ###########################################################################
  46. # import keypair, if public_key is not specified, create new keypair to use
  47. resource "openstack_compute_keypair_v2" "terraform-keypair" {
  48. name = "my-terraform-pubkey"
  49. #public_key = file("~/.ssh/id_rsa.pub")
  50. }
  51. ###########################################################################
  52. #
  53. # create security group
  54. #
  55. ###########################################################################
  56. resource "openstack_networking_secgroup_v2" "terraform-secgroup" {
  57. name = "my-terraform-secgroup"
  58. description = "for terraform instances"
  59. }
  60. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-rule-http" {
  61. direction = "ingress"
  62. ethertype = "IPv4"
  63. protocol = "tcp"
  64. port_range_min = 80
  65. port_range_max = 80
  66. #remote_ip_prefix = "0.0.0.0/0"
  67. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup.id
  68. }
  69. resource "openstack_networking_secgroup_rule_v2" "terraform-secgroup-rule-ssh" {
  70. direction = "ingress"
  71. ethertype = "IPv4"
  72. protocol = "tcp"
  73. port_range_min = 22
  74. port_range_max = 22
  75. #remote_ip_prefix = "0.0.0.0/0"
  76. security_group_id = openstack_networking_secgroup_v2.terraform-secgroup.id
  77. }
  78. ###########################################################################
  79. #
  80. # create network
  81. #
  82. ###########################################################################
  83. resource "openstack_networking_network_v2" "terraform-network-1" {
  84. name = "my-terraform-network-1"
  85. admin_state_up = "true"
  86. }
  87. resource "openstack_networking_subnet_v2" "terraform-subnet-1" {
  88. name = "my-terraform-subnet-1"
  89. network_id = openstack_networking_network_v2.terraform-network-1.id
  90. cidr = "192.168.255.0/24"
  91. ip_version = 4
  92. }
  93. data "openstack_networking_router_v2" "router-1" {
  94. name = local.router_name
  95. }
  96. resource "openstack_networking_router_interface_v2" "router_interface_1" {
  97. router_id = data.openstack_networking_router_v2.router-1.id
  98. subnet_id = openstack_networking_subnet_v2.terraform-subnet-1.id
  99. }
  100. ###########################################################################
  101. #
  102. # create instances
  103. #
  104. ###########################################################################
  105. resource "openstack_compute_instance_v2" "terraform-instance-1" {
  106. name = "my-terraform-instance-1"
  107. image_name = local.image_name
  108. flavor_name = local.flavor_name
  109. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  110. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup.name]
  111. depends_on = [openstack_networking_subnet_v2.terraform-subnet-1]
  112. network {
  113. uuid = openstack_networking_network_v2.terraform-network-1.id
  114. }
  115. user_data = <<-EOF
  116. #!/bin/bash
  117. apt-get update
  118. apt-get -y install apache2
  119. rm /var/www/html/index.html
  120. cat > /var/www/html/index.html << INNEREOF
  121. <!DOCTYPE html>
  122. <html>
  123. <body>
  124. <h1>It works!</h1>
  125. <p>hostname</p>
  126. </body>
  127. </html>
  128. INNEREOF
  129. sed -i "s/hostname/terraform-instance-1/" /var/www/html/index.html
  130. sed -i "1s/$/ terraform-instance-1/" /etc/hosts
  131. EOF
  132. }
  133. resource "openstack_compute_instance_v2" "terraform-instance-2" {
  134. name = "my-terraform-instance-2"
  135. image_name = local.image_name
  136. flavor_name = local.flavor_name
  137. key_pair = openstack_compute_keypair_v2.terraform-keypair.name
  138. security_groups = [openstack_networking_secgroup_v2.terraform-secgroup.id]
  139. depends_on = [openstack_networking_subnet_v2.terraform-subnet-1]
  140. network {
  141. uuid = openstack_networking_network_v2.terraform-network-1.id
  142. }
  143. user_data = <<-EOF
  144. #!/bin/bash
  145. apt-get update
  146. apt-get -y install apache2
  147. rm /var/www/html/index.html
  148. cat > /var/www/html/index.html << INNEREOF
  149. <!DOCTYPE html>
  150. <html>
  151. <body>
  152. <h1>It works!</h1>
  153. <p>hostname</p>
  154. </body>
  155. </html>
  156. INNEREOF
  157. sed -i "s/hostname/terraform-instance-2/" /var/www/html/index.html
  158. sed -i "1s/$/ terraform-instance-2/" /etc/hosts
  159. EOF
  160. }
  161. ###########################################################################
  162. #
  163. # create load balancer
  164. #
  165. ###########################################################################
  166. resource "openstack_lb_loadbalancer_v2" "lb_1" {
  167. vip_subnet_id = openstack_networking_subnet_v2.terraform-subnet-1.id
  168. }
  169. resource "openstack_lb_listener_v2" "listener_1" {
  170. protocol = "HTTP"
  171. protocol_port = 80
  172. loadbalancer_id = openstack_lb_loadbalancer_v2.lb_1.id
  173. connection_limit = 1024
  174. }
  175. resource "openstack_lb_pool_v2" "pool_1" {
  176. protocol = "HTTP"
  177. lb_method = "ROUND_ROBIN"
  178. listener_id = openstack_lb_listener_v2.listener_1.id
  179. }
  180. resource "openstack_lb_members_v2" "members_1" {
  181. pool_id = openstack_lb_pool_v2.pool_1.id
  182. member {
  183. address = openstack_compute_instance_v2.terraform-instance-1.access_ip_v4
  184. protocol_port = 80
  185. }
  186. member {
  187. address = openstack_compute_instance_v2.terraform-instance-2.access_ip_v4
  188. protocol_port = 80
  189. }
  190. }
  191. resource "openstack_lb_monitor_v2" "monitor_1" {
  192. pool_id = openstack_lb_pool_v2.pool_1.id
  193. type = "HTTP"
  194. delay = 5
  195. timeout = 5
  196. max_retries = 3
  197. http_method = "GET"
  198. url_path = "/"
  199. expected_codes = 200
  200. depends_on = [openstack_lb_loadbalancer_v2.lb_1, openstack_lb_listener_v2.listener_1, openstack_lb_pool_v2.pool_1, openstack_lb_members_v2.members_1 ]
  201. }
  202. ###########################################################################
  203. #
  204. # assign floating ip to load balancer
  205. #
  206. ###########################################################################
  207. resource "openstack_networking_floatingip_v2" "fip_1" {
  208. pool = "public1"
  209. port_id = openstack_lb_loadbalancer_v2.lb_1.vip_port_id
  210. }
  211. output "loadbalancer_vip_addr" {
  212. value = openstack_networking_floatingip_v2.fip_1
  213. }