这是部门中动手操作所使用的材料。我将其公开,因为这很重要。
什么是Terraform?
一种由HashiCorp提供的用于通过代码管理各种云服务的工具。
编码基础结构和其他设置可促进协作,重用和版本控制。
有许多有用的功能,如空运行和资源依赖关系可视化,用于实现所谓的"基础结构即代码"。
HashiCorp的Terraform
TL; DR
环境建设
为Terraform
创建IAM用户
为Terraform创建IAM用户。通过参考下面的URL创建一个IAM用户。
这次是动手操作,因此请创建具有Admin特权的用户。
SecretAccessKey不会重新发行,因此,如果丢失,请重新创建您的帐户。
创建IAM用户(控制台)
AWS CLI安装
安装AWS CLI并配置设置。请参考以下URL了解安装方法
在macOS上安装AWS CLI
AWS CLI设置
安装完成后,配置AWS CLI并确认您可以运行AWS CLI。
1 2 3 | $ aws --version $ aws configure $ aws s3api list-buckets # S3 のバケット一覧を表示するコマンド |
通过tfenv
安装
Terraform更新很快,并且还有很多错误 del>,因此强烈建议使用
1 2 3 4 5 6 7 | $ brew install tfenv $ tfenv -v $ tfenv list-remote # インストール可能なバージョンをリモートから取得して列挙します。 $ tfenv install 0.12.10 # 0.12.10 をインストール $ tfenv use 0.12.10 # 0.12.10 を使用する $ tfenv list # インストール済み $ terraform --version # バージョン一覧 |
通过创建
1 2 | $ touch .terraform-version $ tfenv use 0.12.10 # このディレクトリ以下で 0.12.10 を使用する |
HelloWorld
在Terraform中将一个S3存储桶创建为HelloWorld。
1 2 3 | $ mkdir handson $ cd handson $ touch main.tf |
创建一个
主文件
1 2 3 4 5 6 7 8 9 10 | # Provider provider "aws" { profile = "default" region = "ap-northeast-1" } # Resource resource "aws_s3_bucket" "tekitouna_domain_nyuryoku" { bucket = "tekitouna.domain.nyuryoku" } |
使用
1 2 | $ terraform init $ terraform plan |
有关新创建的资源的信息如下所示。
您会看到创建了一个名为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Terraform will perform the following actions: # aws_s3_bucket.tekitouna_domain_nyuryoku will be created + resource "aws_s3_bucket" "tekitouna_domain_nyuryoku" { + acceleration_status = (known after apply) + acl = "private" + arn = (known after apply) + bucket = "tekitouna.domain.nyuryoku" + bucket_domain_name = (known after apply) + bucket_regional_domain_name = (known after apply) + force_destroy = false + hosted_zone_id = (known after apply) + id = (known after apply) + region = (known after apply) + request_payer = (known after apply) + website_domain = (known after apply) + website_endpoint = (known after apply) + versioning { + enabled = (known after apply) + mfa_delete = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. |
如果
没问题,请执行
1 | $ terraform apply |
已创建新的S3存储桶。
通过执行以下命令,您可以看到已经创建了一个新的存储桶。
1 | aws s3api list-buckets | grep tekitouna.domain.nyuryoku |
如果
应用成功,将生成
由于它是动手操作,因此我将跳过详细的说明,但是对于多人开发,建议将
使用Terraform
在S3中管理tfstate文件
terraform.tfstate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | { "version": 4, "terraform_version": "0.12.10", "serial": 1, "lineage": "6a7bf262-a78d-decf-166f-9f1eac238641", "outputs": {}, "resources": [ { "mode": "managed", "type": "aws_s3_bucket", "name": "tekitouna_domain_nyuryoku", "provider": "provider.aws", "instances": [ { "schema_version": 0, "attributes": { "acceleration_status": "", "acl": "private", "arn": "arn:aws:s3:::tekitouna.domain.nyuryoku.xxxx", "bucket": "tekitouna.domain.nyuryoku.xxxx", "bucket_domain_name": "tekitouna.domain.nyuryoku.xxxx.s3.amazonaws.com", "bucket_prefix": null, "bucket_regional_domain_name": "tekitouna.domain.nyuryoku.xxxx.s3.ap-northeast-1.amazonaws.com", "cors_rule": [], "force_destroy": false, "hosted_zone_id": "Z2M4EHUR26P7ZW", "id": "tekitouna.domain.nyuryoku.xxxx", "lifecycle_rule": [], "logging": [], "object_lock_configuration": [], "policy": null, "region": "ap-northeast-1", "replication_configuration": [], "request_payer": "BucketOwner", "server_side_encryption_configuration": [], "tags": null, "versioning": [ { "enabled": false, "mfa_delete": false } ], "website": [], "website_domain": null, "website_endpoint": null }, "private": "bnVsbA==" } ] } ] } |
接下来,删除创建的存储桶。
1 | $ terraform destroy |
显示要删除的资源的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Terraform will perform the following actions: # aws_s3_bucket.tekitouna_domain_nyuryoku will be destroyed - resource "aws_s3_bucket" "tekitouna_domain_nyuryoku" { - acl = "private" -> null - arn = "arn:aws:s3:::tekitouna.domain.nyuryoku.xxxx" -> null - bucket = "tekitouna.domain.nyuryoku.xxxx" -> null - bucket_domain_name = "tekitouna.domain.nyuryoku.xxxx.s3.amazonaws.com" -> null - bucket_regional_domain_name = "tekitouna.domain.nyuryoku.xxxx.s3.ap-northeast-1.amazonaws.com" -> null - force_destroy = false -> null - hosted_zone_id = "Z2M4EHUR26P7ZW" -> null - id = "tekitouna.domain.nyuryoku.xxxx" -> null - region = "ap-northeast-1" -> null - request_payer = "BucketOwner" -> null - tags = {} -> null - versioning { - enabled = false -> null - mfa_delete = false -> null } } Plan: 0 to add, 0 to change, 1 to destroy. |
如果更改没有问题,请执行。
现在,您已经使用Terraform创建并删除了它。
尝试将通过控制台创建的资源导入Terraform
通过参考下图中的过程,创建发布为Web服务器的S3存储桶。



设置存储桶策略。
1 2 3 4 5 6 7 8 9 10 11 12 | { "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadForGetBucketObjects", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::tekitouna.domain.nyuryoku/*" } ] } |
设置
策略后,创建一个名为
index.html
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> タイトル </head> <body> <h1>タイトル</h1> <p>Amazon S3を使って静的なウェブページを公開してみた</p> </body> </html> |
error.html
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> Error </head> <body> <h1>Error</h1> <p>エラーページです</p> </body> </html> |
稍等片刻,然后访问S3文件的URL,并检查是否显示了该文件。
http://tekitouna.domain.nyuryoku.s3-website-ap-northeast-1.amazonaws.com/index.html
http://tekitouna.domain.nyuryoku.s3-website-ap-northeast-1.amazonaws.com/error.html
接下来,让我们使用Terraform管理创建的S3存储桶。
将由以下命令创建的S3存储桶置于Terraform的控制下。
1 | $ terraform import aws_s3_bucket.tekitouna_domain_nyuryoku tekitouna.domain.nyuryoku |
使用
1 | $ terraform plan |
填补差异-