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の詳細については、「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と変数. tf作業ディレクトリにあります。
main.tf: このファイルはTerraformのメインファイルで、デプロイするリソースを定義します。
# For more information about the alicloud_kms_key resource, visit 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に転送できる変数が含まれています。 これらの変数は、環境のカスタマイズに役立ちます。
# The description of the key. variable "description" { default = "the new dkms key" } # The scheduled deletion period. After the period elapses, key is deleted. During the scheduled deletion period, the key is in the Pending Deletion state. After the scheduled deletion period elapses, you cannot cancel the key deletion task. variable "pending_window_in_days" { default = "7" } # For an instance of the software key management type, set this parameter to SOFTWARE. # For an instance of the hardware key management type, set this parameter to HSM. variable "protection_level" { default = "SOFTWARE" } # The ID of your KMS instance. 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と変数. tf作業ディレクトリにあります。
main.tf: このファイルはTerraformのメインファイルで、デプロイするリソースを定義します。
# For more information about the alicloud_kms_secret resource, visit 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に転送できる変数が含まれています。 これらの変数は、環境のカスタマイズに役立ちます。
# The version of the secret. variable "version_id" { default = "000000000001" } # Run the export TF_VAR_ENV_SECRET_DATA=xxxxxxxxxx command to configure the secret. variable "ENV_SECRET_DATA" { default = "Secret data." #sensitive = true } variable "encryption_key_id" { default = "key-xxxxxxxxxxxxxxxxxx" } # The ID of your KMS instance. 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.