2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Terraformを使ったAWS環境の構築

Posted at

はじめに

Terraformを使ってみて、AWSサービスをコンソールやCLIを使わずに自動で構築します。
コマンドはGitbashを使用しています。

構築対象は以下となります。
【構築対象」
・VPC
・Subnet(パブリック)
・EC2(OSはAmazonLinux2023)
・SecurityGroup

※事前に下記のコマンドを使って公開鍵、秘密鍵の作成をお願いします。
ssh-keygen -t rsa -b 2048 -f terratest-proto-keypair

1.Terraformの設定ファイルの作成

1-1.Terraformの設定ファイル(main.tf)の作成

# ---------------------------------------------
# Terraform configuration
# ---------------------------------------------
terraform {
  required_version = ">=0.13"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

# ---------------------------------------------
# Provider
# ---------------------------------------------
provider "aws" {
  region  = "ap-northeast-1"
}

# ---------------------------------------------
# Variables
# ---------------------------------------------
variable "project" {
  type = string
}

variable "environment" {
  type = string
}

main.tfに記入されている項目について

Terraform configuration
Terraformのバージョンとhashicorp/aws プロバイダを指定します。

Provider
対象をAWSの東京リージョンと指定します。
Variables
ここで変数を定義します。

1-2.環境変数の定義

main.tfファイルで定義したVariablesを元に環境変数の定義ファイル(terraform.tfvars)を作成します。

project     = "terratest"
environment = "proto"

1-3.ネットワークの作成

AWSのネットワーク構成のファイル(network.tf)を作成します

# ---------------------------------------------
# VPC
# ---------------------------------------------
resource "aws_vpc" "test_vpc" {
  cidr_block                       = "ciderを入力"
  instance_tenancy                 = "default"
  enable_dns_support               = true
  enable_dns_hostnames             = true
  assign_generated_ipv6_cidr_block = false

  tags = {
    Name    = "${var.project}-${var.environment}-vpc"
    Project = var.project
    Env     = var.environment
  }
}

# ---------------------------------------------
# Subnet
# ---------------------------------------------

resource "aws_subnet" "test_public_subnet_1a" {
  vpc_id                  = aws_vpc.test_vpc.id
  availability_zone       = "ap-northeast-1a"
  cidr_block              = "ciderを入力"
  map_public_ip_on_launch = true

  tags = {
    Name    = "${var.project}-${var.environment}-public-subnet-1a"
    Project = var.project
    Env     = var.environment
    Type    = "public"
  }
}

resource "aws_subnet" "test_public_subnet_1c" {
  vpc_id                  = aws_vpc.test_vpc.id
  availability_zone       = "ap-northeast-1c"
  cidr_block              = "ciderを入力"
  map_public_ip_on_launch = true

  tags = {
    Name    = "${var.project}-${var.environment}-public-subnet-1c"
    Project = var.project
    Env     = var.environment
    Type    = "public"
  }
}
# ---------------------------------------------
# インターネットゲートウェイ
# ---------------------------------------------
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.test_vpc.id

  tags = {
    Name = "${var.project}-${var.environment}-igw"
  }
}

# ---------------------------------------------
# ルートテーブル(インターネット向け)
# ---------------------------------------------
resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.test_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }

  tags = {
    Name = "${var.project}-${var.environment}-public-rt"
  }
}
# サブネットとルートテーブルを関連付け
resource "aws_route_table_association" "public_assoc" {
  subnet_id      = aws_subnet.test_public_subnet_1a.id
  route_table_id = aws_route_table.public_rt.id
}

1-4.SecurityGroupの設定ファイルの作成

# ---------------------------------------------
# Security Group
# ---------------------------------------------
#security group
resource "aws_security_group" "ec2_sg" {
  name        = "${var.project}-${var.environment}-sg"
  description = "EC2test security group"
  vpc_id      = aws_vpc.test_vpc.id

  tags = {
    Name    = "${var.project}-${var.environment}-sg"
    Project = var.project
    Env     = var.environment
  }
}

resource "aws_security_group_rule" "ec2" {
  security_group_id = aws_security_group.ec2_sg.id
  type              = "ingress"
  protocol          = "tcp"
  from_port         = 22
  to_port           = 22
  cidr_blocks       = ["0.0.0.0/0"]
}

1-5.EC2の設定ファイルの作成

EC2構成のファイルを作成します。

# ---------------------------------------------
# key pair
# ---------------------------------------------
resource "aws_key_pair" "keypair" {
  key_name   = "${var.project}-${var.environment}-keypair"
  public_key = file("./src/terratest-proto-keypair.pub")

  tags = {
    Name    = "${var.project}-${var.environment}-keypair"
    Project = var.project
    Env     = var.environment
  }
}

# ---------------------------------------------
# EC2 Instance
# ---------------------------------------------
resource "aws_instance" "test_server" {
  ami                         = "ami-0d4aa492f133a3068" #Amazon Linux 2023
  instance_type               = "t2.micro"
  subnet_id                   = aws_subnet.test_public_subnet_1a.id
  associate_public_ip_address = true
  vpc_security_group_ids = [
    aws_security_group.ec2_sg.id,
  ]
  key_name = aws_key_pair.keypair.key_name

  tags = {
    Name    = "${var.project}-${var.environment}-ec2"
    Project = var.project
    Env     = var.environment
  }
}

2.Terraformを使っての構築

2-1.terraform initの実施

terraform init

terrafrom_init.png

2-2.terraform planの実施

terraform planを実行することで構築対象を確認することができます。
terraform plan
terraformplan2.png

2-3.terraform applyの実施

terraform applyで構築を開始します。
terraform apply
以下の表記を確認したら「yes」を入力してEnterしてください。
teraform_apply1.png

正常終了を確認したら、AWSコンソールで構築対象のサービスの有無をチェックする。
VPC.png

EC2.png

2-4.terraform destroyの実施

AWSは従量課金制のサービスであるため作成したまま放置しておくと料金が発生してしまいます。
使用することがなければ、terraform destroy で削除しましょう。

terraform destroy
以下の表記を確認したら「yes」を入力してEnterしてください。
terraformdestroy.png

正常終了後に削除されていることを確認してください。

削除.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?