Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.

Commit 98c4abb

Browse files
author
Christophe Useinovic
committed
Demo Terraform
1 parent 16605d6 commit 98c4abb

File tree

9 files changed

+150
-1
lines changed

9 files changed

+150
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# terraform-openstack
2-
Demo of terraform with OpenStack provider
2+
deploy with terraform a instance in Openstack

instance.tf

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
resource "openstack_compute_instance_v2" "moninstance" {
2+
name = "demo"
3+
image_name = "${var.image}"
4+
flavor_name = "${var.flavor_http}"
5+
key_pair = "${openstack_compute_keypair_v2.user_key.name}"
6+
user_data = "${file("scripts/webserver.sh")}"
7+
network {
8+
port = openstack_networking_port_v2.moninstance.id
9+
}
10+
}
11+
12+
# Create network port
13+
resource "openstack_networking_port_v2" "moninstance" {
14+
name = "moninstance-port"
15+
network_id = openstack_networking_network_v2.generic.id
16+
admin_state_up = true
17+
security_group_ids = [
18+
openstack_compute_secgroup_v2.ssh.id,
19+
openstack_compute_secgroup_v2.http.id,
20+
]
21+
fixed_ip {
22+
subnet_id = openstack_networking_subnet_v2.subnet.id
23+
}
24+
}
25+
26+
# Create floating ip
27+
resource "openstack_networking_floatingip_v2" "fip_moninstance" {
28+
pool = "${var.external_network_name}"
29+
}
30+
31+
# Attach floating ip to instance
32+
resource "openstack_compute_floatingip_associate_v2" "moninstance" {
33+
floating_ip = openstack_networking_floatingip_v2.fip_moninstance.address
34+
instance_id = openstack_compute_instance_v2.moninstance.id
35+
}

network.tf

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Router creation
2+
resource "openstack_networking_router_v2" "generic" {
3+
name = "router"
4+
external_network_id = "${var.external_network_id}"
5+
}
6+
7+
# Network creation
8+
resource "openstack_networking_network_v2" "generic" {
9+
name = "network-private"
10+
}
11+
12+
#### HTTP SUBNET ####
13+
14+
# Subnet http configuration
15+
resource "openstack_networking_subnet_v2" "subnet" {
16+
name = var.network_cidr["subnet_name"]
17+
network_id = openstack_networking_network_v2.generic.id
18+
cidr = "${var.network_cidr["cidr"]}"
19+
}
20+
21+
# Router interface configuration
22+
resource "openstack_networking_router_interface_v2" "demo" {
23+
router_id = openstack_networking_router_v2.generic.id
24+
subnet_id = openstack_networking_subnet_v2.subnet.id
25+
}

output.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "address" {
2+
value = "${openstack_networking_floatingip_v2.fip_moninstance.address}"
3+
}

provider.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
provider "openstack" {
2+
user_name = "${var.user_name}"
3+
tenant_name = "${var.tenant_name}"
4+
password = "${var.password}"
5+
auth_url = "${var.auth_url}"
6+
region = "${var.region}"
7+
}

scripts/webserver.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
apt update
3+
apt install -y apache2
4+
echo "<h1>mon nom est: $(hostname)</h1>" | sudo tee /var/www/html/index.html 1>2

sg.tf

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "openstack_compute_secgroup_v2" "http" {
2+
name = "http"
3+
description = "http port open IN/OUT"
4+
rule {
5+
from_port = 80
6+
to_port = 80
7+
ip_protocol = "tcp"
8+
cidr = "0.0.0.0/0"
9+
}
10+
}
11+
12+
resource "openstack_compute_secgroup_v2" "ssh" {
13+
name = "ssh"
14+
description = "ssh port open IN/OUT"
15+
rule {
16+
from_port = 22
17+
to_port = 22
18+
ip_protocol = "tcp"
19+
cidr = "0.0.0.0/0"
20+
}
21+
}

ssh-key.tf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
resource "openstack_compute_keypair_v2" "user_key" {
3+
name = "chriskey"
4+
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDYym2YIbA+VcSZE0hw0+/uwmLUd5gXI+DdOMsLkMViZR7Ov0N9Ra5X/2jHgXOyfG5zyMn4WgFoU1294w9E285kkW598j0JXQhjjqC9XwALKTWKL6lfpE64MLPPQXAEY0xAbFcifISc8zCjp7dgh7vqm5S1FYR4viw5j083TpLVn2PzZvnZUDg6Ozjt2sJNtESQieNre4MwhRCuO5hj3cNZOREIaVxHwzzTwOcBXXPLdfavLkJ3sBkE48TPcuNkENCOeN4ukPLdhwIzae14QSMP15iA93VVEQCSYA1Mh343+RPhu/98kWqBBZxpoz+0NuVibuX+IUw3bscQazivLztL christophe@Christophe"
5+
}
6+

variables.tf

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Provider variables
2+
# sourced from environment variable
3+
variable "user_name" {}
4+
variable "tenant_name" {}
5+
variable "password" {}
6+
variable "auth_url" {}
7+
variable "region" {}
8+
9+
# Image Configuration
10+
variable "image" {
11+
type = string
12+
default = "Ubuntu1804"
13+
}
14+
15+
# Network Configuration
16+
variable "external_network_id" {
17+
type = string
18+
default = "acc4ebf7-aa8d-4527-bade-7539624426c5"
19+
}
20+
21+
variable "external_network_name" {
22+
type = string
23+
default = "public"
24+
}
25+
26+
variable "public-subnet" {
27+
type = string
28+
default = "1e83732d-298b-4dc2-bc51-939076a7b1f9"
29+
}
30+
31+
variable "dns_ip" {
32+
type = list(string)
33+
default = ["8.8.8.8", "8.8.8.4"]
34+
}
35+
36+
variable "network_cidr" {
37+
type = map(string)
38+
default = {
39+
subnet_name = "subnet-private"
40+
cidr = "192.168.100.0/24"
41+
}
42+
}
43+
44+
# Flavor instance
45+
variable "flavor_http" {
46+
type = string
47+
default = "m1.high"
48+
}

0 commit comments

Comments
 (0)