first commit
This commit is contained in:
commit
47e349485e
|
@ -0,0 +1,24 @@
|
|||
# ---------------------------
|
||||
# EC2
|
||||
# ---------------------------
|
||||
# Amazon Linux2023のAMIを取得
|
||||
data "aws_ssm_parameter" "amazon_linux2023" {
|
||||
name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64"
|
||||
}
|
||||
|
||||
# EC2作成
|
||||
resource "aws_instance" "hands_on_ec2" {
|
||||
# 作成するEC2インスタンスの数
|
||||
count = 3
|
||||
ami = data.aws_ssm_parameter.amazon_linux2023.value
|
||||
instance_type = "t2.micro"
|
||||
availability_zone = "ap-northeast-1a"
|
||||
vpc_security_group_ids = [aws_security_group.hands_on_ec2_sg.id]
|
||||
subnet_id = aws_subnet.hands_on_public_1a_sn.id
|
||||
associate_public_ip_address = "true"
|
||||
# user_data = file("./install_terraform.sh")
|
||||
tags = {
|
||||
# Name = "userXX-hands-on-ec2"
|
||||
Name = "${format("user%02d-provisioning-ec2", count.index + 1)}"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
# ---------------------------
|
||||
# プロバイダ設定
|
||||
# ---------------------------
|
||||
# AWS
|
||||
provider "aws" {
|
||||
region = "ap-northeast-1"
|
||||
}
|
||||
|
||||
terraform {
|
||||
required_providers {
|
||||
aws = {
|
||||
source = "hashicorp/aws"
|
||||
version = "~> 5.54.1"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
# 作成したEC2のパブリックIPアドレスを出力
|
||||
# output "ec2_global_ips" {
|
||||
# value = aws_instance.hands_on_ec2.public_ip
|
||||
# }
|
|
@ -0,0 +1,28 @@
|
|||
# ---------------------------
|
||||
# Security Group
|
||||
# ---------------------------
|
||||
resource "aws_security_group" "hands_on_ec2_sg" {
|
||||
name = "provisioning-ec2-sg"
|
||||
description = "For EC2 Linux"
|
||||
vpc_id = aws_vpc.hands_on_vpc.id
|
||||
tags = {
|
||||
# Name = "userXX-hands-on-ec2-sg"
|
||||
Name = "provisioning-ec2-sg"
|
||||
}
|
||||
|
||||
# インバウンドルール
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["3.112.23.0/29"]
|
||||
}
|
||||
|
||||
# アウトバウンドルール
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
# ---------------------------
|
||||
# VPC
|
||||
# ---------------------------
|
||||
resource "aws_vpc" "hands_on_vpc" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
enable_dns_hostnames = true # DNSホスト名を有効化
|
||||
tags = {
|
||||
# Name = "userXX-hands-on-vpc"
|
||||
Name = "provisioning-vpc"
|
||||
}
|
||||
}
|
||||
|
||||
# ---------------------------
|
||||
# Subnet
|
||||
# ---------------------------
|
||||
resource "aws_subnet" "hands_on_public_1a_sn" {
|
||||
vpc_id = aws_vpc.hands_on_vpc.id
|
||||
cidr_block = "10.0.1.0/24"
|
||||
availability_zone = "ap-northeast-1a"
|
||||
tags = {
|
||||
# Name = "userXX-hands-on-public-1a-sn"
|
||||
Name = "provisioning-public-1a-sn"
|
||||
}
|
||||
}
|
||||
|
||||
# ---------------------------
|
||||
# Internet Gateway
|
||||
# ---------------------------
|
||||
resource "aws_internet_gateway" "hands_on_igw" {
|
||||
vpc_id = aws_vpc.hands_on_vpc.id
|
||||
tags = {
|
||||
# Name = "userXX-hands-on-igw"
|
||||
Name = "provisioning-igw"
|
||||
}
|
||||
}
|
||||
|
||||
# ---------------------------
|
||||
# Route table
|
||||
# ---------------------------
|
||||
# Route table作成
|
||||
resource "aws_route_table" "hands_on_public_rt" {
|
||||
vpc_id = aws_vpc.hands_on_vpc.id
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.hands_on_igw.id
|
||||
}
|
||||
tags = {
|
||||
# Name = "userXX-hands-on-public-rt"
|
||||
Name = "provisioning-public-rtb"
|
||||
}
|
||||
}
|
||||
|
||||
# SubnetとRoute tableの関連付け
|
||||
resource "aws_route_table_association" "hands_on_public_rt_associate" {
|
||||
subnet_id = aws_subnet.hands_on_public_1a_sn.id
|
||||
route_table_id = aws_route_table.hands_on_public_rt.id
|
||||
}
|
Loading…
Reference in New Issue