-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaws-test.tf
More file actions
128 lines (107 loc) · 2.44 KB
/
aws-test.tf
File metadata and controls
128 lines (107 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# AWS VPC와 EC2 인스턴스 생성 테스트
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# AWS Provider 설정
provider "aws" {
region = "ap-northeast-2" # 서울 리전
}
# VPC 생성
resource "aws_vpc" "test_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "test-vpc"
}
}
# 인터넷 게이트웨이
resource "aws_internet_gateway" "test_igw" {
vpc_id = aws_vpc.test_vpc.id
tags = {
Name = "test-igw"
}
}
# 서브넷
resource "aws_subnet" "test_subnet" {
vpc_id = aws_vpc.test_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-northeast-2a"
map_public_ip_on_launch = true
tags = {
Name = "test-subnet"
}
}
# 라우팅 테이블
resource "aws_route_table" "test_rt" {
vpc_id = aws_vpc.test_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.test_igw.id
}
tags = {
Name = "test-rt"
}
}
# 라우팅 테이블 연결
resource "aws_route_table_association" "test_rta" {
subnet_id = aws_subnet.test_subnet.id
route_table_id = aws_route_table.test_rt.id
}
# 보안 그룹
resource "aws_security_group" "test_sg" {
name = "test-security-group"
description = "Test security group for EC2"
vpc_id = aws_vpc.test_vpc.id
# SSH 접속 허용
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP 접속 허용
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# 모든 아웃바운드 트래픽 허용
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "test-sg"
}
}
# EC2 인스턴스
resource "aws_instance" "test_instance" {
ami = "ami-0c9c942bd7bf113a2" # Amazon Linux 2023 AMI (서울 리전)
instance_type = "t2.micro"
subnet_id = aws_subnet.test_subnet.id
vpc_security_group_ids = [aws_security_group.test_sg.id]
tags = {
Name = "test-instance"
}
}
# 출력
output "vpc_id" {
value = aws_vpc.test_vpc.id
}
output "instance_id" {
value = aws_instance.test_instance.id
}
output "instance_public_ip" {
value = aws_instance.test_instance.public_ip
}