使用Terraform注册S3存储桶
首先
这次我打算用Terraform构建一个S3桶,因为在工作中需要搭建S3。之前也试过用Terraform构建EC2实例。
达到的目的
使用Terraform建立S3存储桶,并且完全不使用管理控制台。
请问你可以给我提供一种选项吗?
首次使用Terraform
通过SSM访问EC2
iResource:aws_s3_bucket
做法
首先,在工作文件夹中进行初始化处理,以便可以使用Terraform命令。
# terraform init
Initializing the backend...
:
:
Terraform has been successfully initialized!
:
:
commands will detect it and remind you to do so if necessary.
我准备了用于构建S3的Terraform文件。同时,我也准备了定义环境变量的文件们。
# terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
+ create
:
:
Plan: 1 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if
you run "terraform apply" now.
因为没有描述错误之类的问题,所以我将实际构建一下。
实际上,我犯了一个描述错误。
不是描述错误,而是我在桶名中使用了“”。
由于在桶名中不能使用“”,所以我已经更改为“-”。
aws_s3_bucket.bucket1: Creating...
╷
│ Error: Error validating S3 bucket name: only lowercase alphanumeric characters and hyphens allowed in "xxxxx_tf_bucket"
│
│ with aws_s3_bucket.bucket1,
│ on s3.tf line 1, in resource "aws_s3_bucket" "bucket1":
│ 1: resource "aws_s3_bucket" "bucket1" {
在这个构建过程中,我们仍然使用”-auto-approve”选项,仅仅是为了方便起见。
# terraform apply -auto-approve
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
+ create
Terraform will perform the following actions:
# aws_s3_bucket.bucket1 will be created
:
:
}
Plan: 1 to add, 0 to change, 0 to destroy.
aws_s3_bucket.bucket1: Creating...
aws_s3_bucket.bucket1: Creation complete after 8s [id=xxxxx-tf-bucket]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
似乎已经正常结束了,但需要确认一下Terraform的执行结果。
# terraform show
# aws_s3_bucket.bucket1:
resource "aws_s3_bucket" "bucket1" {
acl = "private"
arn = "arn:aws:s3:::xxxxx-tf-bucket"
bucket = "xxxxx-tf-bucket"
bucket_domain_name = "xxxxx-tf-bucket.s3.amazonaws.com"
bucket_regional_domain_name = "xxxxx-tf-bucket.s3.us-west-2.amazonaws.com"
force_destroy = false
hosted_zone_id = "xxxxx"
id = "xxxxx-tf-bucket"
region = "us-west-2"
request_payer = "BucketOwner"
tags_all = {}
versioning {
enabled = false
mfa_delete = false
}
}
我將确认一下是否实际上已在AWS上进行了搭建。
# aws s3 ls | grep xxxxx
2021-10-12 06:27:11 xxxxx-tf-bucket
通过使用AWS命令行界面,我可以确认在实际的亚马逊云服务上已经成功搭建了。
为了验证操作目的,我们将删除这个存储桶。
删除当然是指destroy。
我们也将在这里使用-auto-approve选项。
# terraform destroy -auto-approve
aws_s3_bucket.bucket1: Refreshing state... [id=c069544-tf-bucket]
Note: Objects have changed outside of Terraform
Terraform detected the following changes made outside of Terraform since the last "terraform apply":
# aws_s3_bucket.bucket1 has been changed
~ resource "aws_s3_bucket" "bucket1" {
:
:
}
Plan: 0 to add, 0 to change, 1 to destroy.
aws_s3_bucket.bucket1: Destroying... [id=xxxxx-tf-bucket]
aws_s3_bucket.bucket1: Destruction complete after 0s
Destroy complete! Resources: 1 destroyed.
在最后最后,我会确认它是否已被正确删除。
# terraform show
#
# aws s3 ls | grep xxxxx
#
我成功地把事情处理完毕而没有出什么问题。
最后
從一開始到最後都沒有使用GUI和管理控制台。
雖然會覺得GUI已經不需要了嗎?但絕對不是這樣的。
為了理解在TF中指定的參數的意義,最好還是在管理控制台上確認,以便得到視覺上的信息並深入理解。
方法因人而異,但我個人認為,可以先用GUI在AWS上構建,然後嘗試使用Terraform進行相同構建,這種方式既能理解又能建構。
顺便说一下…
我之前试过几次使用Terraform。
分别是针对EC2、SSM EC2和S3。
实际上,每次试验都将文件夹分开进行验证。
└terraform
├ec2
│ ├terraform.tfvars
│ ├variables.tf
│ └ec2.tf
├ec2ssm
│ ├terraform.tfvars
│ ├variables.tf
│ ├ec2.tf
│ └role.tf
└s3
├terraform.tfvars
├variables.tf
└s3.tf
在每个文件夹中运行init,然后运行plan,再运行apply……有点不方便或者说有点麻烦。
如果可以更改文件夹结构并只运行特定的tf文件,我感觉可以更轻松地进行验证。接下来,我打算推进这方面的重组。