HashiCorp によって提供されるオープンソース ツールである Terraform を使用すると、クラウド インフラストラクチャとリソースを安全かつ効率的にプレビュー、構成、および管理できます。 Terraform を使用して、Alibaba Cloud インフラストラクチャとリソースを自動的に作成および更新し、要件に基づいてバージョンを管理できます。 Terraform を使用して環境インスタンスを作成できます。 この方法で、クラウド リソースを Application Real-time Monitoring Service (ARMS) に接続し、クラウド リソースのパフォーマンス メトリックをリアルタイムで監視できます。 これにより、O&M チームは問題を解決し、システム パフォーマンスをできるだけ早く最適化できます。
前提条件
Terraform V0.12.28 以降がインストールされていること。
terraform --versionコマンドを実行して、Terraform のバージョンを照会できます。方法 1: Cloud Shell で Terraform を使用する。 詳細については、「Cloud Shell での Terraform の使用」をご参照ください。 デフォルトでは、Cloud Shell には Terraform がプリインストールされており、アカウント情報が構成されています。 構成を変更する必要はありません。
方法 2: ローカル コンピューターに Terraform をインストールする。 詳細については、「ローカル PC での Terraform のインストールと構成」をご参照ください。
説明Resource Orchestration Service (ROS) は Alibaba Cloud が提供するネイティブの Infrastructure-as-Code (IaC) サービスですが、Terraform テンプレートの統合もサポートしています。 ROS で Terraform を使用することにより、Alibaba Cloud、Amazon Web Services (AWS)、または Microsoft Azure のリソースを定義および管理し、リソース パラメーターを指定し、リソースの依存関係を構成できます。 詳細については、「Terraform テンプレートの作成」および「Terraform スタックの作成」をご参照ください。
Alibaba Cloud アカウント情報が構成されていること。
方法 1: 認証情報を格納する環境変数を追加する。
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"方法 2: 構成ファイルの
providerセクションで ID 情報を指定する。provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-beijing" }
説明権限管理の柔軟性とセキュリティを向上させるために、Terraform という名前の Resource Access Management (RAM) ユーザーを作成することをお勧めします。 次に、RAM ユーザーの AccessKey ペアを作成し、RAM ユーザーに権限を付与します。 詳細については、「RAM ユーザーの作成」および「RAM ユーザーへの権限の付与」をご参照ください。
Managed Service for Prometheus がアクティブ化されていること。
環境インスタンスを作成する
ACK 環境インスタンスを作成する
作業ディレクトリと、そのディレクトリに
main.tfという名前のファイルを作成します。# provider, use alicloud // プロバイダー、alicloud を使用 provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # Configurations of the ACK environment instance. // ACK 環境インスタンスの構成 resource "alicloud_arms_environment" "my_ack-env1" { environment_name = "The name of the environment instance. Example: my_ack-env1." // 環境インスタンスの名前。例: my_ack-env1 environment_type = "CS" environment_sub_type = "ACK" bind_resource_id = "The ID of the Container Service for Kubernetes (ACK) cluster." // Container Service for Kubernetes (ACK) クラスタの ID }次のコマンドを実行して、Terraform のランタイム環境を初期化します。
terraform init予想される出力:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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予想される出力:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...次のコマンドを実行して、環境インスタンスを作成します。
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 ... alicloud_arms_environment: Creation complete after 8m26s [id=************] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Enter a valueが返された場合は、yesと入力します。ARMS コンソールにログインします。 [統合管理] ページで、ACK 環境インスタンスが作成されていることを確認します。
ECS 環境インスタンスを作成する
作業ディレクトリと、そのディレクトリに
main.tfという名前のファイルを作成します。#provider, use alicloud // プロバイダー、alicloud を使用 provider "alicloud" { #access_key = "************" #secret_key = "************" #region = "cn-beijing" } # Configurations of the ECS environment instance. // ECS 環境インスタンスの構成 resource "alicloud_arms_environment" "my_ecs-env1" { environment_name = "The name of the environment instance. Example: my_ecs-env1." // 環境インスタンスの名前。例: my_ecs-env1 environment_type = "ECS" environment_sub_type = "ECS" bind_resource_id = "The ID of the virtual private cloud (VPC). Example: vpc-bp1jxxx." // 仮想プライベートクラウド (VPC) の ID。例: vpc-bp1jxxx }次のコマンドを実行して、Terraform のランタイム環境を初期化します。
terraform init予想される出力:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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予想される出力:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...次のコマンドを実行して、環境インスタンスを作成します。
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 ... alicloud_arms_environment: Creation complete after 8m26s [id=************] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.Enter a valueが返された場合は、yesと入力します。ARMS コンソールにログインします。 [統合管理] ページで、ECS 環境インスタンスが作成されていることを確認します。
クラウドサービス環境インスタンスを作成する
作業ディレクトリと、そのディレクトリに
main.tfという名前のファイルを作成します。#provider, use alicloud // プロバイダー、alicloud を使用 provider "alicloud" { #access_key = "************" #secret_key = "************" #region = "cn-beijing" } # Configurations of the cloud-service environment instance. // クラウドサービス環境インスタンスの構成 resource "alicloud_arms_environment" "my_cloud-env1" { environment_name = "The name of the environment instance. Example: my_cloud-env1." // 環境インスタンスの名前。例: my_cloud-env1 environment_type = "Cloud" environment_sub_type = "Cloud" bind_resource_id = "The ID of the region where the cloud service is deployed. Example: cn-hangzhou." // クラウドサービスがデプロイされているリージョンの ID。例: cn-hangzhou }次のコマンドを実行して、Terraform のランタイム環境を初期化します。
terraform init予想される出力:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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予想される出力:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...次のコマンドを実行して、環境インスタンスを作成します。
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 ... alicloud_arms_environment: Creation complete after 8m26s [id=************] Apply complete! Resources: 4 added, 0 changed, 0 destroyed.Enter a valueが返された場合は、yesと入力します。ARMS コンソールにログインします。 [統合管理] ページで、クラウドサービス環境インスタンスが作成されていることを確認します。
環境インスタンスを削除する
次のコマンドを実行して、Terraform を使用して作成された環境インスタンスを削除します。
terraform 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.Enter a valueが返された場合は、yesと入力します。ARMS コンソールにログインします。 [統合管理] ページで、環境インスタンスが削除されていることを確認します。