-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2.tf
97 lines (88 loc) · 2.13 KB
/
ec2.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
#Create a public key
resource "aws_key_pair" "TF_key" {
key_name = "TF_key"
public_key = tls_private_key.rsa.public_key_openssh
}
#Create a private key to store in machine
resource "tls_private_key" "rsa" {
algorithm = "RSA"
rsa_bits = 4096
}
#Create a local file
resource "local_file" "TF-key" {
content = tls_private_key.rsa.private_key_pem
filename = "tfkey"
}
resource "aws_security_group" "bastion" {
name_prefix = "bastion"
vpc_id = aws_vpc.vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [aws_vpc.vpc.cidr_block]
}
}
resource "aws_security_group" "ssh" {
name_prefix = "ssh"
vpc_id = aws_vpc.vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [aws_vpc.vpc.cidr_block]
}
}
#Baston and private subnet defined for ec2
resource "aws_subnet" "bastion" {
cidr_block = "10.0.3.0/24"
vpc_id = aws_vpc.vpc.id
tags = {
Name = "bastion-subnet"
}
}
resource "aws_subnet" "private" {
cidr_block = "10.0.4.0/24"
vpc_id = aws_vpc.vpc.id
tags = {
Name = "private-subnet"
}
}
#ec2 instance
resource "aws_instance" "bastion" {
ami = "ami-0aa2b7722dc1b5612"
instance_type = "t2.micro"
subnet_id = aws_subnet.bastion.id
key_name = aws_key_pair.TF_key.id
vpc_security_group_ids = [
aws_security_group.bastion.id,
aws_security_group.ssh.id,
]
tags = {
Name = "bastion-server"
}
}
resource "aws_instance" "jenkins" {
ami = "ami-0aa2b7722dc1b5612"
instance_type = "t2.micro"
subnet_id = aws_subnet.private.id
key_name = aws_key_pair.TF_key.id
vpc_security_group_ids = [
aws_security_group.ssh.id,
]
tags = {
Name = "jenkins-server"
}
}
resource "aws_instance" "app" {
ami = "ami-0aa2b7722dc1b5612"
instance_type = "t2.micro"
subnet_id = aws_subnet.private.id
key_name = aws_key_pair.TF_key.id
vpc_security_group_ids = [
aws_security_group.ssh.id,
]
tags = {
Name = "app-server"
}
}