使用 Terraform 在 Amazon S3 服務建立資源

做法

建立工作資料夾。

1
2
mkdir -p terraform-practice/s3
cd terraform-practice/s3

使用 init 指令初始化工作資料夾,需要的供應商外掛(provider plugins)將會被下載下來。

1
terraform init

在工作資料夾新增 main.tf 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.1.0"
}
}

required_version = ">= 1.1"
}

# 指定為 AWS Provider
provider "aws" {
region = "ap-northeast-1"
}

# 建立一個儲存貯體
resource "aws_s3_bucket" "xxx-playground-s3-test" {
bucket = "xxx-playground-s3-test"
}

使用 plan 指令查看執行計畫。

1
aws-vault exec --backend=file playground-PowerUser -- terraform plan

使用 apply 指令建立實體。

1
aws-vault exec --backend=file playground-PowerUser -- terraform apply

使用 destroy 指令刪除實體。

1
aws-vault exec --backend=file playground-PowerUser -- terraform destroy

參考資料