Community Blog Deploy Alibaba Cloud Resources by Using Terraform

Deploy Alibaba Cloud Resources by Using Terraform

In this blog, you can follow detailed steps to learn how to deploy Alibaba Cloud resources by using Terraform.

By Anthony Merrington, Technical Support Engineer at Alibaba Cloud

Terraform is a powerful tool in your orchestration armory, and allows you to create, update, and control versioning of cloud infrastructure.

Terraform provides benefits that include automated infrastructure management, lower deployment costs, and reduced provisioning time. In particular, Terraform comes into its own when it comes to managing hybrid cloud or multi-cloud infrastructure, since it enables developers to use the same tool and configuration file to simultaneously manage the resources of different cloud providers.

In this blog, we'll go through the detailed steps required to deploy cloud infrastructure by using Terraform, taking Alibaba Cloud services as an example. The higher level steps are as follows:

  1. Plan the resources you want to deploy
  2. Inspect the Terraform configurations
  3. Apply the configurations to deploy the services
  4. Verify the infrastructure is deployed in the Alibaba Cloud console

Note that the following steps are taken from the related Best Practice document found on the Alibaba Cloud website. This tutorial walks you step-by-step through the procedure to set up Terraform and then use it to deploy cloud infrastructure.

Before You Begin

Before you get started, you need to make sure that Terraform is downloaded and installed on your system. You can refer to the previous blog article to learn how to do this.

If you're relatively new to Terraform, I also recommend that you read the blog article detailing the benefits of Terraform so that you have a better idea of what it can do for your business.

Step 1: Plan the Resources You Want to Deploy

The first step is to decide which resources you need to deploy, and what the service configurations should be. In this example, we will create a system that runs on multiple Alibaba Cloud services. View the following tables for the configuration details:





NAT Gateway:


Elastic IP:


Step 2: Inspect the Terraform Configurations

The next step is to create configuration files or check that your existing configuration files are correct. In this example, we use a configuration file that is already created in a demo kit

1. To download the demo kit, first install Git on your computer. For more information, see Git documentation.

2. Run the following command to download the demo kit resources:

git clone https://code.aliyun.com/best-practice/017-int.git

3. Check the configuration files. The configuration files provided in the demo kit should include the following files:



4. Inspect the configuration file code.

The demo kit covers basic Terraform configurations that you can use to provision Alibaba Cloud infrastructure. You can get started with Terraform syntax for Alibaba Cloud by reviewing the code provided in the demo kit.

The following code is the part for creating SNAT entries:


The following code is the part for configuring security groups:


The following code is the part for creating ECS instances:


Step 3: Apply the Configurations to Deploy the Services

1. At a command prompt, switch the working directory to the demo kit folder.

2. Run the following command to add environment variables for the AccessKey ID and AccessKey secret:

export ALICLOUD_ACCESS_KEY="LTAI4*************gZ"
export ALICLOUD_SECRET_KEY="GQm*****************f5J9nNJw

Note: Replace the variable values with the AccessKey ID and the AccessKey secret of the corresponding RAM user. We assume that you already have a RAM user with the required permissions to create the corresponding resources.

3. Run the following command to initialize the Terraform configuration files in the current working directory:

terraform init


Note: The command result indicates that the Alibaba Cloud provider is version 1.33.

4. Run the following command to create an execution plan:

terraform plan



Note: The command result lists the resources that are to be created or updated.

5. Run the following command to apply the planned configurations:

terraform apply    

Enter yes when you are prompted to confirm certain actions.



Step 4: Verify the Infrastructure Is Deployed in the Alibaba Cloud Console

1. Log on to the ECS console.

2. On the left-side navigation pane, click Instances & Images > Instances.

3. Select Malaysia (Kuala Lumpur) from the region drop-down list.

4. Verify that the planned ECS instances have been created.


5. Log on to the VPC console.

6. On the left-side navigation pane, click VPCs.

7. Verify that the planned VPCs have been created.


8. On the left-side navigation pane, click VSwitches.

9. Verify that the planned VSwitches have been created.


10. On the left-side navigation pane, click Elastic IP Addresses > Elastic IP Addresses.

11. Verify that the Elastic IP addresses have been configured as expected:


12. On the left-side navigation pane, click NAT Gateways.

13. Verify that the NAT gateway has been created. Click the ID of the NAT gateway.


14. On the left-side navigation pane, click SNAT Table.

15. Verify that the SNAT entries have been created.


Learn More

Deepen your knowledge of Terraform by accessing the following free material:

0 0 0
Share on


9 posts | 4 followers

You may also like



9 posts | 4 followers

Related Products