Commit fdfde7e5 authored by Xiao Gao's avatar Xiao Gao

added sample deployment

parent 0742a917
#
# Number of web instance
#
variable "count" {
default = 2
}
#
# Create a network in our project
# project is defined in provider.tf
#
resource "openstack_networking_network_v2" "tf_network" {
region = "nova"
name = "tf_network"
admin_state_up = "true"
}
#
# Create a subnet in our new network(s)
#
resource "openstack_networking_subnet_v2" "tf_net_sub1" {
region = "nova"
network_id = "${openstack_networking_network_v2.tf_network.id}"
cidr = "3.3.33.0/24"
dns_nameservers="${var.name_server}"
ip_version = 4
}
#
# Create a router for our network(s)
# with NSX-T no need to create exclusive router
#
resource "openstack_networking_router_v2" "tf_router1" {
region = "nova"
name = "tf_router1"
admin_state_up = "true"
external_gateway = "${var.external-gw}"
}
#
# Attach the Router to our Network via an Interface
#
resource "openstack_networking_router_interface_v2" "tf_rtr_if_1" {
region = "nova"
router_id = "${openstack_networking_router_v2.tf_router1.id}"
subnet_id = "${openstack_networking_subnet_v2.tf_net_sub1.id}"
depends_on = [
"openstack_networking_subnet_v2.tf_net_sub1",
]
}
#
# Create some Openstack Floating IP's for our VM's
#
resource "openstack_networking_floatingip_v2" "fip" {
count = "${var.count}"
region = "nova"
pool = "${var.pub-pool}"
depends_on = [
"openstack_networking_router_interface_v2.tf_rtr_if_1",
]
}
resource "openstack_networking_floatingip_v2" "fipdb" {
region = "nova"
pool = "${var.pub-pool}"
depends_on = [
"openstack_networking_router_interface_v2.tf_rtr_if_1",
]
}
#
# Create security group. allowing -
# - ssh
# - icmp
# - mysql
# - web
#
resource "openstack_compute_secgroup_v2" "terraform_sec" {
name = "allow_ssh"
description = "my security group"
rule {
from_port = 22
to_port = 22
ip_protocol = "tcp"
cidr = "0.0.0.0/0"
}
rule {
from_port = 80
to_port = 80
ip_protocol = "tcp"
cidr = "0.0.0.0/0"
}
rule {
from_port = 3306
to_port = 3306
ip_protocol = "tcp"
cidr = "0.0.0.0/0"
}
rule {
from_port = -1
to_port = -1
ip_protocol = "icmp"
cidr = "0.0.0.0/0"
}
}
#
# boots 2 instances of the web server in AZ zone defined in variable.tf
# remove availablity_zone or set to nova if single AZ
# - dependency to subnet creation
#
resource "openstack_compute_instance_v2" "web" {
count = "${var.count}"
name = "${format("web-%02d", count.index+1)}"
image_name = "${var.image-name}"
availability_zone = "${var.az-zone}"
flavor_id = "${var.flavor}"
security_groups = ["${openstack_compute_secgroup_v2.terraform_sec.name}"]
key_pair = "${var.openstack_keypair}"
network {
name = "${openstack_networking_network_v2.tf_network.name}"
}
depends_on = [
"openstack_networking_subnet_v2.tf_net_sub1",
"openstack_networking_floatingip_v2.fip",
]
}
#
# boots 1 instance of the db server in AZ zone defined in variable.tf
# remove availablity_zone or set to nova if single AZ
# - dependency to subnet creation
#
resource "openstack_compute_instance_v2" "db" {
name = "db1"
image_name = "${var.image-name}"
availability_zone = "${var.az-zone}"
flavor_id = "${var.flavor}"
security_groups = ["${openstack_compute_secgroup_v2.terraform_sec.name}"]
key_pair = "${var.openstack_keypair}"
network {
name = "${openstack_networking_network_v2.tf_network.name}"
}
depends_on = [
"openstack_networking_subnet_v2.tf_net_sub1",
"openstack_compute_instance_v2.web",
]
}
#
#associate floating IP to DB server
#
resource "openstack_compute_floatingip_associate_v2" "fipdb" {
floating_ip = "${openstack_networking_floatingip_v2.fipdb.address}"
instance_id = "${openstack_compute_instance_v2.db.id}"
}
#
#associate floating IP to Web Server(s)
#
resource "openstack_compute_floatingip_associate_v2" "fip" {
count = "${var.count}"
floating_ip = "${element(openstack_networking_floatingip_v2.fip.*.address, count.index)}"
instance_id = "${element(openstack_compute_instance_v2.web.*.id, count.index)}"
}
#
# Define provider info
#
provider "openstack" {
user_name = "${var.openstack_user_name}"
tenant_name = "${var.openstack_tenant_name}"
password = "${var.openstack_password}"
auth_url = "${var.openstack_auth_url}"
insecure = "${var.insecure}"
domain_id = "default"
}
#
# Define the varialbles within your Terraform configuration
# update/replace variables to match your deployment
# Provider - OpenStack
#
variable "openstack_user_name" {
description = "The username for the Tenant."
default = "demo"
}
variable "openstack_tenant_name" {
description = "The name of the Tenant."
default = "demo"
}
variable "openstack_password" {
description = "The password for the Tenant."
default = <removed>
}
variable "openstack_auth_url" {
description = "The endpoint url to connect to OpenStack."
default = "https://os.corp.local:5000/v3"
}
variable "openstack_keypair" {
description = "The keypair to be used."
default = "test-pair"
}
variable "insecure" {
type = "string"
description = "Accept self signed SSL certificates?"
default = "true"
}
variable "flavor" {
description = "flavor for vm boot"
default = "2"
}
variable "name_server" {
description = "name servers to be used for VMs"
default = ["192.168.110.10"]
}
variable "pub-pool" {
description = "name of the floating ip pool"
default = "ext-net"
}
variable "image-name" {
description = "name of the image to boot"
default = "ubuntu-16.04-server-cloudimg-amd64"
}
variable "az-zone" {
description = "name of AZ zone to boot"
default = "nova"
}
variable "external-gw" {
description = "Openstack external Network ID"
default = "8338dbf8-67de-41b2-8da3-a38c1ecc607e"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment