-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
167 lines (140 loc) · 3.43 KB
/
main.tf
File metadata and controls
167 lines (140 loc) · 3.43 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.0"
}
provider "aws" {
region = var.aws_region
}
# VPC
resource "aws_vpc" "eticaret_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "eticaret-vpc"
}
}
# Internet Gateway
resource "aws_internet_gateway" "eticaret_igw" {
vpc_id = aws_vpc.eticaret_vpc.id
tags = {
Name = "eticaret-igw"
}
}
# Public Subnet
resource "aws_subnet" "eticaret_public_subnet" {
vpc_id = aws_vpc.eticaret_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = true
tags = {
Name = "eticaret-public-subnet"
}
}
# Route Table
resource "aws_route_table" "eticaret_public_rt" {
vpc_id = aws_vpc.eticaret_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.eticaret_igw.id
}
tags = {
Name = "eticaret-public-rt"
}
}
# Route Table Association
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.eticaret_public_subnet.id
route_table_id = aws_route_table.eticaret_public_rt.id
}
# Security Group
resource "aws_security_group" "eticaret_sg" {
name = "eticaret-sg"
description = "Security group for e-ticaret API"
vpc_id = aws_vpc.eticaret_vpc.id
# SSH
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# API Port
ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# MongoDB Port (sadece VPC içinden)
ingress {
from_port = 27017
to_port = 27017
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
# All outbound traffic
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "eticaret-sg"
}
}
# Key Pair
resource "aws_key_pair" "eticaret_key" {
key_name = "eticaret-key"
public_key = file(var.public_key_path)
}
# EC2 Instance
resource "aws_instance" "eticaret_server" {
ami = data.aws_ami.amazon_linux.id
instance_type = "t2.micro" # Free tier
key_name = aws_key_pair.eticaret_key.key_name
vpc_security_group_ids = [aws_security_group.eticaret_sg.id]
subnet_id = aws_subnet.eticaret_public_subnet.id
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y docker git
systemctl start docker
systemctl enable docker
usermod -aG docker ec2-user
# Docker Compose kurulum
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
# Node.js kurulum
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
yum install -y nodejs
EOF
tags = {
Name = "eticaret-server"
}
}
# Data sources
data "aws_availability_zones" "available" {
state = "available"
}
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
} # Terraform guncellemesi