Terraform integration example

更新时间:
复制 MD 格式

Terraform is an open source tool that you can use to securely and efficiently provision and manage cloud infrastructure. You can use Terraform to manage Service Catalog resources. This topic provides an example of how to create a product portfolio using Terraform.

Supported resources

The following is a partial list of resources and data sources that support Terraform orchestration for Service Catalog. For more information, see Service Catalog Terraform resources and data sources. If you are new to Terraform, see Introduction to Terraform.

Resource type

Resource link

Description

Resources

alicloud_service_catalog_portfolio

Creates a product portfolio.

alicloud_service_catalog_provisioned_product

Launches a product.

Data Sources

alicloud_service_catalog_end_user_products

The end user sends a query to the product.

alicloud_service_catalog_launch_options

Queries launch options.

alicloud_service_catalog_portfolios

Queries product portfolios.

alicloud_service_catalog_product_versions

Queries product versions.

alicloud_service_catalog_provisioned_products

Queries provisioned products.

Procedure

Configure permissions

To execute a Terraform template, you need the permissions of a Resource Access Management (RAM) user. Create a RAM user, obtain an AccessKey, and add an access policy. Set this AccessKey as an environment variable for Terraform. Add the following permission to the RAM user:

  • AliyunServiceCatalogAdminFullAccess: The administrator permissions to manage Service Catalog resources.

An Alibaba Cloud account has access permissions for all APIs, which poses a high security risk. We recommend that you create and use a RAM user for API access or daily O&M. Grant permissions as needed before you make API calls. The RAM user must have the permissions to manage Service Catalog resources. For more information, see Use RAM for access control.

Install Terraform

  • To install and configure Terraform locally, see Install and configure Terraform locally.

    After the installation is complete, open a command-line terminal and run the terraform version command. If version information is returned, the installation is successful.

  • If you do not want to install Terraform locally, you can use Cloud Shell. Cloud Shell is an online service from Alibaba Cloud that provides a built-in Terraform runtime environment.

Write the template

Create a file named main.tf and add the following content to create a product portfolio.

provider "alicloud" {
  region = "cn-hangzhou"
}

variable "name" {
  default = "tf_example"
}
resource "alicloud_service_catalog_portfolio" "default" {
  portfolio_name = var.name
  provider_name  = var.name
}

For more information about the configuration of the alicloud_service_catalog_portfolio resource type, see Service Catalog Terraform resources and data sources.

Run the template

This example uses the Windows operating system. The commands may vary based on your operating system.

  1. Initialize the modules, including the provider.

    terraform init

    image

  2. Validate the template syntax.

    terraform validate

    image

  3. Create an execution plan.

    terraform plan

    image

  4. Deploy the template.

    terraform apply

    After you run the command, enter yes to apply the changes to the resources. Run the terraform destroy command to release the resources created by the template.

  5. Verify that the operation is complete using OpenAPI, an SDK, or the Service Catalog console.