キラキラ系エンジニアへの第1歩 4日目(AWS基礎の基礎。Webアプリ基本構成 Part4)

キラキラ系エンジニアとは

こちらを参照ください。

今日のアップデート

前回は、こちらのページを元にVPCまで作成しました。

投稿内容にはありませんが、サブネットも引き続き実行したところ問題無く作成できました。

コードは以下です。このページのやつをそのまま貼り付けてterraform planとterraform applyを実行したら問題なくサブネットが作成できました。

# Subnet
# https://www.terraform.io/docs/providers/aws/r/subnet.html
resource "aws_subnet" "public_1a" {
  # 先程作成したVPCを参照し、そのVPC内にSubnetを立てる
  vpc_id = "${aws_vpc.main.id}"

  # Subnetを作成するAZ
  availability_zone = "ap-northeast-1a"

  cidr_block        = "10.0.1.0/24"

  tags = {
    Name = "handson-public-1a"
  }
}

resource "aws_subnet" "public_1c" {
  vpc_id = "${aws_vpc.main.id}"

  availability_zone = "ap-northeast-1c"

  cidr_block        = "10.0.2.0/24"

  tags = {
    Name = "handson-public-1c"
  }
}

resource "aws_subnet" "public_1d" {
  vpc_id = "${aws_vpc.main.id}"

  availability_zone = "ap-northeast-1d"

  cidr_block        = "10.0.3.0/24"

  tags = {
    Name = "handson-public-1d"
  }
}

# Private Subnets
resource "aws_subnet" "private_1a" {
  vpc_id = "${aws_vpc.main.id}"

  availability_zone = "ap-northeast-1a"
  cidr_block        = "10.0.10.0/24"

  tags = {
    Name = "handson-private-1a"
  }
}

resource "aws_subnet" "private_1c" {
  vpc_id = "${aws_vpc.main.id}"

  availability_zone = "ap-northeast-1c"
  cidr_block        = "10.0.20.0/24"

  tags = {
    Name = "handson-private-1c"
  }
}

resource "aws_subnet" "private_1d" {
  vpc_id = "${aws_vpc.main.id}"

  availability_zone = "ap-northeast-1d"
  cidr_block        = "10.0.30.0/24"

  tags = {
    Name = "handson-private-1d"
  }
}

今回は、Internet Getwayのところから進めます。

んで、やってみたらネットワークリソースは全てできたのだが、

肝心のEC2をどうやってやるんだってとこがよくわかんなくなり、

方向転換しこちらをやってみました。

terraformコードは以下

# AWSプロバイダの定義
provider "aws" {
  region = "ap-northeast-1"
  access_key = "xxxxxxxxxxxxxxxxxxxx"
  secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

# VPC
resource "aws_vpc" "tfweb_vpc" {
  cidr_block           = "10.100.0.0/16"
  instance_tenancy     = "default"
  enable_dns_hostnames = true
  enable_dns_support   = true
}

# サブネット
resource "aws_subnet" "tfweb_subnet" {
  vpc_id                  = "${aws_vpc.tfweb_vpc.id}"
  cidr_block              = "10.100.0.0/24"
  availability_zone       = "ap-northeast-1a"
  map_public_ip_on_launch = true
}

# Internet Gateway
resource "aws_internet_gateway" "tfweb_igw" {
  vpc_id = "${aws_vpc.tfweb_vpc.id}"
}

# Route Table
resource "aws_route_table" "tfweb_rt" {
  vpc_id = "${aws_vpc.tfweb_vpc.id}"

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

# Associate route table with subnet
resource "aws_route_table_association" "tfweb_rt_association" {
  subnet_id      = aws_subnet.tfweb_subnet.id
  route_table_id = aws_route_table.tfweb_rt.id
}

# EC2 instance
resource "aws_instance" "tfweb_ec2" {
  ami                         = "ami-06098fd00463352b6" # AmazonLinux 2 (x86)
  instance_type               = "t2.micro"
  subnet_id                   = aws_subnet.tfweb_subnet.id
  key_name                    = "xxx" # こちらを見ながら作成

  vpc_security_group_ids = [
    aws_security_group.tfweb_sg_web.id,
    aws_security_group.tfweb_sg_ssh.id
  ]

  user_data = <<EOF
  #!/bin/bash
  yum install -y httpd
  systemctl start httpd.service
  EOF
}

# Security Group
resource "aws_security_group" "tfweb_sg_web" {
  name   = "tfweb_sg_web"
  vpc_id = aws_vpc.tfweb_vpc.id

  ingress {
    description = "allow http"
    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"]
  }
}

resource "aws_security_group" "tfweb_sg_ssh" {
  name   = "tfweb_sg_ssh"
  vpc_id = aws_vpc.tfweb_vpc.id

  ingress {
    description = "allow ssh"
    from_port   = "22"
    to_port     = "22"
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"] # 必要に応じIP制限を設定してください
  }

  egress {
    from_port   = "0"
    to_port     = "0"
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

上記をterraform planしterraform applyしました。

次に以前作成したReactの計算アプリがGithubに上がっているので

こちらを見ながらAmplifyでデプロイしたら簡単にできました!

今日は、中身の解説が無いですが、

  1. 中身(terraformソース)の深掘り
  2. ロードバランサが効いているのか

などを確認できたらいいなと思います。

ここまで。。

さくっと調べたいこと

  1. DX検定
  2. パワポに代わるもの
  3. 底辺系エンジニアのコミュニティってどうだろう

しっかり調べたいこと

  1. 5分又は10分LTのパワポ粒度とネタ
  2. Qiitaとブログの相互リンク

気になっているワード

  1. DX
  2. DX検定
  3. 統計学、Python検定
  4. ITストラテジスト
  5. 中小企業診断士
  6. データエンジニア

毎日言葉にして言います。

以下のことを毎日ペンでノートに書いて、口に出して言います。

  1. 今日の自分を明日の自分が超えていく
  2. 失敗、挫折してきたことは自慢できる
  3. 失敗を恐れない。成功ではなく成長にフォーカス。努力は100%自分を成長させる
  4. 3Y(よし!よっしゃ!やるぞ!)
  5. 決して諦めない