Terraform は、HashiCorp によって提供されるオープンソースツールです。 Terraform を使用すると、クラウドベースのインフラストラクチャとリソースを安全かつ効率的にプレビュー、構成、および管理できます。 Terraform を使用して、Alibaba Cloud にデプロイされたインフラストラクチャリソースを自動的に作成および更新し、要件に基づいてバージョンを管理できます。 このトピックでは、Terraform を使用して Prometheus インスタンスを管理する方法について説明します。
前提条件
Terraform がインストールされていること。
デフォルトでは、Cloud Shell には Terraform がプリインストールされており、アカウント情報が構成されています。 構成を変更する必要はありません。
Cloud Shell を使用しない場合は、Terraform を直接インストールできます。 詳細については、「Terraform のインストールと構成」をご参照ください。
説明Terraform V0.12.28 以後をインストールする必要があります。
terraform --versionコマンドを実行して、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 アカウント情報が構成されていること。 次のいずれかの方法を使用して、Alibaba Cloud アカウント情報を構成できます。
説明権限管理の柔軟性とセキュリティを向上させるために、Terraform という名前の RAM ユーザーを作成することをお勧めします。 次に、RAM ユーザーの AccessKey ペアを作成し、RAM ユーザーに権限を付与します。 詳細については、「RAM ユーザーの作成」および「RAM ユーザーへの権限の付与」をご参照ください。
方法 1: 認証情報を格納する環境変数を追加します。
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"説明export ALICLOUD_REGIONパラメーターの値は、ビジネス要件に基づいて指定してください。方法 2: 構成ファイルの provider セクションで ID 情報を指定します。
provider "alicloud" { access_key = "************" //アクセスキー secret_key = "************" //シークレットキー region = "cn-beijing" //リージョン }説明export ALICLOUD_REGIONパラメーターの値は、ビジネス要件に基づいて指定してください。
Prometheus インスタンスを作成する
Container Service for Kubernetes (ACK) クラスタを監視する Prometheus インスタンスを作成する
作業ディレクトリと、そのディレクトリに main.tf という名前のファイルを作成します。
#provider, use alicloud //プロバイダー、alicloudを使用 provider "alicloud" { #access_key = "************" //アクセスキー #secret_key = "************" //シークレットキー #region = "cn-beijing" //リージョン } # The information about the Prometheus instance for Container Service. //コンテナサービスのPrometheusインスタンスに関する情報 resource "alicloud_arms_prometheus" "my_ack1" { cluster_id = "The ID of the ACK cluster." //ACKクラスタのID cluster_type = "aliyun-cs" grafana_instance_id = "free or the ID of the Grafana workspace." //freeまたはGrafanaワークスペースの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. ...次のコマンドを実行して、Prometheus インスタンスを作成します。
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_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.yesが返された場合、Prometheus インスタンスが作成されます。 Managed Service for Prometheus コンソールにログインし、[インスタンス] ページで Prometheus インスタンスを表示できます。Prometheus インスタンスを表示するには、次の手順を実行します。ARMS コンソール にログインします。
左側のナビゲーションペインで、 を選択します。
[インスタンス] ページで、コンテナーサービス用に作成されたPrometheusインスタンスを表示します。
ACK Serverless クラスタを監視する Prometheus インスタンスを作成する
作業ディレクトリと、そのディレクトリに main.tf という名前のファイルを作成します。
#provider, use alicloud //プロバイダー、alicloudを使用 provider "alicloud" { #access_key = "************" //アクセスキー #secret_key = "************" //シークレットキー #region = "cn-beijing" //リージョン } # The information about the Prometheus instance for Container Service. //コンテナサービスのPrometheusインスタンスに関する情報 resource "alicloud_arms_prometheus" "my_ack serverless1" { cluster_id = "The ID of the ACK Serverless cluster." //ACK ServerlessクラスタのID cluster_type = "aliyun-cs" grafana_instance_id = "free or the ID of the Grafana workspace." //freeまたはGrafanaワークスペースのID vpc_id = "The ID of the VPC where the ACK Serverless cluster resides." //ACK Serverlessクラスタが存在するVPCのID vswitch_id = "The ID of the vSwitch corresponding to the ACK Serverless cluster." //ACK Serverlessクラスタに対応するvSwitchのID security_group_id = "The ID of the security group corresponding to the ACK Serverless cluster." //ACK Serverlessクラスタに対応するセキュリティグループの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. ...次のコマンドを実行して、Prometheus インスタンスを作成します。
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_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.yesが返された場合、Prometheus インスタンスが作成されます。 Managed Service for Prometheus コンソールにログインし、[インスタンス] ページで Prometheus インスタンスを表示できます。Prometheus インスタンスを表示するには、次の手順を実行します。ARMS コンソール にログインします。
左側のナビゲーションペインで、 を選択します。
インスタンス ページで、作成された Container Service 用の Prometheus インスタンスを表示します。
ECS 用の Prometheus インスタンスを作成する
作業ディレクトリと、そのディレクトリに main.tf という名前のファイルを作成します。
#provider, use alicloud //プロバイダー、alicloudを使用 provider "alicloud" { #access_key = "************" //アクセスキー #secret_key = "************" //シークレットキー #region = "cn-beijing" //リージョン } # The information about the Prometheus instance for ECS. //ECSのPrometheusインスタンスに関する情報 resource "alicloud_arms_prometheus" "my_vpc1" { cluster_type = "ecs" cluster_name = "The name of the Prometheus instance." //Prometheusインスタンスの名前。フォーマット:name-vpcId。例:my_vpc1-vpc-xxxx.ss。 grafana_instance_id = "free or the ID of the Grafana workspace." //freeまたはGrafanaワークスペースのID vpc_id = "The ID of the VPC where the ECS instance resides." //ECSインスタンスが存在するVPCのID vswitch_id = "The ID of the vSwitch corresponding to the ECS instance." //ECSインスタンスに対応するvSwitchのID security_group_id = "The ID of the security group corresponding to the ECS instance." //ECSインスタンスに対応するセキュリティグループの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. ...次のコマンドを実行して、Prometheus インスタンスを作成します。
terraform apply想定される出力:
... Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to confirm. Enter a value: yes ... alicloud_arms_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.yesが返された場合、Prometheus インスタンスが作成されます。Managed Service for Prometheus コンソールにログインし、[インスタンス] ページで Prometheus インスタンスを表示できます。Prometheus インスタンスを表示するには、次の手順を実行します。ARMS コンソール にログインします。
左側のナビゲーションペインで、 を選択します。
インスタンス ページで、作成された ECS 用の Prometheus インスタンスを表示します。
汎用 Prometheus インスタンスを作成する
作業ディレクトリと、そのディレクトリに main.tf という名前のファイルを作成します。
#provider, use alicloud //プロバイダー、alicloudを使用 provider "alicloud" { #access_key = "************" //アクセスキー #secret_key = "************" //シークレットキー #region = "cn-beijing" //リージョン } # The information about the Prometheus instance for Remote Write. //リモートライトのPrometheusインスタンスに関する情報 resource "alicloud_arms_prometheus" "my_rw1" { cluster_name = "The name of the Prometheus instance." //Prometheusインスタンスの名前 cluster_type = "remote-write" grafana_instance_id = "free or the ID of the Grafana workspace." //freeまたはGrafanaワークスペースの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. ...次のコマンドを実行して、Prometheus インスタンスを作成します。
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_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 4 added, 0 changed, 0 destroyed.yesが返された場合、Prometheus インスタンスが作成されます。Managed Service for Prometheus コンソールにログインし、[インスタンス] ページで Prometheus インスタンスを表示できます。Prometheus インスタンスを表示するには、次の手順を実行します。ARMS コンソール にログインします。
左側のナビゲーションペインで、 を選択します。
インスタンス ページで、作成された汎用 Prometheus インスタンスを表示します。
グローバル集約インスタンスを作成する
作業ディレクトリと、そのディレクトリに main.tf という名前のファイルを作成します。
#provider, use alicloud //プロバイダー、alicloudを使用 provider "alicloud" { #access_key = "************" //アクセスキー #secret_key = "************" //シークレットキー #region = "cn-beijing" //リージョン } # The information about the global aggregation instance. //グローバル集約インスタンスの情報 resource "alicloud_arms_prometheus" "my_global1" { cluster_name = "The name of the Prometheus instance." //Prometheusインスタンスの名前 cluster_type = "global-view" grafana_instance_id = "free or the ID of the Grafana workspace." //freeまたはGrafanaワークスペースのID subClustersJson = jsonencode( #"The Prometheus instances whose data you want to aggregate. The value must be a JSON string." //集約するPrometheusインスタンス。値はJSON文字列である必要があります。 [ { "headers": {}, "regionId": "The ID of the region. Example: cn-hangzhou.", //リージョンのID。例:cn-hangzhou。 "sourceType": "AlibabaPrometheus", "extras": {}, "clusterId": "The ID of the Prometheus instance.", //PrometheusインスタンスのID "sourceName": "The name of the Prometheus instance.", //Prometheusインスタンスの名前 "dataSource": "", "userId": "The user ID." //ユーザーID }, { "headers": {}, "regionId": "The ID of the region. Example: cn-hangzhou.", //リージョンのID。例:cn-hangzhou。 "sourceType": "AlibabaPrometheus", "extras": {}, "clusterId": "The ID of the Prometheus instance.", //PrometheusインスタンスのID "sourceName": "The name of the Prometheus instance.", //Prometheusインスタンスの名前 "dataSource": "", "userId": "The user ID." //ユーザー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. ...次のコマンドを実行して、Prometheus インスタンスを作成します。
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_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.yesが返された場合、Prometheus インスタンスが作成されます。Managed Service for Prometheus コンソールにログインし、[インスタンス] ページで Prometheus インスタンスを表示できます。Prometheus インスタンスを表示するには、次の手順を実行します。ARMS コンソール にログインします。
左側のナビゲーションペインで、 を選択します。
インスタンス ページで、作成されたグローバル集約インスタンスを表示します。
Prometheus インスタンスを削除する
手順
次のコマンドを実行して、Terraform を使用して作成された Prometheus インスタンスを削除できます。
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.結果を確認する
ARMS コンソール にログインします。
左側のナビゲーションペインで、 を選択します。
インスタンス ページに Prometheus インスタンスが存在しない場合、Prometheus インスタンスは削除されています。