使用 Terraform 在 AWS 上建立环境(使用 Cloud9)
作为最近灾害恢复准备的一部分,我们正在考虑快速构建AWS实例的方法。在这方面,使用Terraform进行服务器配置是一种有前途的方法。
Terraform简介
Terraform 是一种开源的基础设施自动化工具,用于通过代码管理基础设施。此工具可以统一处理云提供商,如AWS、Azure、Google Cloud等,以及本地资源,以高效且简单地进行基础设施的供应和管理。
Terraform 的官方网站:https://www.terraform.io/
安装Terraform
根据Terraform的版本和操作系统的不同,细节可能会有所不同。您可以在以下官方网页上找到每个操作系统的安装步骤:
https://developer.hashicorp.com/terraform/downloads
本次我们将利用AWS Cloud9环境来使用Terraform。
亚马逊云(AWS)Cloud9
AWS Cloud9 是亚马逊提供的云端集成开发环境(IDE)服务。开发者和团队可以利用该工具进行应用开发、调试、测试和协作,以提高效率。
AWS Cloud9的官方网站
https://docs.aws.amazon.com/zh_cn/cloud9/latest/user-guide/welcome.html
Cloud9的环境配置
第一步
在AWS管理控制台中,進行「Cloud9」的搜索,然后点击「创建环境」。

第二步

第三步


第四步
#AWS CLIのバージョン確認
$ aws --version
aws-cli/2.13.26 Python/3.11.6 Linux/5.10.197-186.748.amzn2.x86_64 exe/x86_64.amzn.2 prompt/off
#terraformのバージョン確認
$ terraform -v
Terraform v1.6.2
on linux_amd64
#gitのバージョン確認
$ git -v
git version 2.40.1
#tfenvのインストール(Terraformのバージョン管理用)
$ git clone https://github.com/tfutils/tfenv.git ~/.tfenv
#パスを通す
$ sudo ln -s ~/.tfenv/bin/* /usr/local/bin
#tfenvのバージョン確認
$ tfenv -v
tfenv 3.0.0-18-g1ccfddb
代码实现 (Mandarin Chinese)

首先,创建一个文件夹并进行切换。
$ mkdir terraform-test && cd terraform-test
#以下3つのファイルを作成
$ touch provider.tf
$ touch aws_network.tf
$ touch aws_ec2.tf
以下是provider.tf文件的内容。
provider "aws" {
region = "ap-northeast-1"
}
在provider.tf文件中,我们需要写入构建资源所需的提供者信息。这次我们定义了”aws”作为提供者。region字段用于指定构建资源所在的AWS区域。这次我们指定了东京区域(ap-northeast-1)。
请查阅以下官方文档以了解在Terraform中支持的其他提供商:
https://registry.terraform.io/browse/providers
以下,对Terraform代码的语法进行简要解释。
Terraform的代码是用一种叫做HCL(HashiCorp配置语言)的语言编写的。HCL语法非常简单,即使对于初学者来说也很容易理解。
假如说,
resource "リソースの種別" "リソース名" {
項目1 = 項目のオプション
項目2 = 項目のオプション
項目3 = 項目のオプション
}
「资源类型」->例如,aws_instance、aws_vpc
※资源类型的官方页面:https://registry.terraform.io/providers/hashicorp/aws/latest/docs
可以填入任意的字符作为“资源名称”。
aws_network.tf 的汇编
# VPCの作成
resource "aws_vpc" "terraform_vpc" {
cidr_block = "172.17.0.0/16"
enable_dns_hostnames = true
}
# Publicサブネット作成
resource "aws_subnet" "terraform_publicSubnet" {
vpc_id = aws_vpc.terraform_vpc.id
cidr_block = "172.17.1.0/24"
availability_zone = "ap-northeast-1a"
map_public_ip_on_launch = true
}
# インターネットゲートウェイ作成
resource "aws_internet_gateway" "terraform_igw" {
vpc_id = aws_vpc.terraform_vpc.id
}
# Publicルートテーブルの作成
resource "aws_route_table" "terraform_public_rtb" {
vpc_id = aws_vpc.terraform_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.terraform_igw.id
}
}
# Publicサブネットにルートテーブルを紐づけ
resource "aws_route_table_association" "terraform_public_rtable_assoc" {
subnet_id = aws_subnet.terraform_publicSubnet.id
route_table_id = aws_route_table.terraform_public_rtb.id
}
# セキュリティグループの作成
resource "aws_security_group" "terraform_sg" {
name = "terraform-security01"
vpc_id = aws_vpc.terraform_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
亚马逊云服务器.tf
# EC2インスタンスの作成
resource "aws_instance" "terraform_server" {
ami = "ami-04f0680f68f076681" # Amazon Linux 2
instance_type = "t2.micro"
subnet_id = aws_subnet.terraform_publicSubnet.id
vpc_security_group_ids = [aws_security_group.terraform_sg.id]
user_data = <<EOF
#!/bin/bash
# サーバーの設定変更
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
EOF
}
在AWS环境中构建资源。
#terraform初期化、AWS操作に必要なバイナリファイルをダウンロードします。
$ terraform init
#terraformのコードフォーマットする
$ terraform fmt
# 実施内容の確認
$ terraform plan
# 実環境への反映します。
# Enter a valueと表示されると、問題がなければ、「Yes」を入力しましょう。
$ terraform apply
确认结果



最后进行清理
$ terraform destroy