Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 98a15b1

Browse files
committed
Tunnel using EC2 instance connect
1 parent a024cbb commit 98a15b1

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# EC2 Instance Connect tunnel
2+
3+
Creates a s single node ASG (using the `singe-node-asg` module) allowing SSH
4+
connections using [EC2 Instance Connect](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html).
5+
Assumes Ubuntu AMI to be used (`ec2-instance-connect` gets installed using
6+
`apt`). Use `ec2-connect-role` to setup an IAM role for SSH access.

modules/ec2-connect-tunnel/iam.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# allows connecting with SSM manager
2+
resource "aws_iam_role_policy_attachment" "ssm_instance" {
3+
role = module.asg.asg_iam_role_name
4+
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
5+
}

modules/ec2-connect-tunnel/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module "asg" {
2+
source = "../single-node-asg"
3+
4+
region = var.region
5+
ami = var.ami
6+
key_name = ""
7+
instance_type = var.instance_type
8+
name_prefix = var.name_prefix
9+
name_suffix = var.name_suffix
10+
11+
security_group_ids = [module.tunnel-sg.id]
12+
subnet_id = var.subnet_id
13+
data_volumes = []
14+
assign_eip = true
15+
16+
init_suffix = <<END_INIT_SUFFIX
17+
echo "Installing ec2-instance-connect"
18+
apt install ec2-instance-connect
19+
END_INIT_SUFFIX
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "public_ip" {
2+
value = module.asg.eip_address
3+
description = "Public IP of the tunnel"
4+
}

modules/ec2-connect-tunnel/sg.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module "tunnel-sg" {
2+
source = "../security-group-base"
3+
name = "${var.name_prefix}-sg"
4+
description = "SG for the tunnel ASG"
5+
vpc_id = var.vpc_id
6+
extra_tags = var.extra_tags
7+
}
8+
9+
module "ssh-port-sg-rule" {
10+
source = "../single-port-sg"
11+
security_group_id = module.tunnel-sg.id
12+
cidr_blocks = ["0.0.0.0/0"]
13+
port = 22
14+
description = "SSH from anywhere"
15+
}
16+
17+
# security group rule to open egress (outbound from nodes)
18+
module "allow-open-egress" {
19+
source = "../open-egress-sg"
20+
security_group_id = module.tunnel-sg.id
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
variable "name_prefix" {
2+
description = "Prefix for naming resources, usually project-related"
3+
type = string
4+
}
5+
6+
variable "name_suffix" {
7+
description = "suffix to include when naming the various resources"
8+
type = string
9+
default = ""
10+
}
11+
12+
variable "region" {
13+
description = "The AWS region to deploy to"
14+
type = string
15+
}
16+
17+
variable "ami" {
18+
description = "The base AMI for each AWS instance created"
19+
type = string
20+
}
21+
22+
variable "instance_type" {
23+
description = "The type of AWS instance (size)"
24+
type = string
25+
}
26+
27+
variable "vpc_id" {
28+
description = "ID of VPC to associate SG with"
29+
type = string
30+
}
31+
32+
variable "subnet_id" {
33+
description = "The ID of the subnet to use, depends on the availability zone"
34+
type = string
35+
}
36+
37+
variable "extra_tags" {
38+
description = "map of name,value pairs to tag the security group (append to Name tag)"
39+
default = {}
40+
type = map(string)
41+
}

0 commit comments

Comments
 (0)