By He Guimin, nicknamed Xiaozhu at Alibaba.
This tutorial is intended to be a guide on how you can write Terraform modules on Alibaba Cloud. In it, I will explain how to write a universal and standard module based on my own understanding of modules as a technical expert at Alibaba Cloud.
To get started, let's first discuss what exactly Modules are. In short, Modules are small, reusable Terraform configurations that let you manage a group of related resources as if they were a single resource. Modules make Terraform's template structure simpler and clearer while reducing the complexity of template writing and maintenance. For more information about why modules are used, check out post Modules That Make Terraform Easier to Use.
Currently, the modules that are officially registered on the Terraform Registry only support GitHub repositories. Therefore, before writing a template, you must first create a standard GitHub repository:
terraform-<PROVIDER>-<NAME>
. For example, the repository can be named terraform-alicloud-ecs-instance
or terraform-alicloud-vpc
.To better manage all Alibaba Cloud Terraform modules, we build all repositories under Terraform Alibaba Cloud Modules
. If you need to create a new repository, contact the owner.
After a repository is created for a module, fork this repository and clone it to the corresponding local machine. Next, start writing the module. Hashicorp has provided principles and norms for standard modules to follow. For more information, see standard-module-structure. This tutorial will build on this information.
1. Basic Principles
Server Load Balancer (SLB) instance
and SLB attachment
. These two child resources are defined in modules/slb
and modules/slb_attachment
, respectively. Then, the two child modules are combined in main.tf
at the outermost layer into a new module.2. main.tf
this
or default
. Keywords or keyword prefixes such as foo
and test
should be avoided.provider "alicloud" {
...
configuration_source = "terraform-alicloud-modules/demo" // This should be replaced by the specified owner and module name
}
3. variables.tf
4. outputs.tf
instance_ids
list variable.5. README
Usage
to specify how to use this module. The source format of the module is source = "<Repo Organization>/<NAME>/alicloud"
, such as source = "terraform-alicloud-modules/slb-listener/alicloud"
.To help you better write modules, Alibaba Cloud provides a Module Demo for your reference.
After you write a template, use Terratest to test the current module. When the test is successful, submit the corresponding code to GitHub.
To submit your module to Terraform Alibaba Cloud Modules, you can leave a message below or send an email to heguimin36@163.com. After the application is approved, I will create a new repository for you. Every developer who contributes a module will have the opportunity to become a member of Terraform Alibaba Cloud Modules.
A Terraform module implements version control by using repository releases. After its code is confirmed to be correct, including its README, a new release is published. Then, you can register and publish this module on the Terraform Registry. For more information, see Publishing Modules.
The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.
Getting Started with Terraform with Alibaba Cloud: What is Terraform?
What Technologies Do We Think Will Rise to the Occasion During and After This Pandemic
2,599 posts | 762 followers
FollowAlibaba Clouder - April 23, 2020
Alibaba Cloud Serverless - February 28, 2023
Alibaba Clouder - June 15, 2020
Alibaba Clouder - January 9, 2019
JDP - May 7, 2021
ApsaraDB - February 9, 2022
2,599 posts | 762 followers
FollowAn on-demand database hosting service for MySQL with automated monitoring, backup and disaster recovery capabilities
Learn MoreAn on-demand database hosting service for PostgreSQL with automated monitoring, backup and disaster recovery capabilities
Learn MoreApsaraDB RDS for MariaDB supports multiple storage engines, including MySQL InnoDB to meet different user requirements.
Learn MoreAn on-demand database hosting service for SQL Server with automated monitoring, backup and disaster recovery capabilities
Learn MoreMore Posts by Alibaba Clouder