0% found this document useful (0 votes)
15 views8 pages

Terraform Course

This document provides a step-by-step guide for setting up and deploying virtual machines on OpenStack using Terraform. It includes instructions for downloading necessary files, configuring the environment, creating Terraform configuration files, and executing commands to manage resources. The guide also covers creating a simple web server and managing floating IPs and volumes associated with the VMs.

Uploaded by

Anoir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Terraform Course

This document provides a step-by-step guide for setting up and deploying virtual machines on OpenStack using Terraform. It includes instructions for downloading necessary files, configuring the environment, creating Terraform configuration files, and executing commands to manage resources. The guide also covers creating a simple web server and managing floating IPs and volumes associated with the VMs.

Uploaded by

Anoir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

mkdir ~/terraform

bash mkdir ~/terraform

cd ~/terraform

connect to cloud

Download OpenStack RC Files

• Step: Log in to your OpenStack Horizon dashboard using the provided URL
(http://ctl.openstack3.ensam-pg0.clemson.cloudlab.us/horizon/auth/login/).
Go to "Project" -> "API Access". Click on "Voir les données d'identification"
(View credentials) or "Télécharger le fichier RC d'OpenStack" (Download
OpenStack RC File). Download both "Fichier OpenStack clouds.yaml" and
"Fichier OpenStack RC" (which is typically admin-openrc.sh).

6. copier les fichiers admin-openrc.sh et clouds.yaml vers le dossier ~/terraform

cp /path/to/downloaded/admin-openrc.sh ~/terraform/ cp
/path/to/downloaded/clouds.yaml ~/terraform/

7. Installer OpenStack client

Sudo apt install python-dev python-pip

Sudo pip install python-openstackclient

8.Ajouter dans le fichier /etc/hosts

@IP-de-ctl ctl

# First, find the IP address of ctl.openstack3.ensam-pg0.clemson.cloudlab.us


ping -c 1 ctl.openstack3.ensam-pg0.clemson.cloudlab.us # Let's say the IP is 'X.X.X.X'
echo "X.X.X.X ctl" | sudo tee -a /etc/hosts # Replace 'X.X.X.X' with the actual IP

9. Apres, taper la commande suivante pour ce connecter au cloud openstack par cli

source admin-openrc.sh

10. Utiliser les commandes suivantes pour afficher les informations sur openstack
openstack image list

openstack flavor list

openstack keypair list

openstack network list

11. Create provider.tf

Step: Create a file named provider.tf in your ~/terraform directory. This file tells Terraform
how to connect to OpenStack.

cat << EOF > ~/terraform/provider.tf

terraform {

required_version = ">= 0.14.0"

required_providers {

openstack = {

source = "terraform-provider-openstack/openstack"

version = "~> 1.48.0"

provider "openstack" {

cloud = "openstack"

EOF
12. Test Terraform Initialization

terraform init

13. Create deploy.tf for VM Creation

cat << EOF > ~/terraform/deploy.tf

resource "openstack_compute_instance_v2" "yourname-VM1" { # Replace


'yourname'

name = "yourname-VM1" # Replace 'yourname'

image_id = "ACTUAL_IMAGE_ID_FROM_LIST" # e.g., "403ba7e6-d94e-4b25-


87da-3385fe7223c7"

flavor_id = "ACTUAL_FLAVOR_ID_FROM_LIST" # e.g., "2" (for small)

security_groups = ["default"]

network {

uuid = "ACTUAL_NETWORK_UUID_FROM_LIST" # e.g., "8673acca-381a-496b-


a2f7-ef756b3c8953" (for flat-lan-1-net)

EOF

14. Validate and Plan Terraform Deployment

terraform validate

terraform plan

15. Launch VM and Verify

terraform apply

ls terraform.tfstate* # Verify VM is created [cite: 1]

openstack server show yourname-VM1 # Replace 'yourname-VM1' with your


VM's name to see details [cite: 1]
16. Assign a Floating IP to the VM

Command to append to deploy.tf:

cat << EOF >> ~/terraform/deploy.tf

resource "openstack_networking_floatingip_v2" "myvm_fip" {

pool = "ext-net"

resource "openstack_compute_floatingip_associate_v2" "myvm_fip" {

floating_ip = openstack_networking_floatingip_v2.myvm_fip.address

instance_id = openstack_compute_instance_v2.yourname-VM1.id # Ensure this


matches your VM name

EOF

17. Execute terraform apply for Floating IP

terraform apply

openstack server list

18. Add SSH Keypair to VM

cat << EOF >> ~/terraform/deploy.tf

resource "openstack_compute_keypair_v2" "my-keypair" {

name = "my-keypair"

public_key = file("~/.ssh/id_rsa.pub") # Ensure this path is correct for your


public key

}
# IMPORTANT: You'll need to modify the existing
openstack_compute_instance_v2 block

# to include the 'key_pair' argument.

# It's generally better to edit the file directly with a text editor.

# Example modification for your existing 'yourname-VM1' resource:

# resource "openstack_compute_instance_v2" "yourname-VM1" {

# name = "yourname-VM1"

# image_id = "ACTUAL_IMAGE_ID_FROM_LIST"

# flavor_id = "ACTUAL_FLAVOR_ID_FROM_LIST"

# key_pair = "my-keypair" # ADD THIS LINE

# security_groups = ["default"]

# network {

# uuid = "ACTUAL_NETWORK_UUID_FROM_LIST"

# }

#}

EOF

19. Create and Attach a Volume

cat << EOF >> ~/terraform/deploy.tf

resource "openstack_blockstorage_volume_v2" "my-volume" {

name = "my-volume"

size = 10

}
resource "openstack_compute_volume_attach_v2" "volumes" {

instance_id = openstack_compute_instance_v2.yourname-VM1.id # Ensure


this matches your VM name

volume_id = openstack_blockstorage_volume_v2.my-volume.id

EOF

Command to apply and verify:

terraform apply

openstack volume list

20. Build a Simple Web Server

Step 20.1: Destroy Existing Resources

terraform destroy

Step 20.2: Create Bootstrap Script

cat << 'EOF' > ~/terraform/bootstrap.sh

#!/bin/bash

apt-get update

apt-get install -y apache2

EOF

Step 20.3: Modify deploy.tf with user_data

cat << EOF > ~/terraform/deploy.tf

resource "openstack_compute_instance_v2" "vm2" {

name = "vm2"

image_id = "ACTUAL_IMAGE_ID_FOR_BIONIC_SERVER" # e.g., "c80fd967-


b6c0-4970-8c6e-1b4b654b1d24" [cite: 18]

flavor_id = "ACTUAL_FLAVOR_ID_FOR_SMALL" # e.g., "2" [cite: 18]

key_pair = "my-keypair" # Ensure 'my-keypair' from step 18 exists

security_groups = ["default"]
user_data = file("bootstrap.sh") # Execute the script [cite: 18]

network {

uuid = "ACTUAL_NETWORK_UUID" # e.g., "82315ada-3802-4f57-983-


6f7fc6b0b3f9" [cite: 18]

# Optionally, add floating IP for vm2 if needed for external access

resource "openstack_networking_floatingip_v2" "vm2_fip" {

pool = "ext-net"

resource "openstack_compute_floatingip_associate_v2" "vm2_fip_associate" {

floating_ip = openstack_networking_floatingip_v2.vm2_fip.address

instance_id = openstack_compute_instance_v2.vm2.id

EOF

Step 20.4: Execute and Verify

terraform apply

Step 20.5: Destroy Instances

terraform destroy

You might also like