# Terraform

> Provision cloud VM with terraform
---

[Terraform](https://www.terraform.io/) is a popular IaC tool. You can create VMs on public cloud with one command.

Aliyun & AWS templates are used as example providers. You can take [`terraform.tf`](https://github.com/pgsty/pigsty/blob/v3.7.0/terraform/terraform.tf) as an example.

------

## Get Started

You can install terraform with [**homebrew**](https://brew.sh/) on macOS

```bash {title="install homebrew and terraform"}
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install terraform
```

Then init terraform cloud provider, adjust the `terraform.tf` config file and apply it:

```bash {title="cd ~/pigsty/terraform"}
terraform init
terraform apply #-auto-approve
```

Print public IP Address:

```bash
terraform output | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
```



--------

## AWS Setup

You have to set up aws config & credential to use AWS provider.

```bash
# ~/.aws

# ~/.aws/config
[default]
region = cn-northwest-1

# ~/.aws/credentials
[default]
aws_access_key_id = <YOUR_AWS_ACCESS_KEY>
aws_secret_access_key =  <AWS_ACCESS_SECRET>

# ~/.aws/pigsty-key
# ~/.aws/pigsty-key.pub
```

There's a contrib example for AWS (Amazon Web Services), but it's not actively maintained.

- [spec/aws-cn.tf](https://github.com/pgsty/pigsty/blob/v3.7.0/terraform/spec/aws-cn.tf) : AWS 4 node CentOS7 environment


--------

## Aliyun Setup

You can add your aliyun credentials to the environment file, such as `~/.bash_profile`

```bash
export ALICLOUD_ACCESS_KEY="<your_access_key>"
export ALICLOUD_SECRET_KEY="<your_secret_key>"
export ALICLOUD_REGION="cn-beijing"
```

Example config files:

- [spec/aliyun-meta.tf](https://github.com/pgsty/pigsty/blob/v3.7.0/terraform/spec/aliyun-meta.tf) : Aliyun 1 meta node template for all distro & amd/arm (default)
- [spec/aliyun-full.tf](https://github.com/pgsty/pigsty/blob/v3.7.0/terraform/spec/aliyun-full.tf) : Aliyun 4-node sandbox template for all distro & amd/arm.
- [spec/aliyun-oss.tf](https://github.com/pgsty/pigsty/blob/v3.7.0/terraform/spec/aliyun-oss.tf) : Aliyun 5-node building template for all distro & amd/arm.


Here are example [ECS public OS images](https://help.aliyun.com/zh/ecs/user-guide/public-mirroring-overview) used in Aliyun:

| Distribution | Image Prefix          | Image Prefix           |
|--------------|-----------------------|------------------------|
| CentOS 7.9   | `centos_7_9_x64`      | `rockylinux_8_10_arm6` |
| Rocky 8.10   | `rockylinux_8_10_x64` | `rockylinux_9_6_arm64` |
| Rocky 9.6    | `rockylinux_9_5_x64`  |                        |
| Debian 11.11 | `debian_11_11_x64`    |                        |
| Debian 12.11 | `debian_12_11_x64`    | `debian_12_11_arm64`   |
| Ubuntu 20.04 | `ubuntu_20_04_x64`    |                        |
| Ubuntu 22.04 | `ubuntu_22_04_x64`    | `ubuntu_22_04_arm64`   |
| Ubuntu 24.04 | `ubuntu_24_04_x64`    | `ubuntu_24_04_arm64`   |
| Anolis 8.8   | `anolisos_8_9_x64`    |                        |


--------

## QCloud Setup

There's a contrib example for QCloud (Tencent Cloud), but it's not actively maintained.

- [spec/tencentcloud.tf](https://github.com/pgsty/pigsty/blob/v3.7.0/terraform/spec/tencentcloud.tf) : QCloud 4 node CentOS7 environment
