-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.tf
160 lines (130 loc) · 3.9 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
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
# Copyright 2023 Uli Heilmeier, Vitesco Technologies
#
# SPDX-License-Identifier: Apache-2.0
resource "aws_instance" "tm-ec2-target" {
ami = data.aws_ami.ubuntu-linux.id
instance_type = "c7g.xlarge"
user_data = data.template_cloudinit_config.master.rendered
iam_instance_profile = aws_iam_instance_profile.tm_ssm_inst_profile.name
availability_zone = "${var.region}${var.region_az[var.region]}"
root_block_device {
volume_size = 30
volume_type = "gp3"
encrypted = true
}
network_interface {
network_interface_id = aws_network_interface.mgmt.id
device_index = 0
}
network_interface {
network_interface_id = aws_network_interface.capture.id
device_index = 1
}
tags = merge(var.tf_tags, {Name = "traffic-mirror-ec2"})
}
resource "aws_volume_attachment" "this_ec2" {
skip_destroy = true
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.this.id
instance_id = aws_instance.tm-ec2-target.id
}
resource "aws_ebs_volume" "this" {
availability_zone = "${var.region}${var.region_az[var.region]}"
size = 100
type = "gp3"
encrypted = true
tags = merge(var.tf_tags, {Name = "traffic-mirror-ebs"})
}
resource "aws_security_group" "ssh-vxlan" {
name = "allow_ssh_vxlan_traffic_mirror-${var.region}"
description = "Allow SSH and VXLAN inbound traffic"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.network_ranges
}
ingress {
description = "VXLAN"
from_port = 4789
to_port = 4789
protocol = "udp"
cidr_blocks = var.network_ranges
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
vpc_id = var.region_vpc_id[var.region]
tags = merge(var.tf_tags, {Name = "traffic-mirror-sg"})
}
resource "aws_network_interface" "mgmt" {
subnet_id = var.region_subnet_id[var.region]
security_groups = [aws_security_group.ssh-vxlan.id]
tags = merge(var.tf_tags, {Name = "traffic-mirror-mgmt-if"})
}
resource "aws_network_interface" "capture" {
subnet_id = var.region_subnet_id[var.region]
security_groups = [aws_security_group.ssh-vxlan.id]
tags = merge(var.tf_tags, {Name = "traffic-mirror-capture-if"})
}
# Create EC2 Instance Role
resource "aws_iam_role" "ssm_s3_role" {
name = "tm-traffic-mirror-ssm-role-${var.region}"
path = "/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = merge(var.tf_tags, {Name = "traffic-mirror-ec2-role"})
}
resource "aws_iam_instance_profile" "tm_ssm_inst_profile" {
name = "tm-traffic-mirror-ssm-inst-profile-${var.region}"
role = aws_iam_role.ssm_s3_role.name
}
data "aws_iam_policy" "AmazonSSMManagedInstanceCore" {
arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy_attachment" "SSM-role-policy-attach" {
role = aws_iam_role.ssm_s3_role.name
policy_arn = data.aws_iam_policy.AmazonSSMManagedInstanceCore.arn
}
resource "aws_iam_policy" "s3_access" {
name = "tm-traffic-mirror-s3-policy"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "allowS3Access",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::${aws_s3_bucket.traffic_mirror_s3.id}",
"arn:aws:s3:::${aws_s3_bucket.traffic_mirror_s3.id}/*"
]
}
]
})
}
resource "aws_iam_role_policy_attachment" "s3_attach" {
role = aws_iam_role.ssm_s3_role.name
policy_arn = aws_iam_policy.s3_access.arn
}