From 7eac6b72d9a3f5e8ef6c436e7759af9a86b6cbcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=A4=E5=B7=9D=20=E4=B8=80=E4=B9=8B=E8=BC=94?= <8100076@AS-RHEL.wb.local> Date: Thu, 3 Jul 2025 17:08:46 +0900 Subject: [PATCH] first commit --- README.md | 2 ++ ec2.tf | 21 ++++++++++++++++++ main.tf | 16 ++++++++++++++ output.tf | 4 ++++ securitygroup.tf | 28 ++++++++++++++++++++++++ vpc.tf | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 128 insertions(+) create mode 100644 README.md create mode 100644 ec2.tf create mode 100644 main.tf create mode 100644 output.tf create mode 100644 securitygroup.tf create mode 100644 vpc.tf diff --git a/README.md b/README.md new file mode 100644 index 0000000..c51389f --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# provisioning-1 + diff --git a/ec2.tf b/ec2.tf new file mode 100644 index 0000000..67e7087 --- /dev/null +++ b/ec2.tf @@ -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 = + } +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..89d9fbe --- /dev/null +++ b/main.tf @@ -0,0 +1,16 @@ +# --------------------------- +# プロバイダ設定 +# --------------------------- +# AWS +provider "aws" { + region = "ap-northeast-1" +} + +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.54.1" + } + } +} diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..c60a4b1 --- /dev/null +++ b/output.tf @@ -0,0 +1,4 @@ +# 作成したEC2のパブリックIPアドレスを出力 +output "ec2_global_ips" { + value = aws_instance.hands_on_ec2.public_ip +} diff --git a/securitygroup.tf b/securitygroup.tf new file mode 100644 index 0000000..3b2a604 --- /dev/null +++ b/securitygroup.tf @@ -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"] + } +} diff --git a/vpc.tf b/vpc.tf new file mode 100644 index 0000000..35cf7cb --- /dev/null +++ b/vpc.tf @@ -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 +}