-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
70 lines (58 loc) · 2.1 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
# AMI
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = [local.ubuntu_ami_name_latest]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = [local.canonical_account_id]
}
# Security groups
resource "aws_security_group" "blue_sg" {
name = "blue_sg"
vpc_id = var.vpc_id
description = "Security Group to allow outbound SSH to the orange instance from the blue instance"
}
resource "aws_vpc_security_group_egress_rule" "blue-outbound-ssh" {
security_group_id = aws_security_group.blue_sg.id
description = "Allow outbound SSH traffic from blue EC2 instance to orange EC2 instance"
referenced_security_group_id = aws_security_group.orange_sg.id
from_port = 22
to_port = 22
ip_protocol = "tcp"
}
resource "aws_security_group" "orange_sg" {
name = "orange_sg"
vpc_id = var.vpc_id
description = "Security Group to allow inbound SSH from the blue instance to the orange instance"
}
resource "aws_vpc_security_group_ingress_rule" "orange-inbound-ssh" {
security_group_id = aws_security_group.orange_sg.id
description = "Allow inbound SSH traffic from blue EC2 instance to orange EC2 instance"
referenced_security_group_id = aws_security_group.blue_sg.id
from_port = 22
to_port = 22
ip_protocol = "tcp"
}
# SSH key pair
resource "aws_key_pair" "fun_key" {
key_name = "fun_key"
public_key = file(var.public_key_path)
}
# EC2
resource "aws_instance" "ec2_instance" {
for_each = local.aws_instances
ami = data.aws_ami.ubuntu.id
instance_type = var.ec2_instance_type
associate_public_ip_address = false
subnet_id = var.subnet_id
vpc_security_group_ids = each.value.ssh_reachable ? [aws_security_group.orange_sg.id] : [aws_security_group.blue_sg.id]
key_name = aws_key_pair.fun_key.key_name
tags = {
Name = each.key
}
}