File tree Expand file tree Collapse file tree 4 files changed +40
-0
lines changed Expand file tree Collapse file tree 4 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -97,3 +97,7 @@ output "alb_secgrp_id" {
97
97
output "efs_fs_id" {
98
98
value = try (aws_efs_file_system. ecs [0 ]. id , " " )
99
99
}
100
+
101
+ output "private_key_pem" {
102
+ value = try (tls_private_key. algorithm [0 ]. private_key_pem , " " )
103
+ }
Original file line number Diff line number Diff line change @@ -289,3 +289,13 @@ variable "fargate_only" {
289
289
default = false
290
290
description = " Enable when cluster is only for fargate and does not require ASG/EC2/EFS infrastructure"
291
291
}
292
+
293
+ variable "ec2_key_enabled" {
294
+ default = false
295
+ description = " Generate a SSH private key and include in launch template of ECS nodes"
296
+ }
297
+
298
+ variable "vpn_cidr" {
299
+ default = [" 10.37.0.0/16" ]
300
+ description = " Cidr of VPN to grant ssh access to ECS nodes"
301
+ }
Original file line number Diff line number Diff line change @@ -33,7 +33,21 @@ resource "aws_launch_template" "ecs" {
33
33
34
34
user_data = base64encode (data. template_file . userdata [0 ]. rendered )
35
35
36
+ key_name = var. ec2_key_enabled ? aws_key_pair. generated_key [0 ]. key_name : null
37
+
36
38
lifecycle {
37
39
create_before_destroy = true
38
40
}
39
41
}
42
+
43
+ resource "tls_private_key" "algorithm" {
44
+ count = var. ec2_key_enabled ? 1 : 0
45
+ algorithm = " RSA"
46
+ rsa_bits = 4096
47
+ }
48
+
49
+ resource "aws_key_pair" "generated_key" {
50
+ count = var. ec2_key_enabled ? 1 : 0
51
+ key_name = " ${ var . name } -key"
52
+ public_key = tls_private_key. algorithm [0 ]. public_key_openssh
53
+ }
Original file line number Diff line number Diff line change @@ -32,6 +32,18 @@ resource "aws_security_group_rule" "all_from_alb_internal_to_ecs_nodes" {
32
32
source_security_group_id = aws_security_group. alb_internal [0 ]. id
33
33
}
34
34
35
+ resource "aws_security_group_rule" "ssh_from_vpn_to_ecs_nodes" {
36
+ count = var. ec2_key_enabled ? 1 : 0
37
+
38
+ description = " ssh from VPN"
39
+ type = " ingress"
40
+ from_port = 22
41
+ to_port = 22
42
+ protocol = " tcp"
43
+ cidr_blocks = var. vpn_cidr
44
+ security_group_id = aws_security_group. ecs_nodes . id
45
+ }
46
+
35
47
resource "aws_security_group_rule" "all_from_ecs_nodes_to_ecs_nodes" {
36
48
description = " Traffic between ECS nodes"
37
49
type = " ingress"
You can’t perform that action at this time.
0 commit comments