Terraform is an open source tool from HashiCorp that lets you preview, configure, and manage cloud infrastructure and resources. With Terraform, you can automatically create and update Alibaba Cloud infrastructure resources, and keep those resources under version management. Use Terraform to create the environment instances that connect your cloud resources to Application Real-Time Monitoring Service (ARMS), so that you can monitor the performance metrics of the resources in real time.
Prerequisites
Terraform v0.12.28 or later is installed. Run
terraform --versionto check the installed version. Install Terraform in one of the following ways:Cloud Shell — Use Terraform in Cloud Shell. Cloud Shell installs and configures Terraform and your Alibaba Cloud account information by default. No additional configuration is required. For instructions, see Quickly create resources by using Terraform.
Local machine — Install Terraform on your local machine. For instructions, see Install and configure Terraform locally.
Resource Orchestration Service provides managed Terraform capabilities, allowing you to create Terraform templates, define resources on Alibaba Cloud, AWS, or Azure, and configure resource parameters and dependencies. For more information, see Create a Terraform template and Create a Terraform stack.
Your Alibaba Cloud account information is configured. Provide the identity credentials in one of the following ways:
Environment variables — Add environment variables to store the identity credentials:
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"Configuration file — Specify the identity credentials in the
providerblock of the configuration file:provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-beijing" }
NoteTo improve the flexibility and security of permission management, create a Resource Access Management (RAM) user named Terraform, create an AccessKey for the RAM user, and grant permissions to the RAM user. For instructions, see Create a RAM user and Manage permissions for a RAM user.
Managed Service for Prometheus is activated.
Create an environment instance
ARMS supports environment instances for Container Service, ECS, and cloud services. The three environment types share the same Terraform workflow and differ only in the alicloud_arms_environment resource that you declare in the main.tf configuration file:
Environment type |
|
|
|
Container Service |
|
| The cluster ID of Container Service |
ECS |
|
| The VPC ID, for example, |
Cloud service |
|
| The region ID of the cloud service instance, for example, |
Identify the environment type that matches the cloud resources you want to monitor, and then follow the steps in the corresponding section.
Container Service environment instance
Create a working directory and a configuration file named
main.tfin it:# provider, use alicloud provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # The environment instance of Container Service. resource "alicloud_arms_environment" "my_ack-env1" { environment_name = "Container Service environment name 1" environment_type = "CS" environment_sub_type = "ACK" bind_resource_id = "The cluster ID of Container Service" }access_key,secret_key, andregionfields commented out.Initialize the Terraform runtime environment:
terraform initInitializing 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.Preview the resources that Terraform plans to create:
terraform planRefreshing 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. ...Create the Container Service environment instance:
terraform applyEnter a value, enteryes. The following output is returned:... 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.Log on to the ARMS console. On the Integration Management page, verify that the Container Service environment instance appears in the environment list.
ECS environment instance
Create a working directory and a configuration file named
main.tfin it:# provider, use alicloud provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # The ECS environment instance. resource "alicloud_arms_environment" "my_ecs-env1" { environment_name = "ECS environment name 1" environment_type = "ECS" environment_sub_type = "ECS" bind_resource_id = "The vpcId, for example, vpc-bp1jxxx" }access_key,secret_key, andregionfields commented out.Initialize the Terraform runtime environment:
terraform initInitializing 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.Preview the resources that Terraform plans to create:
terraform planRefreshing 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. ...Create the ECS environment instance:
terraform applyEnter a value, enteryes. The following output is returned:... 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.Log on to the ARMS console. On the Integration Management page, verify that the ECS environment instance appears in the environment list.
Cloud service environment instance
Create a working directory and a configuration file named
main.tfin it:# provider, use alicloud provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # The cloud service environment instance. resource "alicloud_arms_environment" "my_cloud-env1" { environment_name = "Cloud service environment name 1" environment_type = "Cloud" environment_sub_type = "Cloud" bind_resource_id = "The regionId of the cloud service instance, for example, cn-hangzhou" }access_key,secret_key, andregionfields commented out.Initialize the Terraform runtime environment:
terraform initInitializing 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.Preview the resources that Terraform plans to create:
terraform planRefreshing 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. ...Create the cloud service environment instance:
terraform applyEnter a value, enteryes. The following output is returned:... 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.Log on to the ARMS console. On the Integration Management page, verify that the cloud service environment instance appears in the environment list.
Delete an environment instance
The following steps apply to all three environment types: Container Service, ECS, and cloud service.
terraform destroy destroys all the infrastructure that Terraform manages, and the operation cannot be undone.
Delete the environment instance that you created by using Terraform:
terraform destroyEnter a value, enteryes. The following output is returned:... 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.Log on to the ARMS console. On the Integration Management page, verify that the environment instance is deleted from the environment list.