Terraformを使用して、アプリケーションアクセスポイント (AAP) を作成および管理できます。 このトピックでは、AAPを作成する方法について説明します。
概要
自己管理アプリケーションが暗号化操作を実行したり、シークレットを取得したりする前に、アプリケーションは必要なAAPのクライアントキーを使用してkey Management Service (KMS) インスタンスにアクセスする必要があります。
Alibaba Cloudサービスのサーバー側暗号化にKMSインスタンスのキーを使用する場合、またはKMS SDKを呼び出してシークレットを使用する場合、AAPを作成する必要はありません。 KMSインスタンスSDKを呼び出してKMSインスタンスでキーまたはシークレットを使用する場合は、AAPを作成する必要があります。
このトピックのサンプルコードは、数回クリックするだけで実行できます。 詳細については、「Terraform Explorer」をご参照ください。
前提条件
Alibaba Cloudアカウントには、このアカウントに属するすべてのリソースに対する完全な権限があります。 Alibaba Cloudアカウントの資格情報が漏洩すると、セキュリティリスクが発生する可能性があります。 Resource Access Management (RAM) ユーザーを使用し、RAMユーザーのAccessKeyペアを作成することを推奨します。 詳細については、「RAMユーザーの作成」および「AccessKeyペアの作成」をご参照ください。
次のコードは、RAMユーザーに権限を付与する方法の例を示しています。 AliyunKMSFullAccessポリシーがRAMユーザーにアタッチされていることを確認します。 詳細については、「RAM ユーザーへの権限の付与」をご参照ください。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "kms:*" ], "Resource": [ "*" ], "Condition": {} } ] }Terraformのランタイム環境は、次のいずれかの方法を使用して準備されます。
Terraform ExplorerでTerraformを使用する: Alibaba Cloudは、Terraformのオンラインランタイム環境を提供します。 Terraform Explorer環境にログインして、TerraformをインストールしなくてもTerraformを使用できます。 この方法は、Terraformを高速かつ便利な方法で追加費用なしで使用およびデバッグするシナリオに適しています。
Cloud ShellでTerraformを使用する: TerraformはCloud Shellにプリインストールされており、ID認証情報が設定されています。 Cloud ShellでTerraformコマンドを実行できます。 この方法は、Terraformを高速で便利な方法で低コストで使用およびデバッグするシナリオに適しています。
オンプレミスのマシンにTerraformをインストールして構成する: この方法は、ネットワーク接続が不安定なシナリオやカスタム開発環境が必要なシナリオに適しています。
重要Terraform 0.12.28以降をインストールする必要があります。
terraform -- versionコマンドを実行して、Terraformバージョンを照会できます。
必要なリソース
alicloud_kms_network_rule: ネットワークルールを作成します。
alicloud_kms_application_access_point: AAPを作成します。
alicloud_kms_client_key: クライアントキーを作成します。
alicloud_kms_policy: アクセス制御ポリシーを作成します。
TerraformでAAPを作成する
次の例では、AAPはKMSインスタンスに作成されます。
作業ディレクトリと
main.tfという名前の設定ファイルを作業ディレクトリに作成します。 main.tfはTerraformのメインファイルで、デプロイするリソースを定義します。 KMSインスタンスが事前に作成されていることを確認してください。variable "region" { default = "cn-heyuan" } provider "alicloud" { region = var.region } variable "instance_name" { default = "tf-kms-vpc-172-16" } variable "instance_type" { default = "ecs.n1.tiny" } # Use data sources to obtain information about available zones. You can create resources only in specified zones. data "alicloud_zones" "default" { available_disk_category = "cloud_efficiency" available_resource_creation = "VSwitch" available_instance_type = var.instance_type } # Create a virtual private cloud (VPC). resource "alicloud_vpc" "vpc" { vpc_name = var.instance_name cidr_block = "192.168.0.0/16" } # Create a vSwitch whose CIDR block is 192.168.10.0/24. resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "192.168.10.0/24" zone_id = data.alicloud_zones.default.zones.0.id vswitch_name = "terraform-example-1" } # Create another vSwitch whose CIDR block is 192.168.20.0/24. resource "alicloud_vswitch" "vsw1" { vpc_id = alicloud_vpc.vpc.id cidr_block = "192.168.20.0/24" zone_id = data.alicloud_zones.default.zones.0.id vswitch_name = "terraform-example-2" } # Create an instance of the software key management type and configure network parameters to enable the instance. resource "alicloud_kms_instance" "default" { # The instance of the software key management type. product_version = "3" vpc_id = alicloud_vpc.vpc.id # Specify zones for the KMS instance by using the obtained zone IDs. zone_ids = [ "cn-heyuan-a", "cn-heyuan-b", ] # The vSwitch IDs. vswitch_ids = [ alicloud_vswitch.vsw.id,alicloud_vswitch.vsw1.id ] # The computing performance, number of keys, number of secrets, and access management quota. vpc_num = "1" key_num = "1000" secret_num = "100" spec = "1000" # Associate a different VPC with the instance. This parameter is optional. # If the two VPCs belong to different Alibaba Cloud accounts, you must first share the vSwitch in the second VPC with the first VPC. #bind_vpcs { #vpc_id = "vpc-j6cy0l32yz9ttxfy6****" #vswitch_id = "vsw-j6cv7rd1nz8x13ram****" #region_id = "cn-shanghai" #vpc_owner_id = "119285303511****" #} #bind_vpcs { #vpc_id = "vpc-j6cy0l32yz9ttd7g3****" #vswitch_id = "vsw-3h4yrd1nz8x13ram****" #region_id = "cn-shanghai" #vpc_owner_id = "119285303511****" #} } # Save the certificate authority (CA) certificate of the instance to a local file. resource "local_file" "ca_certificate_chain_pem" { content = alicloud_kms_instance.default.ca_certificate_chain_pem filename = "ca.pem" }AAPを作成する:
# Create a network rule. resource "alicloud_kms_network_rule" "network_rule_example" { # The name of the network rule. network_rule_name = "sample_network_rule" # The description of the network rule. description = "description_test_module" # The allowed range of source private IP addresses. source_private_ip = ["172.16.0.0/12"] } # Create an access control policy. resource "alicloud_kms_policy" "policy_example" { # The name of the policy. policy_name = "sample_policy" # The description of the policy. description = "description_test_module" # The list of specified permissions, which include the permissions to use keys and the permissions to access keys. permissions = ["RbacPermission/Template/CryptoServiceKeyUser", "RbacPermission/Template/CryptoServiceSecretUser"] # The list of resources, which indicate all keys and secrets. resources = ["key/*", "secret/*"] # The ID of the KMS instance. kms_instance_id = alicloud_kms_instance.default.id # The access control rules in the JSON format. The network rule specified in the preceding section is used. access_control_rules = <<EOF { "NetworkRules":[ "alicloud_kms_network_rule.network_rule_example.network_rule_name" ] } EOF } # Create an AAP. resource "alicloud_kms_application_access_point" "application_access_point_example" { # The name of the AAP. application_access_point_name = "sample_aap" # The list of associated policies. The access control policy specified in the preceding section is used. policies = [alicloud_kms_policy.policy_example.policy_name] # The description of the AAP. description = "aap_description" } # Create a client key. resource "alicloud_kms_client_key" "client_key" { # The name of the specified AAP. aap_name = alicloud_kms_application_access_point.application_access_point_example.application_access_point_name # The password of the client key. Specify a custom password. password = "testPassword@" # The start time of the validity period for the client key. not_before = "2023-09-01T14:11:22Z" not_after = "2032-09-01T14:11:22Z" # The path of the local file that stores the client key. private_key_data_file = "./client_key.json" }重要AAPの作成後、ローカルファイルパスを使用してクライアントキーを取得し、クライアントキーを安全な場所に保存する必要があります。
Terraformの機密入力を使用して、パスワードパラメーターを設定できます。
次のコマンドを実行して、
Terraformのランタイム環境を初期化します。terraform init次の情報が返されると、Terraformは初期化されます。
Initializing the backend... Initializing provider plugins... - Reusing previous version of hashicorp/alicloud from the dependency lock file - Using previously-installed hashicorp/alicloud v1.231.0 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次のコマンドを実行してAAPを作成します。
terraform apply実行中に、プロンプトに従って
yesと入力し、enterキーを押します。 コマンドが実行されるまで待ちます。 次の情報が返されると、AAPが作成されます。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 ... alicloud_kms_network_rule.network_rule_example: Creating... alicloud_kms_policy.policy_example: Creating... alicloud_kms_network_rule.network_rule_example: Creation complete after 0s [id=sample_network_***] alicloud_kms_policy.policy_example: Creation complete after 0s [id=sample_pol***] alicloud_kms_application_access_point.application_access_point_example: Creating... alicloud_kms_application_access_point.application_access_point_example: Creation complete after 0s [id=sample_***] alicloud_kms_client_key.client_key: Creation complete after 0s [id=KAAP.5093ea57-0b84-4455-a8e9-7679bdc****] ... Apply complete! Resources: 4 added, 0 changed, 0 destroyed.結果を確認します。
terraform showコマンドを実行します。
次のコマンドを実行して、Terraformを使用して作成されたリソースを照会できます。
terraform show
KMSコンソールにログインする
KMSコンソールにログインして、作成したAAPを表示します。

リソースのリリース
Terraformを使用して作成または管理された上記のリソースが不要になった場合は、次のコマンドを実行してリソースを解放します。 terraform destroyの詳細については、「一般的なコマンド」をご参照ください。
terraform destroy完全なサンプルコード
数回クリックするだけでサンプルコードを実行できます。 詳細については、「Terraform Explorer」をご参照ください。