让我们使用Terraform通过容器构建AWS基础架构吧!
前提 – 一个重要的先决条件
-
- AWSを使用
-
- AWSのアカウントを作成済み
- AWS Vaultをインストールおよび設定済み
如果你还没有安装和配置AWS Vault,请参考以下官方文件和文章。
为什么要使用容器?
由于预计在多个项目中进行开发,所以我认为根据项目的不同,Terraform的版本可能会有所不同。因此,我认为与其在本地上操作,不如为每个项目创建一个terraform容器,并通过容器来使用Terraform会更好。
本次我们将根据教程创建一个t2.micro的EC2实例。
目录结构
tree
.
├── .gitignore
└── infra
├── docker-compose.yml
└── main.tf
.gitignore 文件
从下面的网站上复制terraform的.gitignore内容。
基础设施/容器化编排文件:docker-compose.yml
请按照下面的方式编写docker-compose.yml文件。
我們將使用的映像是hashicorp/terraform:1.3.6。
version: '3.9'
services:
terraform:
container_name: terraform
image: hashicorp/terraform:1.3.6
# M1チップでも動くように
platform: linux/x86_64
volumes:
- .:/infra
working_dir: /infra
environment:
# AWS_ACCESS_KEY_IDとAWS_SECRET_ACCESS_KEYを環境変数として使用
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
# MFAを使うため、AWS_SESSION_TOKENも環境変数として使用
- AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
# 永続Volumeを作成
volumes:
infra:
基础设施/主要.tf
这次将几乎原样使用官方文件中的教程内容。
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
# 東京リージョンを使用します
region = "ap-northeast-1"
}
resource "aws_instance" "app_server" {
ami = "ami-0bba69335379e17f8"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
让我们试着用Terraform构建基础架构吧!
调整访问密钥的有效期限 de
如果您尚未调整访问密钥,请执行–持续时间。
aws-vault exec shun198 --duration=12h
使用Terraform进行初始设置。
请运行以下命令来进行Terraform的初始设置。
docker-compose -f infra/docker-compose.yml run --rm terraform init
如果看到以下日志,那就代表成功了。
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 4.16"...
- Installing hashicorp/aws v4.48.0...
- Installed hashicorp/aws v4.48.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
如果你在意的话,可以运行terraform fmt命令来修改main.tf文件的格式。
docker-compose -f infra/docker-compose.yml run --rm terraform fmt
main.tf
我会使用validate命令来确认main.tf文件是否有效。
docker-compose -f infra/docker-compose.yml run --rm terraform validate
Success! The configuration is valid.
使用`plan`命令确认适用于AWS的更改。
docker-compose -f infra/docker-compose.yml run --rm terraform plan
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:
创建一个EC2实例
将main.tf的配置应用于AWS,这次将自动输入yes执行-auto-approve。
docker-compose -f infra/docker-compose.yml run --rm terraform apply -auto-approve
如果出现以下日志,则表示成功。
Plan: 1 to add, 0 to change, 0 to destroy.
aws_instance.app_server: Creating...
aws_instance.app_server: Still creating... [10s elapsed]
aws_instance.app_server: Still creating... [20s elapsed]
aws_instance.app_server: Still creating... [30s elapsed]
aws_instance.app_server: Creation complete after 32s [id=i-01d6eeab4d9a96cb4]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

我們要刪除已建立的基礎設施。
删除EC2实例
docker-compose -f infra/docker-compose.yml run --rm terraform destroy
我要输入”是”。
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
如果出现以下日志,则表示成功。
aws_instance.app_server: Destroying... [id=i-01d6eeab4d9a96cb4]
aws_instance.app_server: Still destroying... [id=i-01d6eeab4d9a96cb4, 10s elapsed]
aws_instance.app_server: Still destroying... [id=i-01d6eeab4d9a96cb4, 20s elapsed]
aws_instance.app_server: Destruction complete after 30s
Destroy complete! Resources: 1 destroyed.

请使用Makefile将命令简化。
由于命令太长,我会创建一个Makefile。例如,当你想要整理格式时。
make fmt
只需要一种选项:
只要击打而已
docker-compose -f infra/docker-compose.yml run --rm terraform fmt
因为打了这个球,我会感到非常轻松。
RUN_TERRAFORM = docker-compose -f infra/docker-compose.yml run --rm terraform
IAM_USER = shun198
DURATION = 12h
vault:
aws-vault exec $(IAM_USER) --duration=$(DURATION)
init:
$(RUN_TERRAFORM) init
fmt:
$(RUN_TERRAFORM) fmt
validate:
$(RUN_TERRAFORM) validate
show:
$(RUN_TERRAFORM) show
apply:
$(RUN_TERRAFORM) apply -auto-approve
graph:
$(RUN_TERRAFORM) graph | dot -Tsvg > graph.svg
destroy:
$(RUN_TERRAFORM) destroy
介绍这篇文章
如果您有兴趣的话,我也写了以下的文章,如果您愿意阅读,我将不胜感激。
参照