Skip to content

Commit d96d59d

Browse files
authored
Merge pull request #1 from hazelops/openvpn+bastion
Added Bastion functionality to EC2
2 parents b532455 + 066c6f6 commit d96d59d

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

iam.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AWS SSM resources
22
resource "aws_iam_role" "this" {
3-
name = "${var.env}-openvpn-connector"
3+
name = local.name
44
assume_role_policy = data.aws_iam_policy_document.this.json
55
}
66

@@ -10,7 +10,7 @@ resource "aws_iam_role_policy_attachment" "this" {
1010
}
1111

1212
resource "aws_iam_instance_profile" "this" {
13-
name = "${var.env}-openvpn-connector"
13+
name = local.name
1414
role = aws_iam_role.this.name
1515
}
1616

main.tf

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Security Groups
22
resource "aws_security_group" "this" {
33
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)"
66
vpc_id = var.vpc_id
77

88
ingress {
@@ -24,7 +24,7 @@ resource "aws_security_group" "this" {
2424
tags = {
2525
Terraform = "true"
2626
Env = var.env
27-
Name = "${var.env}-openvpn-connector"
27+
Name = local.name
2828
}
2929

3030
lifecycle {
@@ -40,9 +40,12 @@ resource "aws_instance" "this" {
4040
iam_instance_profile = aws_iam_instance_profile.this.name
4141
subnet_id = var.private_subnets[0]
4242
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+
])
4446

45-
disable_api_termination = true
47+
disable_api_termination = true
48+
associate_public_ip_address = false
4649

4750
lifecycle {
4851
ignore_changes = all
@@ -53,7 +56,7 @@ resource "aws_instance" "this" {
5356
tags = {
5457
Terraform = "true"
5558
Env = var.env
56-
Name = "${var.env}-openvpn-connector"
59+
Name = local.name
5760
}
5861

5962
}

output.tf

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

variables.tf

+30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
variable "env" {}
2+
3+
variable "aws_profile" {}
24
variable "vpc_id" {}
35
variable "private_subnets" {}
46
variable "ec2_key_pair_name" {}
57
variable "openvpn_token" {}
68

79
variable "instance_type" {
10+
type = string
811
default = "t3.nano"
912
}
1013

@@ -14,6 +17,12 @@ variable "enabled" {
1417
description = "Gives ability to enable or disable Creation of NAT EC2"
1518
}
1619

20+
variable "ext_security_groups" {
21+
description = "External security groups to add to bastion host"
22+
type = list(any)
23+
default = []
24+
}
25+
1726
variable "allowed_cidr_blocks" {
1827
type = list(string)
1928
description = "List of network subnets that are allowed"
@@ -26,3 +35,24 @@ variable "ssm_role_arn" {
2635
type = string
2736
default = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
2837
}
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+

versions.tf

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ terraform {
66
template = {
77
source = "hashicorp/template"
88
}
9+
local = {
10+
source = "hashicorp/local"
11+
version = "~> 1.2"
12+
}
913
}
1014
required_version = ">= 0.13"
1115
}

0 commit comments

Comments
 (0)