Terraform入門 AWS VPC リソースコードを作成する手順

環境
AWS Cloud9

概要
AWS Cloud9 は統合開発環境、または IDE です。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
この AWS Cloud9 IDE では、リッチなコード編集エクスペリエンスを実現しており、複数のプログラミング言語、ランタイムデバッガ、
および組み込みターミナルがサポートされています。
この AWS Cloud9 IDE では、リッチなコード編集エクスペリエンスを実現しており、複数のプログラミング言語、ランタイムデバッガ、 および組み込みターミナルがサポートされています。
この AWS Cloud9 IDE では、リッチなコード編集エクスペリエンスを実現しており、複数のプログラミング言語、ランタイムデバッガ、
および組み込みターミナルがサポートされています。

操作手順
1.AdministratorAccess権限を持ったIAMユーザでAWSマネジメントコンソールにログインします。

2.Cloud9画面に遷移します。

3.画面右上のCreate environmentをクリックします。

4.環境名を入力し、Next Stepをクリックします。

5.Configure settingsは特に何も変更せずにNext Stepをクリックし、設定内容を確認後、Create environmentをクリックします。

6.Cloud9が立ち上がります。
Cloud9には最初からTerraformがインストールされています。

7.Terraformのバージョンを確認します。
$ terraform -v

作業用のディレクトリを作成します。
$ mkdir terraform && cd terraform

以下のコマンドでtfファイルを作成します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ touch provider.tf
$ touch aws_vpc.tf
$ touch provider.tf $ touch aws_vpc.tf
$ touch provider.tf
$ touch aws_vpc.tf

8.provider.tf

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
provider "aws" {
region = "ap-northeast-1"
}
provider "aws" { region = "ap-northeast-1" }
provider "aws" {
  region = "ap-northeast-1"
}

regionには、リソース構築先のAWSリージョンを指定します。今回は東京リージョン(ap-northeast-1)を指定しています。

リソースを記述する書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
resource "リソースの種類" "リソース名" {
設定項目1 = 設定値
設定項目2 = 設定値
設定項目3 = 設定値
}
resource "リソースの種類" "リソース名" { 設定項目1 = 設定値 設定項目2 = 設定値 設定項目3 = 設定値 }
resource "リソースの種類" "リソース名" {
    設定項目1 = 設定値
    設定項目2 = 設定値
    設定項目3 = 設定値
}

9.VPCの作成コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#----------------------------------------
# VPCの作成
#----------------------------------------
resource "aws_vpc" "test_vpc" {
cidr_block = "10.172.11.0/22"
enable_dns_hostnames = true
}
#----------------------------------------
# パブリックサブネットの作成
#----------------------------------------
resource "aws_subnet" "test_subnet" {
vpc_id = aws_vpc.test_vpc.id
cidr_block = "10.172.11.0/24"
availability_zone = "ap-northeast-1a"
map_public_ip_on_launch = true
}
#----------------------------------------
# インターネットゲートウェイの作成
#----------------------------------------
resource "aws_internet_gateway" "test_igw" {
vpc_id = aws_vpc.test_vpc.id
}
#----------------------------------------
# ルートテーブルの作成
#----------------------------------------
resource "aws_route_table" "test_rtb" {
vpc_id = aws_vpc.test_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.test_igw.id
}
}
#----------------------------------------
# サブネットにルートテーブル
#----------------------------------------
resource "aws_route_table_association" "test_rt_assoc" {
subnet_id = aws_subnet.test_subnet.id
route_table_id = aws_route_table.test_rtb.id
}
#----------------------------------------
# セキュリティグループの作成
#----------------------------------------
resource "aws_security_group" "test_sg" {
name = "test-sg"
vpc_id = aws_vpc.test_vpc.id
ingress {
from_port = 22
to_port = 22
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"]
}
}
#---------------------------------------- # VPCの作成 #---------------------------------------- resource "aws_vpc" "test_vpc" { cidr_block = "10.172.11.0/22" enable_dns_hostnames = true } #---------------------------------------- # パブリックサブネットの作成 #---------------------------------------- resource "aws_subnet" "test_subnet" { vpc_id = aws_vpc.test_vpc.id cidr_block = "10.172.11.0/24" availability_zone = "ap-northeast-1a" map_public_ip_on_launch = true } #---------------------------------------- # インターネットゲートウェイの作成 #---------------------------------------- resource "aws_internet_gateway" "test_igw" { vpc_id = aws_vpc.test_vpc.id } #---------------------------------------- # ルートテーブルの作成 #---------------------------------------- resource "aws_route_table" "test_rtb" { vpc_id = aws_vpc.test_vpc.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.test_igw.id } } #---------------------------------------- # サブネットにルートテーブル #---------------------------------------- resource "aws_route_table_association" "test_rt_assoc" { subnet_id = aws_subnet.test_subnet.id route_table_id = aws_route_table.test_rtb.id } #---------------------------------------- # セキュリティグループの作成 #---------------------------------------- resource "aws_security_group" "test_sg" { name = "test-sg" vpc_id = aws_vpc.test_vpc.id ingress { from_port = 22 to_port = 22 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"] } }
#----------------------------------------
# VPCの作成
#----------------------------------------
resource "aws_vpc" "test_vpc" {
  cidr_block           = "10.172.11.0/22"
  enable_dns_hostnames = true
}
#----------------------------------------
# パブリックサブネットの作成
#----------------------------------------
resource "aws_subnet" "test_subnet" {
  vpc_id                  = aws_vpc.test_vpc.id
  cidr_block              = "10.172.11.0/24"
  availability_zone       = "ap-northeast-1a"
  map_public_ip_on_launch = true
}
#----------------------------------------
# インターネットゲートウェイの作成
#----------------------------------------
resource "aws_internet_gateway" "test_igw" {
  vpc_id = aws_vpc.test_vpc.id
}
#----------------------------------------
# ルートテーブルの作成
#----------------------------------------
resource "aws_route_table" "test_rtb" {
  vpc_id = aws_vpc.test_vpc.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.test_igw.id
  }
}
#----------------------------------------
# サブネットにルートテーブル
#----------------------------------------
resource "aws_route_table_association" "test_rt_assoc" {
  subnet_id      = aws_subnet.test_subnet.id
  route_table_id = aws_route_table.test_rtb.id
}
#----------------------------------------
# セキュリティグループの作成
#----------------------------------------
resource "aws_security_group" "test_sg" {
  name   = "test-sg"
  vpc_id = aws_vpc.test_vpc.id
  ingress {
    from_port   = 22
    to_port     = 22
    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"]
  }
}

10.実行コマンド
$ terraform init
結果にTerraform has been successfully initialized!と表示されていれば成功です
$ terraform validate
結果にSuccess! The configuration is valid.と表示されていれば成功です。

$ terraform fmt
コードのインデントを自動で整形する
$ terraform apply
実環境上にリソースを構築します。

11.リソースを削除する
$ terraform destroy

 

Terraform

Posted by arkgame