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 Images.
Pre-requisite:
We need to install the following software on your local system.
Terraform code:
Here are the terraform files required for the creation.
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) Deployment file: deploy.tf
$ touch deploy.tf
Add the below code to deploy.tf file.
resource "openstack_images_image_v2" "cirros" {
name = "cirros"
image_source_url = "https://download.cirros-cloud.net/0.6.1/cirros-0.6.1-x86_64-disk.img"
container_format = "bare"
disk_format = "qcow2"
region = "LA3"
properties = {
key = "value"
}
}
Here we are creating an image resource by the name "cirros" in the region "LA3".
Modify the variables, image_sour_url, container_format and disk_format as per your requirement.
Alternatively, you can also use the "local_file_path" variable to upload any image from your local machine.
Using Terraform to create a Image resource
we execute the below operations:
- 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 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_images_image_v2.cirros will be created
+ resource "openstack_images_image_v2" "cirros" {
+ checksum = (known after apply)
+ container_format = "bare"
+ created_at = (known after apply)
+ disk_format = "qcow2"
+ file = (known after apply)
+ hidden = false
+ id = (known after apply)
+ image_cache_path = "/root/.terraform/image_cache"
+ image_id = (known after apply)
+ image_source_url = "https://download.cirros-cloud.net/0.6.1/cirros-0.6.1-x86_64-disk.img"
+ metadata = (known after apply)
+ min_disk_gb = 0
+ min_ram_mb = 0
+ name = "cirros"
+ owner = (known after apply)
+ properties = {
+ "key" = "value"
}
+ protected = false
+ region = "LA3"
+ schema = (known after apply)
+ size_bytes = (known after apply)
+ status = (known after apply)
+ update_at = (known after apply)
+ updated_at = (known after apply)
+ visibility = "private"
}
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:
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_images_image_v2.cirros will be created
+ resource "openstack_images_image_v2" "cirros" {
+ checksum = (known after apply)
+ container_format = "bare"
+ created_at = (known after apply)
+ disk_format = "qcow2"
+ file = (known after apply)
+ hidden = false
+ id = (known after apply)
+ image_cache_path = "/root/.terraform/image_cache"
+ image_id = (known after apply)
+ image_source_url = "https://download.cirros-cloud.net/0.6.1/cirros-0.6.1-x86_64-disk.img"
+ metadata = (known after apply)
+ min_disk_gb = 0
+ min_ram_mb = 0
+ name = "cirros"
+ owner = (known after apply)
+ properties = {
+ "key" = "value"
}
+ protected = false
+ region = "LA3"
+ schema = (known after apply)
+ size_bytes = (known after apply)
+ status = (known after apply)
+ update_at = (known after apply)
+ updated_at = (known after apply)
+ visibility = "private"
}
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_images_image_v2.cirros: Creating...
openstack_images_image_v2.cirros: Still creating... [10s elapsed]
openstack_images_image_v2.cirros: Creation complete after 12s [id=2443bb4e-ee3f-4aad-ac7a-4003f5814b5a]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Verification:
You can verify the successful creation of an Image on IDrive® Compute with the below command.
$ openstack image show cirros -f json
Output:
{
"checksum": "0c839612eb3f2469420f2ccae990827f",
"container_format": "bare",
"created_at": "2023-02-06T07:34:06Z",
"disk_format": "qcow2",
"file": "/v2/images/7c85ce04-d7dd-4875-9a92-757cf3ffffbe/file",
"id": "7c85ce04-d7dd-4875-9a92-757cf3ffffbe",
"min_disk": 0,
"min_ram": 0,
"name": "cirros",
"owner": "b88176b3de3f4771aa183e8bd7e1edc7",
"properties": {
"os_hidden": false,
"os_hash_algo": "sha512",
"os_hash_value": "df88bac2791254f68941229792539621516bd480aa3d6fe4c0ca16057393d024a4944d644959f323dc01a25e3417c0b581776ab3c8db8da542039f2a67230579",
"key": "value"
},
"protected": false,
"schema": "/v2/schemas/image",
"size": 21233664,
"status": "active",
"tags": [],
"updated_at": "2023-02-06T07:34:10Z",
"visibility": "private"
}