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.
| Concept | Description |
|---|---|
| Provider | A plugin that connects Terraform to an external service API |
| Resource | An infrastructure object that Terraform can create, update, or delete. Define resources in a resource block |
| Data source | A 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 |
| State | Data that records the relationship between resources in a configuration and the actual infrastructure objects |
| Plan | The 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.
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.
| Category | Requirement |
|---|---|
| Terraform | Terraform CLI 1.14 or later |
| Provider | Application credential ID and secret for KakaoCloud authentication |
| KakaoCloud project | Permission 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.
| Credential | Provider argument | Environment variable |
|---|---|---|
| Application credential ID | application_credential_id | KAKAOCLOUD_APPLICATION_CREDENTIAL_ID |
| Application credential secret | application_credential_secret | KAKAOCLOUD_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
}
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.
| Step | Task | Description |
|---|---|---|
| 1. Write the configuration | Write .tf files | Define resources and required arguments using examples from the official documentation |
| 2. Initialize | terraform init | Initialize the working directory and install the required provider |
| 3. Validate the configuration | terraform validate | Check the configuration syntax and internal consistency. This does not guarantee API access or that resources can be created |
| 4. Review the plan | terraform plan | Review the resources and attributes to create, update, or delete |
| 5. Apply | terraform apply | Review and approve the execution plan to apply changes to the infrastructure |
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.
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.
Related documentation
- KakaoCloud Terraform Provider official documentation ↗: Provider settings, resource and data source arguments, and examples by version
- KakaoCloud Terraform Provider source code ↗: Implementation code and changes
Terraform tutorials
- Provision and update VM with Terraform: Create a VM in an existing subnet and practice updating and deleting resources with Terraform
- Build MySQL-based 3-Tier web service with Terraform: Configure KakaoCloud resources with Terraform and deploy a 3-Tier web service