-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_aws.tf
More file actions
219 lines (180 loc) · 4.74 KB
/
main_aws.tf
File metadata and controls
219 lines (180 loc) · 4.74 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
terraform {
// aws 라이브러리 불러옴
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
# AWS 설정 시작
provider "aws" {
region = var.region
}
# AWS 설정 끝
# VPC 설정 시작
resource "aws_vpc" "vpc_1" {
cidr_block = "10.0.0.0/16"
# 무조건 켜세요.
enable_dns_support = true
# 무조건 켜세요.
enable_dns_hostnames = true
tags = {
Name = "${var.prefix}-vpc-1"
}
}
resource "aws_subnet" "subnet_1" {
vpc_id = aws_vpc.vpc_1.id
cidr_block = "10.0.1.0/24"
availability_zone = "${var.region}a"
map_public_ip_on_launch = true
tags = {
Name = "${var.prefix}-subnet-1"
}
}
resource "aws_subnet" "subnet_2" {
vpc_id = aws_vpc.vpc_1.id
cidr_block = "10.0.2.0/24"
availability_zone = "${var.region}b"
map_public_ip_on_launch = true
tags = {
Name = "${var.prefix}-subnet-2"
}
}
resource "aws_subnet" "subnet_3" {
vpc_id = aws_vpc.vpc_1.id
cidr_block = "10.0.3.0/24"
availability_zone = "${var.region}c"
map_public_ip_on_launch = true
tags = {
Name = "${var.prefix}-subnet-3"
}
}
resource "aws_internet_gateway" "igw_1" {
vpc_id = aws_vpc.vpc_1.id
tags = {
Name = "${var.prefix}-igw-1"
}
}
resource "aws_route_table" "rt_1" {
vpc_id = aws_vpc.vpc_1.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw_1.id
}
tags = {
Name = "${var.prefix}-rt-1"
}
}
resource "aws_route_table_association" "association_1" {
subnet_id = aws_subnet.subnet_1.id
route_table_id = aws_route_table.rt_1.id
}
resource "aws_route_table_association" "association_2" {
subnet_id = aws_subnet.subnet_2.id
route_table_id = aws_route_table.rt_1.id
}
resource "aws_route_table_association" "association_3" {
subnet_id = aws_subnet.subnet_3.id
route_table_id = aws_route_table.rt_1.id
}
resource "aws_security_group" "sg_1" {
name = "${var.prefix}-sg-1"
ingress {
from_port = 0
to_port = 0
protocol = "all"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "all"
cidr_blocks = ["0.0.0.0/0"]
}
vpc_id = aws_vpc.vpc_1.id
tags = {
Name = "${var.prefix}-sg-1"
}
}
# EC2 설정 시작
# EC2 역할 생성
resource "aws_iam_role" "ec2_role_1" {
name = "${var.prefix}-ec2-role-1"
# 이 역할에 대한 신뢰 정책 설정. EC2 서비스가 이 역할을 가정할 수 있도록 설정
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
# EC2 역할에 AmazonS3FullAccess 정책을 부착
resource "aws_iam_role_policy_attachment" "s3_full_access" {
role = aws_iam_role.ec2_role_1.name
policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"
}
# EC2 역할에 AmazonEC2RoleforSSM 정책을 부착
resource "aws_iam_role_policy_attachment" "ec2_ssm" {
role = aws_iam_role.ec2_role_1.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
}
# IAM 인스턴스 프로파일 생성
resource "aws_iam_instance_profile" "instance_profile_1" {
name = "${var.prefix}-instance-profile-1"
role = aws_iam_role.ec2_role_1.name
}
locals {
ec2_user_data_base = <<-END_OF_FILE
#!/bin/bash
yum install docker -y
systemctl enable docker
systemctl start docker
curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
yum install git -y
sudo dd if=/dev/zero of=/swapfile bs=128M count=32
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s
sudo sh -c 'echo "/swapfile swap swap defaults 0 0" >> /etc/fstab'
END_OF_FILE
}
# EC2 인스턴스 생성
resource "aws_instance" "ec2_1" {
# 사용할 AMI ID
ami = "ami-04c596dcf23eb98d8"
# EC2 인스턴스 유형
instance_type = "t2.micro"
# 사용할 서브넷 ID
subnet_id = aws_subnet.subnet_1.id
# 적용할 보안 그룹 ID
vpc_security_group_ids = [aws_security_group.sg_1.id]
# 퍼블릭 IP 연결 설정
associate_public_ip_address = true
# 인스턴스에 IAM 역할 연결
iam_instance_profile = aws_iam_instance_profile.instance_profile_1.name
# 인스턴스에 태그 설정
tags = {
Name = "${var.prefix}-ec2-1"
}
# 루트 볼륨 설정
root_block_device {
volume_type = "gp3"
volume_size = 30 # 볼륨 크기를 30GB로 설정
}
# User data script for ec2_1
user_data = <<-EOF
${local.ec2_user_data_base}
EOF
}