We can deploy cloud Infrastructure on IDrive® Compute with the help of widely used open-source infrastructure as code software Terraform. While we can deploy various virtual resources on IDrive® Compute with Terraform, In this article we will try to concentrate on creating IDrive Compute instances.
Pre-requisite:
We need to install the following software on your local system to deploy an IDrive® Compute instance using Terraform.
To create an IDrive® Compute instance we need the below information:
- Network
- Flavors
- Image
- Security Group
You can get the required data using the below OpenStack commands:
Make sure you authenticate with OpenStack-CLI before proceeding.
Note: You can get your application credentials from the UI of your IDrive® Compute Dashboard
Example of credential file:
#!/usr/bin/env bash; export OS_AUTH_TYPE=v3applicationcredential export OS_AUTH_URL=https://identity.api.idrivecompute.io/v3 export OS_IDENTITY_API_VERSION=3 export OS_REGION_NAME=<region_code> export OS_INTERFACE=public export OS_APPLICATION_CREDENTIAL_ID=<credential_id> export OS_APPLICATION_CREDENTIAL_SECRET=<credential_secret>
Network:
Get the network on which you want to deploy your instance.
$ openstack network list -f json
[ { "ID": "74628c66-d90d-4d50-ace6-bd5816f2a4ba", "Name": "provider.public", "Subnets": [ "124bae6d-71dc-40e5-aa09-fd77ee6ba430", "930601ee-dc05-4699-95e8-284e5356f13e", "6dc5fd86-35ae-41bd-b244-d6461ed18696" ] } ]
For IDrive® Compute we spin up our instances on "provider. public network".
Flavor:
$ openstack flavor list
[ { "ID": "06424e65-d29c-4147-88b9-8a716565e232", "Name": "LA3/DCPU 32GB/8CPU/480GB", "RAM": 32768, "Disk": 480, "Ephemeral": 0, "VCPUs": 8, "Is Public": true }, { "ID": "1468c56b-a5e4-4d84-8b2c-ded1dd22792d", "Name": "LA3/SCPU 4GB/2CPU/80GB", "RAM": 4096, "Disk": 80, "Ephemeral": 0, "VCPUs": 2, "Is Public": true }, { "ID": "1a048f8f-69c3-4fd0-bdc4-1c3ebc7f9ada", "Name": "LA3/SCPU 56GB/28CPU/1120GB", "RAM": 57344, "Disk": 1120, "Ephemeral": 0, "VCPUs": 28, "Is Public": true } ]
Select the flavor for the instance you want to create. In this instance, we will proceed with "LA3/SCPU 4GB/2CPU/80GB"
Image:
$ openstack image list
[ { "ID": "099f4835-575f-41d8-b287-ed12fdcf0c17", "Name": "AlmaLinux-8.0-x86_64", "Status": "active" }, { "ID": "292b08e6-ad43-4eb6-a4df-bf6fdfa7c038", "Name": "AlmaLinux-9.0-x86_64", "Status": "active" }, { "ID": "841a2653-9893-4f63-b9a5-8e63a4b95bee", "Name": "Ubuntu-22.04 LTS-x86_64", "Status": "active" } ]
choose the image for the instance to be created. We pick "Ubuntu-22.04 LTS-x86_64" for the instance.
Once we have the required information for creating Instances, we proceed with the creation of configuration files for Terraform to work on
Terraform code:
Here are the files I ended up with.
1) Providers file:
Create a file named main.tf and write the file with the below code.
$ touch main.tf
Append the following configuration to the main.tf file.
# Configure the OpenStack Provider
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
}
}
}
2) Variables.tf for the terraform variables.
$ touch variables.tf
Add the below variables to the file.
Note: You should be already having an existing ssh-keypair in the OpenStack to access the instance. check out this official article for creating keypairs on openstack. Here we are using ssh-keys named "my-key"
variable "image" {
default = "Ubuntu-22.04 LTS-x86_64"
}
variable "flavor" {
default = "LA3/SCPU 4GB/2CPU/80GB"
}
variable "ssh_key_pair" {
default = "mykeypair"
}
variable "ssh_user_name" {
default = "root"
}
variable "availability_zone" {
default = "nova"
}
variable "security_group" {
default = "default_firewall"
}
variable "network" {
default = "provider.public"
}
3) Deployment file: deploy.tf
$ touch deploy.tf
Add the below code to deploy.tf file.
resource "openstack_compute_instance_v2" "vm1" {
count = "1"
name = "demo2"
image_name = "${var.image}"
availability_zone = "${var.availability_zone}"
flavor_name = "${var.flavor}"
key_pair = "${var.ssh_key_pair}"
security_groups = ["${var.security_group}"]
network {
name = "${var.network}"
}
}
Using Terraform to deploy an OpenStack Instance:
We execute the below operations in sequence.
- terraform init ( Needs to be executed only once for initializing)
- terraform plan ( To see what's being deployed)
- terraform deploy (creates the requested resources)
Initializing terraform:
$ terraform init
Output:
Initializing the backend...
Initializing provider plugins...
- Finding latest version of terraform-provider-openstack/openstack...
- Installing terraform-provider-openstack/openstack v1.49.0...
- Installed terraform-provider-openstack/openstack v1.49.0 (self-signed, key ID 4F80527A391BEFD2)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Applying to terraform plan:
$ terraform plan
Output:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# openstack_compute_instance_v2.vm1[0] will be created
+ resource "openstack_compute_instance_v2" "vm1" {
+ access_ip_v4 = (known after apply)
+ access_ip_v6 = (known after apply)
+ all_metadata = (known after apply)
+ all_tags = (known after apply)
+ availability_zone = "nova"
+ created = (known after apply)
+ flavor_id = (known after apply)
+ flavor_name = "LA3/SCPU 4GB/2CPU/80GB"
+ force_delete = false
+ id = (known after apply)
+ image_id = (known after apply)
+ image_name = "Ubuntu-22.04 LTS-x86_64"
+ key_pair = "mykeypair"
+ name = "demo2"
+ power_state = "active"
+ region = (known after apply)
+ security_groups = [
+ "default_firewall",
]
+ stop_before_destroy = false
+ updated = (known after apply)
+ network {
+ access_network = false
+ fixed_ip_v4 = (known after apply)
+ fixed_ip_v6 = (known after apply)
+ floating_ip = (known after apply)
+ mac = (known after apply)
+ name = "provider.public"
+ port = (known after apply)
+ uuid = (known after apply)
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
Applying the configuration:
$ terraform apply
Output:
openstack_blockstorage_volume_v3.volume_1: Refreshing state... [id=9f82665c-1d93-4298-a517-54c0a27c1812]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# openstack_compute_instance_v2.vm1[0] will be created
+ resource "openstack_compute_instance_v2" "vm1" {
+ access_ip_v4 = (known after apply)
+ access_ip_v6 = (known after apply)
+ all_metadata = (known after apply)
+ all_tags = (known after apply)
+ availability_zone = "nova"
+ created = (known after apply)
+ flavor_id = (known after apply)
+ flavor_name = "LA3/SCPU 4GB/2CPU/80GB"
+ force_delete = false
+ id = (known after apply)
+ image_id = (known after apply)
+ image_name = "Ubuntu-22.04 LTS-x86_64"
+ key_pair = "mykeypair"
+ name = "demo2"
+ power_state = "active"
+ region = (known after apply)
+ security_groups = [
+ "default_firewall",
]
+ stop_before_destroy = false
+ updated = (known after apply)
+ network {
+ access_network = false
+ fixed_ip_v4 = (known after apply)
+ fixed_ip_v6 = (known after apply)
+ floating_ip = (known after apply)
+ mac = (known after apply)
+ name = "provider.public"
+ port = (known after apply)
+ uuid = (known after apply)
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
openstack_compute_instance_v2.vm1[0]: Creating...
openstack_compute_instance_v2.vm1[0]: Still creating... [10s elapsed]
openstack_compute_instance_v2.vm1[0]: Still creating... [20s elapsed]
openstack_compute_instance_v2.vm1[0]: Creation complete after 26s [id=1ac3f501-23d2-4746-baf3-707cd79ff076]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Verification:
You can verify the deployment of the instance by running the "openstack server list" command.
$ openstack server list -f json
Output:
[ { "ID": "1ac3f501-23d2-4746-baf3-707cd79ff076", "Name": "demo2", "Status": "ACTIVE", "Networks": { "provider.public": [ "2606:6e00:8000:3:f816:3eff:fe3f:becc", "72.26.117.222" ] }, "Image": "Ubuntu-22.04 LTS-x86_64", "Flavor": "LA3/SCPU 4GB/2CPU/80GB" } ]