Terraform is an open source tool that provisions and manages cloud infrastructure securely and efficiently. You can use Terraform to manage ENS (Edge Node Service) resources. This tutorial shows you how to create an ENS EIP resource by using Terraform.
The sample code supports one-click deployment. Run in Terraform Explorer to deploy the sample code online without installing Terraform.
Prerequisites
Prepare the following items before you run the sample code. The runtime environment that you choose determines whether you configure credentials yourself:
RAM user and AccessKey pair — An Alibaba Cloud account has permissions on all resources, and a leak of its credentials causes significant risks. (Recommended) Use a Resource Access Management (RAM) user and create an AccessKey pair for the RAM user. For more information, seeCreate a RAM userandCreate an AccessKey.
ENS permissions — Grant the RAM user the AliyunENSFullAccess permission, which allows the RAM user to manage ENS resources. For more information, seeManage RAM user permissions. The following policy document describes the permissions that the sample code requires:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": "ens:*", "Resource": "*" } ] }Terraform runtime environment — Prepare a Terraform runtime environment in any of the following ways:
(Recommended) Use Terraform inTerraform Explorer: Alibaba Cloud provides an online Terraform runtime environment. You do not need to install Terraform. Log on and use Terraform online. This option suits scenarios in which you want to try out and debug Terraform quickly at no cost. The one-click deployment link in this topic opens the sample code in this environment.
Create resources with Terraform: Alibaba Cloud Cloud Shell comes with Terraform components preinstalled and credentials preconfigured, so you can run Terraform commands directly in Cloud Shell. This option suits scenarios in which you want quick, low-cost access to Terraform.
Install and configure Terraform: This option suits scenarios in which network connectivity is poor or you need a customized development environment.
Regardless of the option that you choose, make sure that the Terraform version is v0.12.28 or later. To check the installed version, run the
terraform --versioncommand.
Some resources in this example incur fees. Release the resources as soon as you no longer need them. For instructions, see the Clean up resources section of this topic.
Resources used
alicloud_ens_eip: creates an ENS EIP resource.
Integrate resources by using Terraform
In this section, you define the ENS EIP resource in a Terraform configuration file and then apply the configuration.
Create a working directory, and then create a configuration file named
main.tfin it. main.tf is the main Terraform file, which defines the resources to deploy.variable "name" { default = "terraform-example" } # Create an ENS EIP resource. resource "alicloud_ens_eip" "default" { # (Optional) The description of the EIP. description = "EipDescription_autotest" # (Optional, computed) The peak bandwidth of the EIP. Default value: 5. Valid values: 5 to 10000. bandwidth = "5" # (Optional, forces new resource) The Internet service provider. isp = "cmcc" # (Required, forces new resource) The billing method of the EIP. Valid value: PayAsYouGo (pay-as-you-go). payment_type = "PayAsYouGo" # (Required, forces new resource) The ID of the ENS edge zone. ens_region_id = "cn-chenzhou-telecom_unicom_cmcc" # (Optional) The name of the EIP. eip_name = var.name # (Required, forces new resource) Valid value: 95BandwidthByMonth. internet_charge_type = "95BandwidthByMonth" }Run the following command to initialize the Terraform runtime environment:
terraform initInitializing 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.Run the following command to create an execution plan and preview the changes:
terraform planRun the following command to create the ENS EIP resource:
terraform applyyesand press the Enter key. Wait for the command to finish. The following output indicates that the ENS EIP resource is created.
Verify the result
Confirm that the ENS EIP resource exists in either of the following ways.
Run the terraform show command
Run the following command to query the details of the resources that Terraform created:
terraform showLog on to the ENS console
After the resource is created, log on to the ENS console to view the ENS EIP resource that you created.

Clean up resources
When you no longer need the resources that you created or manage by using Terraform, run the following command to release them:
terraform destroyFor more information about terraform destroy, seeCommon commands.
Complete example
The following configuration is the complete sample code for creating an ENS EIP resource. Run in Terraform Explorer to deploy it without preparing a local runtime environment.
Sample code
variable "name" {
default = "terraform-example"
}
# Create an ENS EIP resource.
resource "alicloud_ens_eip" "default" {
# (Optional) The description of the EIP.
description = "EipDescription_autotest"
# (Optional, computed) The peak bandwidth of the EIP. Default value: 5. Valid values: 5 to 10000.
bandwidth = "5"
# (Optional, forces new resource) The Internet service provider.
isp = "cmcc"
# (Required, forces new resource) The billing method of the EIP. Valid value: PayAsYouGo (pay-as-you-go).
payment_type = "PayAsYouGo"
# (Required, forces new resource) The ID of the ENS edge zone.
ens_region_id = "cn-chenzhou-telecom_unicom_cmcc"
# (Optional) The name of the EIP.
eip_name = var.name
# (Required, forces new resource) Valid value: 95BandwidthByMonth.
internet_charge_type = "95BandwidthByMonth"
}For more examples, go to More complete examples and open the folder of the corresponding service.