Terraform 0.12 で EC2 と VPC をたてる
目次
概要
やりたいこと
Terraform で EC2 と VPC を作りたい!
Terraform 使うための準備
IAM と S3 バケット作成
AWS コンソールにログインし、 terraform 実行用 IAM と terraform.state ファイルを保存するバケットを作成する。
terraform.tfstate を AWS S3 上に保存する理由は、異なる環境から terraform を実施する予定のため。
- Terraform 用 IAM を作成 (terraform)
- AdministratorAccess
- アプリケーションからのみアクセス可能
- S3 バケット
- 東京リージョン
Terraform 設定
terraform は tfenv でいれる。
$ brew install tfenv $ tfenv --version tfenv 1.0.2
最新バージョン( 0.12.21 )を確認し、 terraform をインストールする。
$ tfenv list-remote
$ tfenv install 0.12.21
//インストール済みバージョン一覧を取得
$ tfenv list
* 0.12.21 (set by /usr/local/Cellar/tfenv/1.0.2/version)
terraform コマンドが利用できるようになった。
$ terraform --version Terraform v0.12.21 + provider.aws v2.50.0
aws cli 設定
Python の version は 3.7.4 を利用する。
python --version Python 3.7.4
pip で aws cli をインストールする。
pip install awscli --upgrade
terraform 実行用に使うユーザを登録する。
aws configure --profile terraform
プロファイルを確認しておく。
aws configure list --profile terraform
terraform 初期設定
作業ディレクトリを作成する。
mkdir terraform-vpc cd terraform-vpc
Provider を設定する。
vi main.tf
provider "aws" { profile = "terraform" region = "ap-northeast-1" } terraform { required_version = ">= 0.12.0" backend "s3" { profile = "terraform" region = "ap-northeast-1" bucket = "terraform-tfstate-runble1" key = "owasp-server/terraform.tfstate" encrypt = true } }
terraform の初期化。
terraform init
一度 terraform apply を実施し、作成したバケットに terraform.state ファイルが作成されていること。
terraform apply
terraform workspace を作成する。
今回の terraform テンプレート内で、 workspace 名をタグに指定するため利用する。
terraform workspace new vpc-ec2
今回はディレクトリ名と workspace 名を合わせる。
terraform workspace show
Terraform で EC2, VPC をたてる
Terraform で VPC をたてる
VPC 用の terraform ファイルを作成します。
vi myVPC.tf
resource "aws_vpc" "myVPC" { cidr_block = "10.1.0.0/16" instance_tenancy = "default" enable_dns_support = "true" enable_dns_hostnames = "false" tags = { Name = "${terraform.workspace}-vpc" } }
コードに問題がないことを確認します。
terraform plan
実行します。
terraform apply # Enter a value: のところで yes を入力
作成できたことをAWS コンソール画面から確認。 Name に workspace 名「vpc-ec2」が入っている。

Subnet + Gateway + RoteTable + SecurityGroup
Subnet を作成する。
resource "aws_subnet" "mySubnet" { cidr_block = "10.0.1.0/24" availability_zone = "ap-northeast-1a" vpc_id = aws_vpc.myVPC.id # trueにするとインスタンスにパブリックIPアドレスを自動的に割り当ててくれる map_public_ip_on_launch = true tags = { Name = "${terraform.workspace}-subnet" } }
Internet Gateway を作成する。
resource "aws_internet_gateway" "myGW" { vpc_id = aws_vpc.myVPC.id tags = { Name = "${terraform.workspace}-gateway" } }
Route table を作成する。
resource "aws_route_table" "myRouteTable" { vpc_id = aws_vpc.myVPC.id tags = { Name = "${terraform.workspace}-routetable" } } resource "aws_route" "myRoute" { gateway_id = aws_internet_gateway.myGW.id route_table_id = aws_route_table.myRouteTable.id destination_cidr_block = "0.0.0.0/0" } resource "aws_route_table_association" "myRouteTableAssociation" { subnet_id = aws_subnet.mySubnet.id route_table_id = aws_route_table.myRouteTable.id }
Security Group を作成する。
resource "aws_security_group" "mySecurityGroup" { vpc_id = aws_vpc.myVPC.id name = "mySecurityGroup" tags = { Name = "${terraform.workspace}-securitygroup" } } # インバウンドルール(ssh接続用) resource "aws_security_group_rule" "in_ssh" { security_group_id = aws_security_group.mySecurityGroup.id type = "ingress" cidr_blocks = ["0.0.0.0/0"] from_port = 22 to_port = 22 protocol = "tcp" } # インバウンドルール(pingコマンド用) resource "aws_security_group_rule" "in_icmp" { security_group_id = aws_security_group.mySecurityGroup.id type = "ingress" cidr_blocks = ["0.0.0.0/0"] from_port = -1 to_port = -1 protocol = "icmp" } # アウトバウンドルール(全開放) resource "aws_security_group_rule" "out_all" { security_group_id = aws_security_group.mySecurityGroup.id type = "egress" cidr_blocks = ["0.0.0.0/0"] from_port = 0 to_port = 0 protocol = "-1" }
反映する。
terraform plan
terraform apply
各リソースが作成されていることを確認。
EC2
Amazon Linux2 の AMI を使って、EC2 を作成する。
data aws_ssm_parameter amzn2_ami { name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" } resource "aws_instance" "myEC2" { ami = data.aws_ssm_parameter.amzn2_ami.value vpc_security_group_ids = [aws_security_group.mySecurityGroup.id] subnet_id = aws_subnet.mySubnet.id key_name = aws_key_pair.myKeyPair.id instance_type = "t2.micro" tags = { Name = "${terraform.workspace}-myEC2" } }
反映する。
terraform plan terraform apply
参考
初めてのTerraform サーバーワークスエンジニアブログ
ディスカッション
コメント一覧
まだ、コメントがありません