テラフォームでAWS構築

テラフォームでAWSを構築していきたいと思います。条件としては以下のようになります

条件

  • VPC:新規で作成
  • ゾーン:ap-northeast-1a
  • サブネット:10.0.2.0/24
  • エラスティックIP:自動取得
  • セキュリティー:22番、80番、443番許可
  • OS:AmazonLinux2
  • CPU:インテル系
  • インスタンス:t2.micro
  • EBS:30GB

となります

main.cf

001# 変数定義
002variable "aws_access_key" {}
003variable "aws_secret_key" {}
004variable "aws_region" {}
005variable "aws_vpc_cidr" {}
006variable "aws_vpc_tags_name" {}
007 
008# Provider
009provider "aws" {
010  version    = "4.5.0"
011  access_key = var.aws_access_key
012  secret_key = var.aws_secret_key
013  region     = var.aws_region
014}
015 
016# VPC の作成
017resource "aws_vpc" "main" {
018  cidr_block           = var.aws_vpc_cidr
019  instance_tenancy     = "default"
020  enable_dns_support   = "true"
021  enable_dns_hostnames = "true"
022 
023  tags = {
024    Name = var.aws_vpc_tags_name
025  }
026}
027 
028# subnetの作成
029resource "aws_subnet" "public_1a" {
030  # 先程作成したVPCを参照し、そのVPC内にSubnetを立てる
031  vpc_id = "${aws_vpc.main.id}"
032 
033  # Subnetを作成するAZ
034  availability_zone = "ap-northeast-1a"
035 
036  cidr_block        = "10.0.2.0/24"
037 
038  tags = {
039    Name = "terraform-subnet"
040  }
041}
042 
043#ゲートウェイの作成
044resource "aws_internet_gateway" "main" {
045  vpc_id = "${aws_vpc.main.id}"
046 
047  tags = {
048    Name = "terraform-gateway"
049  }
050}
051 
052 
053# ルートテーブル
054resource "aws_route_table" "public" {
055  vpc_id = "${aws_vpc.main.id}"
056 
057  tags = {
058    Name = "terraform-public"
059  }
060}
061 
062# ルート
063resource "aws_route" "public" {
064  destination_cidr_block = "0.0.0.0/0"
065  route_table_id         = "${aws_route_table.public.id}"
066  gateway_id             = "${aws_internet_gateway.main.id}"
067}
068 
069resource "aws_route_table_association" "public_1a" {
070  subnet_id      = "${aws_subnet.public_1a.id}"
071  route_table_id = "${aws_route_table.public.id}"
072}
073 
074 
075# Security Group作成
076resource "aws_security_group" "handson_ec2_sg" {
077  name              = "terraform-handson-ec2-sg"
078  description       = "For EC2 Linux"
079  vpc_id            = aws_vpc.main.id
080  tags = {
081    Name = "terraform-handson-ec2-sg"
082  }
083 
084 
085  # アウトバウンドルール
086  egress {
087    from_port   = 0
088    to_port     = 0
089    protocol    = "-1"
090    cidr_blocks = ["0.0.0.0/0"]
091  }
092}
093 
094resource "aws_security_group_rule" "inbound_http" {
095  type        = "ingress"
096  from_port   = 80
097  to_port     = 80
098  protocol    = "tcp"
099  cidr_blocks = [
100    "0.0.0.0/0"
101  ]
102 
103  # ここでweb_serverセキュリティグループに紐付け
104  security_group_id = "${aws_security_group.handson_ec2_sg.id}"
105}
106 
107# 443
108resource "aws_security_group_rule" "inbound_https" {
109  type        = "ingress"
110  from_port   = 443
111  to_port     = 443
112  protocol    = "tcp"
113  cidr_blocks = [
114    "0.0.0.0/0"
115  ]
116 
117  # ここでweb_serverセキュリティグループに紐付け
118  security_group_id = "${aws_security_group.handson_ec2_sg.id}"
119}
120 
121# 22番
122resource "aws_security_group_rule" "inbound_ssh" {
123  type        = "ingress"
124  from_port   = 22
125  to_port     = 22
126  protocol    = "tcp"
127  cidr_blocks = [
128    "0.0.0.0/0"
129  ]
130 
131  # ここでweb_serverセキュリティグループに紐付け
132  security_group_id = "${aws_security_group.handson_ec2_sg.id}"
133}

ec2.tf

01# EC2の作成
02 
03# ---------------------------
04# EC2
05# ---------------------------
06# Amazon Linux 2 の最新版AMIを取得
07data "aws_ssm_parameter" "amzn2_latest_ami" {
08  name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-gp2"
09  #name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-2.0.20220912.1-arm64-gp2"
10}
11 
12# EC2作成
13resource "aws_instance" "handson_ec2"{
14  ami                         = data.aws_ssm_parameter.amzn2_latest_ami.value
15  instance_type               = "t2.micro"
16  #availability_zone           = ap-northeast-1a
17  vpc_security_group_ids      = [aws_security_group.handson_ec2_sg.id]
18  subnet_id                   = aws_subnet.public_1a.id
19  associate_public_ip_address = "true"
20  key_name                    = "terraform"
21 
22  #EBSのデフォルトの8GBを変更
23  root_block_device {
24  volume_type = "gp2"
25  volume_size = 30
26  }
27  tags = {
28    Name = "terraform-handson-ec2"
29  }
30}
31 
32#エラスティックIPの設定
33resource "aws_eip" "public_ip" {
34  instance = aws_instance.handson_ec2.id
35  vpc      = true
36  tags = {
37    Name = "terraform-public_ip"
38  }
39}

個人支援・寄付について

サイトラボでは個人支援・寄付を受けております。ご協力いただける方はお願いいたします。当サイトではビットコインで受け付けております。

  • ビットコイン:3LHnADwZwUbic2L45EnVJEykiG6KfbqrwS