File tree 5 files changed +64
-8
lines changed
5 files changed +64
-8
lines changed Original file line number Diff line number Diff line change 1
1
# AWS SSM resources
2
2
resource "aws_iam_role" "this" {
3
- name = " ${ var . env } -openvpn-connector "
3
+ name = local . name
4
4
assume_role_policy = data. aws_iam_policy_document . this . json
5
5
}
6
6
@@ -10,7 +10,7 @@ resource "aws_iam_role_policy_attachment" "this" {
10
10
}
11
11
12
12
resource "aws_iam_instance_profile" "this" {
13
- name = " ${ var . env } -openvpn-connector "
13
+ name = local . name
14
14
role = aws_iam_role. this . name
15
15
}
16
16
Original file line number Diff line number Diff line change 1
1
# Security Groups
2
2
resource "aws_security_group" "this" {
3
3
count = var. enabled ? 1 : 0
4
- name = " ${ var . env } -openvpn-connector "
5
- description = " Security Group for Cloud OpenVPN EC2 Instance (connector)"
4
+ name = local . name
5
+ description = " Security Group for Cloud OpenVPN+Bastion EC2 Instance (connector)"
6
6
vpc_id = var. vpc_id
7
7
8
8
ingress {
@@ -24,7 +24,7 @@ resource "aws_security_group" "this" {
24
24
tags = {
25
25
Terraform = " true"
26
26
Env = var.env
27
- Name = " ${ var . env } -openvpn-connector "
27
+ Name = local.name
28
28
}
29
29
30
30
lifecycle {
@@ -40,9 +40,12 @@ resource "aws_instance" "this" {
40
40
iam_instance_profile = aws_iam_instance_profile. this . name
41
41
subnet_id = var. private_subnets [0 ]
42
42
key_name = var. ec2_key_pair_name
43
- vpc_security_group_ids = [aws_security_group . this [0 ]. id ]
43
+ vpc_security_group_ids = concat (var. ext_security_groups , [
44
+ aws_security_group . this [0 ]. id
45
+ ])
44
46
45
- disable_api_termination = true
47
+ disable_api_termination = true
48
+ associate_public_ip_address = false
46
49
47
50
lifecycle {
48
51
ignore_changes = all
@@ -53,7 +56,7 @@ resource "aws_instance" "this" {
53
56
tags = {
54
57
Terraform = " true"
55
58
Env = var.env
56
- Name = " ${ var . env } -openvpn-connector "
59
+ Name = local.name
57
60
}
58
61
59
62
}
Original file line number Diff line number Diff line change
1
+ output "cmd" {
2
+ value = {
3
+ up = " ssh -M -S bastion.sock -fNT ubuntu@${ element (aws_instance. this . * . id , 0 )} "
4
+ down = " ssh -S bastion.sock -O exit ubuntu@${ element (aws_instance. this . * . id , 0 )} "
5
+ status = " ssh -S bastion.sock -O check ubuntu@${ element (aws_instance. this . * . id , 0 )} "
6
+ }
7
+ }
8
+
9
+ output "instance_id" {
10
+ value = element (aws_instance. this . * . id , 0 )
11
+ }
12
+
13
+ output "ssh_config" {
14
+ value = local. ssh_config
15
+ }
16
+
17
+ output "security_group" {
18
+ value = element (aws_security_group. this . * . id , 0 )
19
+ }
Original file line number Diff line number Diff line change 1
1
variable "env" {}
2
+
3
+ variable "aws_profile" {}
2
4
variable "vpc_id" {}
3
5
variable "private_subnets" {}
4
6
variable "ec2_key_pair_name" {}
5
7
variable "openvpn_token" {}
6
8
7
9
variable "instance_type" {
10
+ type = string
8
11
default = " t3.nano"
9
12
}
10
13
@@ -14,6 +17,12 @@ variable "enabled" {
14
17
description = " Gives ability to enable or disable Creation of NAT EC2"
15
18
}
16
19
20
+ variable "ext_security_groups" {
21
+ description = " External security groups to add to bastion host"
22
+ type = list (any )
23
+ default = []
24
+ }
25
+
17
26
variable "allowed_cidr_blocks" {
18
27
type = list (string )
19
28
description = " List of network subnets that are allowed"
@@ -26,3 +35,24 @@ variable "ssm_role_arn" {
26
35
type = string
27
36
default = " arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
28
37
}
38
+
39
+ variable "ssh_forward_rules" {
40
+ type = list (string )
41
+ description = " Rules that will enable port forwarding. SSH Config syntax"
42
+ default = []
43
+ }
44
+
45
+ locals {
46
+ name = " ${ var . env } -openvpn-connector(bastion)"
47
+ proxycommand = <<- EOT
48
+ ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
49
+ EOT
50
+ ssh_config = concat ([
51
+ " # SSH over Session Manager" ,
52
+ " host i-* mi-*" ,
53
+ " ServerAliveInterval 180" ,
54
+ local . proxycommand ,
55
+ ], var. ssh_forward_rules )
56
+ ssm_document_name = local. name
57
+ }
58
+
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ terraform {
6
6
template = {
7
7
source = " hashicorp/template"
8
8
}
9
+ local = {
10
+ source = " hashicorp/local"
11
+ version = " ~> 1.2"
12
+ }
9
13
}
10
14
required_version = " >= 0.13"
11
15
}
You can’t perform that action at this time.
0 commit comments