freesky-edward

Using Terraform With OTC Cloud

published on 10 Apr 2018.

Copyright belongs to huaweicloud, the address is: https://github.com/huaweicloud. the author is niuzhenguo

Managing Open Telekom Cloud infrastructure as code with Terraform

Managing cloud resources usually involves a web interface or a command-line interface, which works great for individuals and small teams, but can be troublesome for larger teams with complex requirements. To manage this complexity, users of Open Telekom Cloud can incorporate the “Infrastructure as code” methodology of Terraform for provisioning and managing their cloud resources.

HashiCorp Terraform is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned in the same way that software developers can with application code. It enables you to safely and predictably create, change, and improve infrastructure.

Configuring the environment

To unlock Terraform magic in Open Telekom Cloud, you need to allow Terraform scripts to provision resources on your behalf. This requires some sort of configuration to provide authentication information, endpoint URLs, etc. And because of high level of access granted, you should always protect the security information generated by those scripts.

A provider configuration looks like the following:

provider "opentelekomcloud" {
    user_name   = "user"
    tenant_name = "tenant"
    domain_name = "domain"
    password    = "password"
    auth_url    = "https://iam.eu-de.otc.t-systems.com/v3"
    region      = "eu-de"
}

In the “provider” section, you tell Terraform to use opentelekomcloud provider to provision resources in the scripts. Then you can write your resource sections to instruct Terraform to create them.

Here’s a sample Terraform configuration for creating an instance on Open Telekom Cloud:

resource "opentelekomcloud_compute_instance_v2" "webserver" {
    name            = "webserver"
    image_name      = "image"
    flavor_name     = "flavor"
    key_pair        = "keypair"
    security_groups = [
        "default"
    ]
    network {
        uuid        = "net-uuid"
    }
}

Executing the Script

Terraform separates the planning phase from the execution phase by using the concept of an execution plan. And, notably, Terraform presents a detailed and readable summary of the changes that will be applied. The terraform apply command makes the changes to real infrastructure.

$ terraform plan
+ opentelekomcloud_compute_instance_v2.webserver
    name:       "webserver"
    image_name: "image"
    key_pair:   "keypair"
    ...

Everything looks correct, then go ahead and provision this new instance.

$ terraform apply
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

If you look in the Open Telekom Cloud portal now, you should see this instance running there. Terraform can also detects changes to the configuration and only applies the difference of the changes. If you change some updatable attributes on the script above like name, it will only update the name of the instance resource.

There are many benefits of infrastructure as code with Terraform to manage resources on Open Telekom Cloud. For more information, please visit the Open Telekom Cloud Terraform provider website.