When you need to provision resources across Alibaba Cloud, Amazon Web Services (AWS), and Microsoft Azure from a single workflow, use Resource Orchestration Service (ROS) with Terraform templates. Define all resources and their dependency relationships in one template and manage your multi-cloud infrastructure without switching between providers.
Scenario
If you want to create and manage resources of third-party cloud platforms such as AWS and Microsoft Azure on Alibaba Cloud, you can define the third-party resources and configure dependency relationships for the resources in a Terraform template. This helps you manage resources across cloud platforms in a convenient manner.
Terraform template structure
Every Terraform workspace has configuration files in its root directory — Terraform treats this as the root module. You can organize related resources into child modules stored in subdirectories (for example, modules/vpc/). The root module calls child modules using module blocks, and child modules expose values back to the root module through output blocks.
For more information about the structure of Terraform templates, see Structure of Terraform templates.
Create a Terraform template
The following procedure creates a Terraform template in ROS. The example provisions a vSwitch in a virtual private cloud (VPC), using a child module to separate the VPC definition from the root configuration.
Log on to the ROS console.
In the left-side navigation pane, choose Templates > My Templates.
On the My Templates page, click Create Template.
Set the Template Type parameter to Terraform Template.
-
Create the Terraform template files.
The example below creates a vSwitch in a VPC. The child module (
modules/vpc/main.tf) defines the VPC, and the rootmain.tfcalls that module and creates the vSwitch using the VPC ID passed through an output.The final directory structure:
. ├── main.tf (root module — creates the vSwitch) └── modules/ └── vpc/ └── main.tf (child module — creates the VPC)-
Create
modules/vpc/main.tfto define the VPC.In the upper-right corner of the Directory section, click the + icon and select Create Folder.
In the Create Folder dialog box, enter
modulesand click OK. A folder namedmodulesis created in the Directory section.Hover over the
modulesfolder, click the + icon on the right, and select Create Folder.In the Create Folder dialog box, enter
vpcand click OK. A folder namedvpcis created insidemodules.Hover over the
vpcfolder, click the + icon on the right, and select Create File.In the Create File dialog box, enter
main.tfand click OK. A file namedmain.tfis created in thevpcfolder.-
Click
main.tfand enter the following code in the code editor:modules/vpc/main.tf# Create a VPC resource "alicloud_vpc" "vpc" { name = "tf_test" cidr_block = "172.16.0.0/12" } # Expose the VPC ID so the root module can reference it output "vpc_id" { value = "${alicloud_vpc.vpc.id}" }The
resourceblock creates the VPC. Theoutputblock passes the VPC ID to any module that calls this one.
-
Edit the root
main.tfto call the VPC module and create the vSwitch.Click the
main.tffile in the root directory.-
Enter the following code in the code editor:
main.tf# Call the VPC child module module "my_vpc" { source = "./modules/vpc" } # Create a vSwitch in the VPC using the output from the child module resource "alicloud_vswitch" "vsw" { vpc_id = "${module.my_vpc.vpc_id}" cidr_block = "172.16.0.0/21" availability_zone = "cn-shanghai-b" } # Expose the vSwitch ID for use by other resources output "vsw_id" { value = "${alicloud_vswitch.vsw.id}" }The
moduleblock calls the VPC child module and makes its outputs available asmodule.my_vpc.*. Theresourceblock for the vSwitch referencesmodule.my_vpc.vpc_idto use the VPC created by the child module — this is how dependency relationships are expressed between modules.
-
In the lower-left corner of the Create Template page, choose .
In the Save as My Template dialog box, configure the Template Name, Template Description, Resource Group, and Tag parameters.
Click OK.