-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
126 lines (107 loc) · 3.36 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
121
122
123
124
125
126
provider "aws" {
region = local.region
}
# A security group to allow ssh (or other) access.
resource "aws_security_group" "ingress-ssh" {
#checkov:skip=CKV2_AWS_5:False positive. This is attached via a module and checkov can't detect it.
#checkov:skip=CKV_AWS_382:Purposeful decision.
name = "${local.name}-allow-ssh-sg"
vpc_id = data.aws_vpc.selected.id
description = "Allow ssh"
ingress {
description = "Allow inbound traffic to SSH from JUST you"
cidr_blocks = [
"${local.your_ip}/32"
]
from_port = 22
to_port = 22
protocol = "tcp"
}
egress {
description = "Allow outbound traffic to anything"
from_port = 0
to_port = 0
protocol = "-1"
#tfsec:ignore:aws-vpc-no-public-egress-sgr
cidr_blocks = ["0.0.0.0/0"]
}
}
# All the gubbins to setup a quick access key
resource "tls_private_key" "the_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = local.name
public_key = tls_private_key.the_key.public_key_openssh
}
resource "local_file" "ssh_key" {
filename = "${aws_key_pair.generated_key.key_name}.pem"
content = tls_private_key.the_key.private_key_pem
}
# Ya boi. This is what we're here for.
module "ec2" {
#checkov:skip=CKV_TF_1:Stupid Check
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> v5.7.1"
name = local.name
ami = data.aws_ami.amazon-linux-2023.id
instance_type = local.instance_type
key_name = aws_key_pair.generated_key.key_name
monitoring = true
vpc_security_group_ids = [aws_security_group.ingress-ssh.id]
subnet_id = local.subnet_id
associate_public_ip_address = local.public_ip
create_iam_instance_profile = true
iam_role_description = "IAM role for EC2 Support instance"
iam_role_policies = {
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
ami_ssm_parameter = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64"
user_data_base64 = base64encode(local.user_data)
metadata_options = {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 8
instance_metadata_tags = "enabled"
}
enable_volume_tags = true
root_block_device = [
{
encrypted = true
volume_type = "gp3"
},
]
tags = {
Terraform = "true"
Environment = "support"
}
}
module "vpc_endpoints" {
#checkov:skip=CKV_TF_1:Stupid Check
source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints"
version = "~> 5.19.0"
vpc_id = data.aws_vpc.selected.id
endpoints = { for service in toset(["ssm", "ssmmessages", "ec2messages"]) :
replace(service, ".", "_") =>
{
service = service
subnet_ids = [local.subnet_id]
private_dns_enabled = true
tags = { Name = "${local.name}-${service}" }
}
}
create_security_group = true
security_group_name_prefix = "${local.name}-vpc-endpoints-"
security_group_description = "VPC endpoint security group"
security_group_rules = {
ingress_https = {
description = "HTTPS from subnets"
cidr_blocks = [data.aws_vpc.selected.cidr_block]
}
}
tags = {
Terraform = "true"
Environment = "support"
}
}