Skip to main content

Terraform guide

This guide covers Terraform fundamentals, provider configuration, and how to manage KakaoCloud resources.

Terraform fundamentals​

Terraform is an Infrastructure as Code (IaC) tool for defining and managing infrastructure. Write the required resources and settings in configuration files, and Terraform compares them with the current state to create and apply an execution plan.

Configuration files can be managed in a version control system so that teams can track changes and review them together. You can also group reusable configurations into modules and use them across development, testing, and production environments. For more information about Terraform, see the HashiCorp Terraform introduction.

The following concepts are used in Terraform configurations.

ConceptDescription
ProviderA plugin that connects Terraform to an external service API
ResourceAn infrastructure object that Terraform can create, update, or delete. Define resources in a resource block
Data sourceA way to retrieve information about existing resources and use it in a configuration. Define data sources in a data block; they do not create or change the target
StateData that records the relationship between resources in a configuration and the actual infrastructure objects
PlanThe result of comparing the configuration with the current infrastructure and showing the changes to create, update, or delete

KakaoCloud Terraform Provider​

The KakaoCloud Terraform Provider is a plugin that connects Terraform to the KakaoCloud API. Use it to manage KakaoCloud compute, networking, database, and Kubernetes resources in Terraform configuration files.

Check the provider version

The supported resources and data sources depend on the provider version. Select the version you use, then check the required arguments and examples for each item in the Terraform Registry.

Prerequisites​

Prepare the following to use Terraform and the provider.

CategoryRequirement
TerraformTerraform CLI 1.14 or later
ProviderApplication credential ID and secret for KakaoCloud authentication
KakaoCloud projectPermission to manage the project and its resources

For information about issuing credentials, see Manage credentials.

Configure the provider​

Specify the provider source and version in the main.tf file in your working directory. The following example uses version 0.4.5 and passes credentials through environment variables.

terraform {
required_version = ">= 1.14.0"

required_providers {
kakaocloud = {
source = "kakaoenterprise/kakaocloud"
version = "0.4.5"
}
}
}

provider "kakaocloud" {}

Authentication​

The provider checks authentication arguments in the provider block first, then checks the corresponding environment variables. The arguments and environment variables are listed below.

CredentialProvider argumentEnvironment variable
Application credential IDapplication_credential_idKAKAOCLOUD_APPLICATION_CREDENTIAL_ID
Application credential secretapplication_credential_secretKAKAOCLOUD_APPLICATION_CREDENTIAL_SECRET

When using environment variables, set the following values in the terminal where you run Terraform. Replace the example values with your issued credentials.

export KAKAOCLOUD_APPLICATION_CREDENTIAL_ID="<APPLICATION_CREDENTIAL_ID>"
export KAKAOCLOUD_APPLICATION_CREDENTIAL_SECRET="<APPLICATION_CREDENTIAL_SECRET>"

To use provider arguments, replace the empty provider block above and declare input variables as follows. Pass the variable values when you run Terraform.

variable "application_credential_id" {
type = string
sensitive = true
}

variable "application_credential_secret" {
type = string
sensitive = true
}

provider "kakaocloud" {
application_credential_id = var.application_credential_id
application_credential_secret = var.application_credential_secret
}
Protect credentials

Do not commit configuration or variable files that contain actual credentials to a source code repository.

For more information about authentication, see the KakaoCloud Terraform Provider official documentation.

Basic workflow​

After configuring the provider and credentials, write the resource configuration to create infrastructure. The provider configuration alone does not create resources.

StepTaskDescription
1. Write the configurationWrite .tf filesDefine resources and required arguments using examples from the official documentation
2. Initializeterraform initInitialize the working directory and install the required provider
3. Validate the configurationterraform validateCheck the configuration syntax and internal consistency. This does not guarantee API access or that resources can be created
4. Review the planterraform planReview the resources and attributes to create, update, or delete
5. Applyterraform applyReview and approve the execution plan to apply changes to the infrastructure
Review the plan before applying

Review the plan after modifying the configuration and before applying changes. Changes to some attributes may replace a resource, so check which resources will be deleted and recreated.

Manage state​

Terraform uses state to connect resources in the configuration with actual infrastructure objects. By default, state is stored in the terraform.tfstate file in the working directory.

Protect the state file

State files may contain sensitive information, so do not commit them to a Git repository. When multiple people manage the same infrastructure, use a remote backend that supports secure access control and state locking.

For information about state storage and locking support by backend, see Terraform state.

Terraform tutorials​