-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
120 lines (98 loc) · 2.57 KB
/
main.tf
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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.2.0"
}
}
}
provider "aws" {
region = "eu-central-1"
access_key = var.access_key
secret_key = var.secret_key
}
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/24"
tags = {
Name = "${var.environment}-${var.application_name}-vpc"
Location = "${var.location}"
Environment = "${var.environment}"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.0.0/24"
availability_zone = "eu-central-1a"
map_public_ip_on_launch = true
tags = {
Name = "${var.environment}-${var.application_name}-public_subnet"
Location = "${var.location}"
Environment = "${var.environment}"
}
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "${var.environment}-${var.application_name}-igw"
Location = "${var.location}"
Environment = "${var.environment}"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
route {
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "${var.environment}-${var.application_name}-route-table-public"
Location = "${var.location}"
Environment = "${var.environment}"
}
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public.id
}
resource "aws_security_group" "web_sg" {
name = "SSH"
vpc_id = aws_vpc.vpc.id
ingress {
from_port = 22
to_port = 22
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 = "${var.environment}-${var.application_name}-web-sg"
Location = "${var.location}"
Environment = "${var.environment}"
}
}
resource "aws_key_pair" "leon" {
key_name = "leon-key"
public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFul1dKr5rwuqpkj2uucuh0x4QLyASfyvyddqTDXYKGD"
}
resource "aws_instance" "public_instance" {
count = 4
ami = "ami-0b0c5a84b89c4bf99"
instance_type = "t3.small"
key_name = aws_key_pair.leon.key_name
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.web_sg.id]
tags = {
Name = "${var.environment}-${var.application_name}-${count.index}"
Location = "${var.location}"
Environment = "${var.environment}"
}
}