first commit
This commit is contained in:
commit
7eac6b72d9
|
@ -0,0 +1,21 @@
|
||||||
|
# ---------------------------
|
||||||
|
# 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" {
|
||||||
|
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"
|
||||||
|
tags = {
|
||||||
|
# Name = "userXX-hands-on-ec2"
|
||||||
|
Name =
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 = "user01-hands-on-ec2-sg"
|
||||||
|
description = "For EC2 Linux"
|
||||||
|
vpc_id = aws_vpc.hands_on_vpc.id
|
||||||
|
tags = {
|
||||||
|
# Name = "userXX-hands-on-ec2-sg"
|
||||||
|
Name =
|
||||||
|
}
|
||||||
|
|
||||||
|
# インバウンドルール
|
||||||
|
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 =
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# 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 =
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# Internet Gateway
|
||||||
|
# ---------------------------
|
||||||
|
resource "aws_internet_gateway" "hands_on_igw" {
|
||||||
|
vpc_id = aws_vpc.hands_on_vpc.id
|
||||||
|
tags = {
|
||||||
|
# Name = "userXX-hands-on-igw"
|
||||||
|
Name =
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# 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 =
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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