On this page, you can find essential steps and code for starting with Terraform and Cloud, from installation to advanced deployment scenarios, highlighting the benefits of infrastructure as code (IaC) for simplifying and automating cloud infrastructure management.
mkdir terraform_basic_vm
cd terraform_basic_vm
terraform {
required_version = ">= 0.14.0"
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.53.0"
}
}
}
This is a configuration block for Terraform, an infrastructure as code tool used for building, changing, and versioning infrastructure safely and efficiently. We will use it in all our examples.
In this specific block:
required_version
: Specifies the minimum version of Terraform required to run this configuration. In this case, it requires version 0.14.0 or newer.
required_providers
: Specifies the providers required by this configuration. Providers are plugins that Terraform uses to interact with different infrastructure APIs. In this block:
openstack
: Specifies the provider for OpenStack, a cloud computing platform. It includes:source
: Specifies where Terraform can find the provider plugin. In this case, it’s hosted on the official Terraform registry under the namespace terraform-provider-openstack/openstack
.version
: Specifies the version constraint for the provider. In this case, it requires version 1.53.0 or newer, but not version 2.0.0.Overall, this configuration ensures that when you run Terraform commands against this project, Terraform will check that it’s using at least version 0.14.0 and that it has the required OpenStack provider version installed to manage the OpenStack infrastructure.
output "instance_ip" {
value = openstack_compute_instance_v2.example_instance.access_ip_v4
}
output "instance_ip"
: This line declares an output variable named “instance_ip”. Outputs in Terraform allow you to extract and display specific information from your infrastructure after it’s been created or modified.
value = openstack_compute_instance_v2.example_instance.access_ip_v4
: This line specifies the value of the output variable. It retrieves the IPv4 address (access_ip_v4
) of an OpenStack compute instance named example_instance
. The openstack_compute_instance_v2
is a Terraform resource type that represents an OpenStack compute instance.
Overall, this output configuration retrieves and displays the IPv4 address of an OpenStack compute instance named example_instance
. After applying the Terraform configuration, you can use the terraform output
command to view the value of the “instance_ip” output variable.
source openrc
Create a new file named main.tf in terraform_basic_vm folder. This file will contain your Terraform configuration. You can use code from first example.
Create id_rsa.pub file and put inside public key (it can be created in another path, do not forger define path in public_key = file("/path_to_file/id_rsa.pub") )
The first two examples (Basic virtual machine setup and Complex virtual machine setup) imply that all 6 steps have been completed;
In the third example (Basic Kubernetes setup), you need to skip the third step, since all the necessary information for connecting to the k8s cluster will be saved to a file.
terraform init
terraform plan
terraform apply
terraform destroy
SSH Key Pair Creation
openstack_compute_keypair_v2
resource.Security Group Creation
openstack_networking_secgroup_v2
resource.Ingress Rules Configuration
openstack_networking_secgroup_rule_v2
resources:Instance Creation
openstack_compute_instance_v2
resource.This code can be used to provision resources in the cloud, including creating VM instance with SSH access configured via key pairs and security groups. Adjustments can be made to customize the deployment according to specific requirements.
// Add ssh public key
resource "openstack_compute_keypair_v2" "example_keypair" {
name = "example-keypair"
public_key = file("./id_rsa.pub") // or can be public_key =("ssh-rsa AAAAB3.......")
}
//Create security group with name sec_group
resource "openstack_networking_secgroup_v2" "sec_group" {
name = "sec_group"
description = "Master sec group"
}
//Add ingress rule ssh in security group with name sec_group
resource "openstack_networking_secgroup_rule_v2" "allow_ssh" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 22
port_range_max = 22
remote_ip_prefix = "0.0.0.0/0" //For more secure add your IP/32
security_group_id = "${openstack_networking_secgroup_v2.sec_group.id}"
}
//Add ingress rule ICMP all options in security group with name sec_group
resource "openstack_networking_secgroup_rule_v2" "allow_icmp" {
direction = "ingress"
ethertype = "IPv4"
protocol = "icmp"
remote_ip_prefix = "0.0.0.0/0" //For more secure add your IP/32
security_group_id = "${openstack_networking_secgroup_v2.sec_group.id}"
}
//Create instance with name example_instance in public network with ssh key and security group
resource "openstack_compute_instance_v2" "example_instance" {
name = "example-instance"
image_name = "ubuntu-server-22.04-LTS-20240110"
flavor_name = "VC-2"
key_pair = openstack_compute_keypair_v2.example_keypair.name
security_groups = [openstack_networking_secgroup_v2.sec_group.name]
network {
name = "public"
}
}
SSH Key Pair Creation
openstack_compute_keypair_v2
resource.Security Group Creation
openstack_networking_secgroup_v2
resource.Ingress Rules Configuration
openstack_networking_secgroup_rule_v2
resources:Instance Creation
openstack_compute_instance_v2
resource.user_data
attribute, which installs Nginx and enables unattended upgrades.This code can be used to provision resources in the cloud, including creating VM instance with SSH and Web access configured via key pairs and security groups. Adjustments can be made to customize the deployment according to specific requirements.
//Extended VM creation (cloud init + couple rules in sec group)
resource "openstack_compute_keypair_v2" "example_keypair" {
name = "example-keypair"
public_key = file("./id_rsa.pub")
}
//Create security group with name sec_group
resource "openstack_networking_secgroup_v2" "sec_group" {
name = "sec_group"
description = "Master sec group"
}
//Add ingress rule ssh in security group with name sec_group
resource "openstack_networking_secgroup_rule_v2" "allow_ssh" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 22
port_range_max = 22
remote_ip_prefix = "0.0.0.0/0" //For more secure add your IP/32
security_group_id = "${openstack_networking_secgroup_v2.sec_group.id}"
}
//Add ingress rule http in security group with name sec_group
resource "openstack_networking_secgroup_rule_v2" "allow_http" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 80
port_range_max = 80
remote_ip_prefix = "0.0.0.0/0" //For more secure add your IP/32
security_group_id = "${openstack_networking_secgroup_v2.sec_group.id}"
}
//Add ingress rule https in security group with name sec_group
resource "openstack_networking_secgroup_rule_v2" "allow_https" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_ip_prefix = "0.0.0.0/0" //For more secure add your IP/32
security_group_id = "${openstack_networking_secgroup_v2.sec_group.id}"
}
//Add ingress rule ICMP all options in security group with name sec_group
resource "openstack_networking_secgroup_rule_v2" "allow_icmp" {
direction = "ingress"
ethertype = "IPv4"
protocol = "icmp"
remote_ip_prefix = "0.0.0.0/0" //For more secure add your IP/32
security_group_id = "${openstack_networking_secgroup_v2.sec_group.id}"
}
resource "openstack_compute_instance_v2" "example_instance" {
name = "example-instance"
image_name = "ubuntu-server-22.04-LTS-20240110"
flavor_name = "VC-2"
key_pair = openstack_compute_keypair_v2.example_keypair.name
security_groups = [openstack_compute_secgroup_v2.sec_group.name]
network {
name = "public"
}
user_data = <<-EOF
#cloud-config
packages:
- nginx
- unattended-upgrades
package_update: true
package_upgrade: true
EOF
}
Keypair Creation:
An OpenStack compute SSH key pair named “example-keypair” is created using the openstack_compute_keypair_v2
resource.
Kubernetes Cluster Creation:
An OpenStack Container Infrastructure (COI) cluster named “cluster_1” is created using the openstack_containerinfra_cluster_v1
resource.
Get Kubernetes Configuration A null resource named “get_kubeconfig” is used to execute a local command to retrieve the Kubernetes configuration.
This code can be used to provision resources in the cloud, including creating k8s cluster. Adjustments can be made to customize the deployment according to specific requirements.
#Keypair
resource "openstack_compute_keypair_v2" "example_keypair" {
name = "example-keypair"
public_key = file("./id_rsa.pub")
}
#Create k8s cluster
resource "openstack_containerinfra_cluster_v1" "cluster_1" {
name = "cluster_1"
cluster_template_id = "v1.28.4"
master_count = 1
node_count = 1
keypair = "example-keypair"
master_flavor = "VC-4"
flavor = "VC-2"
floating_ip_enabled = true
}
//Get configuration
resource "null_resource" "get_kubeconfig" {
depends_on = [openstack_containerinfra_cluster_v1.cluster_1]
provisioner "local-exec" {
command = "mkdir -p ./kubeconfig && openstack coe cluster config ${openstack_containerinfra_cluster_v1.cluster_1.name} --dir ./kubeconfig"
}
}