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.

178 lines
5.2 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.46.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. ###########################################################################
  134. #
  135. # assign floating ip to instance
  136. #
  137. ###########################################################################
  138. resource "openstack_networking_floatingip_v2" "fip_1" {
  139. pool = "public1"
  140. }
  141. resource "openstack_compute_floatingip_associate_v2" "fip_1_assoc" {
  142. floating_ip = openstack_networking_floatingip_v2.fip_1.address
  143. instance_id = openstack_compute_instance_v2.terraform-instance-1.id
  144. }
  145. output "vip_addr" {
  146. value = openstack_networking_floatingip_v2.fip_1
  147. }