From 47e349485ea2841cdbb6ea478a01174a22acd30d 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:07:53 +0900 Subject: [PATCH] first commit --- README.md | 3 +++ ec2.tf | 24 ++++++++++++++++++++ main.tf | 16 ++++++++++++++ output.tf | 4 ++++ securitygroup.tf | 28 ++++++++++++++++++++++++ vpc.tf | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 132 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..b5dd174 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# provisioning-0 + +ハンズオン参加者向けのTerraform環境構築用スクリプト \ No newline at end of file diff --git a/ec2.tf b/ec2.tf new file mode 100644 index 0000000..790ccd0 --- /dev/null +++ b/ec2.tf @@ -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)}" + } +} 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..aaad363 --- /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..46a88b0 --- /dev/null +++ b/securitygroup.tf @@ -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"] + } +} diff --git a/vpc.tf b/vpc.tf new file mode 100644 index 0000000..d28b1e6 --- /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 = "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 +}