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

Commit 2072603

Browse files
committed
New: module bastion
Setup a ssh bastion for specified VPC. This is useful when managing a VPC. Simply a single node ASG (with EIP) with SSH-in allowed.
1 parent 683992c commit 2072603

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

examples/bastion-test/tester.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
variable "region" {
2+
description = "The region to put resources in"
3+
default = "us-east-1"
4+
}
5+
6+
variable "az" {
7+
description = "The availability zone to put resources in"
8+
default = "us-east-1c"
9+
}
10+
11+
variable "key_name" {
12+
description = "The keypair used to ssh into the asg intances"
13+
default = "shida-east-1"
14+
}
15+
16+
provider "aws" {
17+
region = var.region
18+
}
19+
20+
module "vpc" {
21+
source = "../../modules/vpc-scenario-1"
22+
azs = [var.az]
23+
name_prefix = "bastion-test"
24+
cidr = "192.168.0.0/16"
25+
public_subnet_cidrs = ["192.168.0.0/16"]
26+
region = var.region
27+
map_on_launch = false
28+
}
29+
30+
module "bastion" {
31+
source = "../../modules/bastion"
32+
region = var.region
33+
key_name = var.key_name
34+
public_subnet_id = module.vpc.public_subnet_ids[0]
35+
identifier = "test"
36+
vpc_id = module.vpc.vpc_id
37+
}

examples/single-node-asg-test/tester.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module "snasg" {
3737
key_name = var.key_name
3838
subnet_id = module.vpc.public_subnet_ids[0]
3939
security_group_ids = [aws_security_group.eiptest.id]
40-
assign_eip = true
40+
assign_eip = false # true case is tested in bastion-test example
4141
}
4242

4343
module "ubuntu-ami" {

modules/bastion/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SSH Bastion
2+
3+
This is a module to provide a bastion to access the inside of a VPC from Internet.

modules/bastion/main.tf

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
variable "vpc_id" {
2+
type = string
3+
description = "ID of the VPC."
4+
}
5+
6+
variable "identifier" {
7+
type = string
8+
description = "Identifier of related resources."
9+
}
10+
11+
variable "region" {
12+
type = string
13+
description = "AWS region for this bastion to be in."
14+
}
15+
16+
variable "key_name" {
17+
type = string
18+
description = "SSH key pair name for the bastion."
19+
}
20+
21+
variable "public_subnet_id" {
22+
type = string
23+
description = "The subnet for the bastion. The subnet must be able to access Internet."
24+
}
25+
26+
variable "instance_type" {
27+
type = string
28+
default = "t2.nano"
29+
description = "Bastion instance type."
30+
}
31+
32+
variable "egress_cidrs" {
33+
type = list(string)
34+
default = ["0.0.0.0/0"]
35+
description = "Egress subnets that bastion can access."
36+
}
37+
38+
module "instance" {
39+
source = "../single-node-asg"
40+
name_prefix = var.identifier
41+
name_suffix = "bastion"
42+
ami = module.ubuntu-ami.id
43+
instance_type = var.instance_type
44+
region = var.region
45+
key_name = var.key_name
46+
subnet_id = var.public_subnet_id
47+
security_group_ids = [aws_security_group.bastion.id]
48+
assign_eip = true
49+
}
50+
51+
resource "aws_security_group" "bastion" {
52+
name = "${var.identifier}-bastion"
53+
vpc_id = var.vpc_id
54+
55+
ingress {
56+
from_port = 22
57+
to_port = 22
58+
protocol = "tcp"
59+
cidr_blocks = ["0.0.0.0/0"]
60+
}
61+
62+
egress {
63+
from_port = 0
64+
to_port = 0
65+
protocol = "-1"
66+
cidr_blocks = var.egress_cidrs
67+
}
68+
}
69+
70+
module "ubuntu-ami" {
71+
source = "../../modules/ami-ubuntu"
72+
release = "18.04"
73+
}

0 commit comments

Comments
 (0)