Terraform は、HashiCorp が提供するオープンソースツールです。Terraform を使用すると、安全かつ効率的な方法で、クラウドインフラストラクチャとリソースのプレビュー、構成、管理ができます。Terraform を構成して、Alibaba Cloud のインフラストラクチャとリソースを自動的に作成・更新し、バージョン管理を実行できます。このトピックでは、Terraform を使用して Key Management Service (KMS) リソースを管理する方法について説明します。
前提条件
デフォルトでは、Cloud Shell には Terraform がプリインストールされており、Alibaba Cloud アカウント情報が設定されています。Cloud Shell を使用する場合、他の操作は不要です。Cloud Shell を使用しない場合は、Terraform をインストールし、ご利用の Alibaba Cloud アカウント情報を設定する必要があります。
Terraform v0.14 以降がインストールされていること。詳細については、「ローカル PC での Terraform のインストールと設定」をご参照ください。
説明Terraform をインストールした後、terraform --version コマンドを実行して Terraform のバージョンを確認できます。Terraform のバージョンが v0.14 より前の場合は、必要なバージョンの Terraform をインストールして元のバージョンを上書きしてください。Terraform の詳細については、「Terraform とは」をご参照ください。
ご利用の Alibaba Cloud アカウント情報を設定します。
説明権限管理の柔軟性とセキュリティを向上させるために、Terraform という名前の Resource Access Management (RAM) ユーザーを作成することを推奨します。次に、その RAM ユーザーの AccessKey ペアを作成し、RAM ユーザーに権限を付与します。詳細については、「RAM ユーザーの作成」および「RAM ユーザーへの権限付与」をご参照ください。
環境変数で ID 情報を指定することを推奨します。環境変数の設定方法は、オペレーティングシステムによって異なります。詳細については、「Linux、macOS、Windows での環境変数の設定」をご参照ください。
export ALICLOUD_ACCESS_KEY="******" export ALICLOUD_SECRET_KEY="******" export ALICLOUD_REGION="******"設定ファイルの
providerコードブロックで ID 認証情報を指定します。provider "alicloud" { access_key = "******" secret_key = "******" region = "******" }
Terraform を使用したキーの作成
作業ディレクトリを作成し、その作業ディレクトリに main.tf と variables.tf という名前の設定ファイルを作成します。
main.tf:このファイルは Terraform のメインファイルであり、デプロイするリソースを定義します。
# alicloud_kms_key リソースの詳細については、https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/kms_key をご参照ください。 resource "alicloud_kms_key" "dkms_key" { description = "${var.description}" protection_level = "${var.protection_level}" dkms_instance_id = "${var.dkms_instance_id}" } output "dkms_key_id" { value = alicloud_kms_key.dkms_key.id }variables.tf:このファイルには、main.tf に渡すことができる変数が含まれています。これらの変数を使用して、環境をカスタマイズできます。
# キーの説明。 variable "description" { default = "the new dkms key" } # 定期削除期間。この期間が経過すると、キーは削除されます。定期削除期間中、キーは「削除保留中」状態になります。定期削除期間が経過すると、キーの削除タスクはキャンセルできなくなります。 variable "pending_window_in_days" { default = "7" } # ソフトウェアキー管理タイプのインスタンスの場合、このパラメーターを SOFTWARE に設定します。 # ハードウェアキー管理タイプのインスタンスの場合、このパラメーターを HSM に設定します。 variable "protection_level" { default = "SOFTWARE" } # ご利用の KMS インスタンスの ID。 variable "dkms_instance_id" { default = "kst-xxxxxxxxxxxxxxx" }
terraform initコマンドを実行して、Terraform ランタイム環境を初期化します。想定される出力:
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.183.0... - Installed hashicorp/alicloud v1.183.0 (signed by HashiCorp) 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. ╷ │ Warning: Additional provider information from registry │ │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud: │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers. ╵ 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.terraform planコマンドを実行して、リソースプランを生成します。想定される出力:
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: # alicloud_kms_key.dkms_key will be created + resource "alicloud_kms_key" "dkms_key" { ... } Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + dkms_key_id = (known after apply) ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 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.terraform applyコマンドを実行して、キーを作成します。想定される出力:
... 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 ... Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: dkms_key_id = "key-xxxxxxxxxxxxxxxxxx"キーが作成された後、次の操作を実行できます:
キーの ID を照会する:
terraform output dkms_key_id想定される出力:
"key-xxxxxxxxxxxxxxxxxx"キー削除タスクをスケジュールする:
terraform destroy想定される出力:
... Plan: 0 to add, 0 to change, 1 to destroy. ... Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes ... Destroy complete! Resources: 1 destroyed.
Terraform を使用したシークレットの作成
作業ディレクトリを作成し、その作業ディレクトリに main.tf と variables.tf という名前の設定ファイルを作成します。
main.tf:このファイルは Terraform のメインファイルであり、デプロイするリソースを定義します。
# alicloud_kms_secret リソースの詳細については、https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/kms_secret をご参照ください。 resource "alicloud_kms_secret" "dkms_secret" { secret_name = "secret-simple" description = "from terraform" secret_data = "${var.ENV_SECRET_DATA}" version_id = "${var.version_id}" encryption_key_id = "${var.encryption_key_id}" dkms_instance_id = "${var.dkms_instance_id}" force_delete_without_recovery = true }variables.tf:このファイルには、main.tf に渡すことができる変数が含まれています。これらの変数を使用して、環境をカスタマイズできます。
# シークレットのバージョン。 variable "version_id" { default = "000000000001" } # export TF_VAR_ENV_SECRET_DATA=xxxxxxxxxx コマンドを実行してシークレットを設定します。 variable "ENV_SECRET_DATA" { default = "Secret data." #sensitive = true } variable "encryption_key_id" { default = "key-xxxxxxxxxxxxxxxxxx" } # ご利用の KMS インスタンスの ID。 variable "dkms_instance_id" { default = "kst-xxxxxxxxxxxxxxx" }
terraform initコマンドを実行して、Terraform ランタイム環境を初期化します。想定される出力:
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.183.0... - Installed hashicorp/alicloud v1.183.0 (signed by HashiCorp) 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. ╷ │ Warning: Additional provider information from registry │ │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud: │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers. ╵ 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.terraform planコマンドを実行して、リソースプランを生成します。想定される出力:
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: # alicloud_kms_secret.dkms_secret will be created + resource "alicloud_kms_secret" "dkms_secret" { ... } 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.terraform applyコマンドを実行して、シークレットを作成します。想定される出力:
... 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 ... Apply complete! Resources: 1 added, 0 changed, 0 destroyed.シークレットが作成された後、次のコマンドを実行してシークレットを削除できます:
terraform destroy想定される出力:
... Plan: 0 to add, 0 to change, 1 to destroy. ... Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes ... Destroy complete! Resources: 1 destroyed.