Skip to content

Commit aedb3d4

Browse files
chore: Update Terraform versions and required providers in modules
1 parent 9ce6c26 commit aedb3d4

File tree

35 files changed

+1376
-532
lines changed

35 files changed

+1376
-532
lines changed

live/common-infra/configs/prod.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ region = "us-west-2"
44
name = "common-infra"
55
namespace = "nan"
66
environment = "prod"
7-
tags = {}
7+
tags = {}
88

99
# Core Networking settings
1010

live/common-infra/configs/staging.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ region = "us-west-2"
44
name = "common-infra"
55
namespace = "nan"
66
environment = "staging"
7-
tags = {}
7+
tags = {}
88

99
# Core Networking settings
1010

live/core-networking/configs/prod.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ region = "us-west-2"
44
name = "core-networking"
55
namespace = "nan"
66
environment = "prod"
7-
tags = {}
7+
tags = {}
88

99
# AWS settings
1010

live/core-networking/configs/staging.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ region = "us-west-2"
44
name = "core-networking"
55
namespace = "nan"
66
environment = "staging"
7-
tags = {}
7+
tags = {}
88

99
# AWS settings
1010

live/terraform-backend/configs/prod.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ region = "us-west-2"
44
name = "tf-backend"
55
namespace = "nan"
66
environment = "prod"
7-
tags = {}
7+
tags = {}

live/terraform-backend/configs/staging.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ region = "us-west-2"
44
name = "tf-backend"
55
namespace = "nan"
66
environment = "staging"
7-
tags = {}
7+
tags = {}
File renamed without changes.

modules/bastion/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,41 @@ ssh -i /path/to/your/private/key ubuntu@$bastion_instance_id
196196

197197
### Optional Steps
198198

199+
The following steps are optional and can be used to further secure the Bastion host
200+
or to streamline the connection process.
201+
202+
#### Quick Connection Using Script
203+
204+
To streamline the process of connecting to the Bastion host, you can use the provided `connect.sh` script. This script automates the steps of generating an SSH key, uploading it to the instance, and establishing a connection through AWS Systems Manager.
205+
206+
```sh
207+
./scripts/connect.sh -i <instance-id>
208+
```
209+
210+
Replace `<instance-id>` with the ID of the Bastion host instance. The script will generate an SSH key pair, upload the public key to the Bastion host, and establish an SSH connection using the private key.
211+
212+
Use the `-h` or `--help` flag to see the available options:
213+
214+
```sh
215+
$ ./scripts/connect.sh --help
216+
217+
Script to connect to an AWS Bastion host. Usage:
218+
219+
connect.sh [option] ARGUMENTS...
220+
221+
Options:
222+
-h, --help Display this help message
223+
224+
--instance-id=INSTANCE_ID EC2 instance ID of the Bastion host
225+
--tag=TAG Tag to identify the Bastion host.
226+
Will be used to retrieve the instance ID.
227+
If not provided, instance ID must be provided.
228+
Will be ignored if instance ID is provided.
229+
230+
--key-name=KEY_NAME Name of the SSH key file (default: bastion_key)
231+
--key-dir=KEY_DIR Directory to store the SSH key (default: ~/.ssh)
232+
```
233+
199234
#### Use Other SSH Options to Open Connection
200235
201236
It is possible to use different options to open connection to bastion host. For example you can use -D 8888 option to open SSH connection with a local “dynamic” application-level port forwarding through 8888 port. See [this link](https://explainshell.com/explain?cmd=ssh+-i+%24PRIVATE_KEY_FILE+-D+8888+ubuntu%40%24INSTANCE_ID) for detailed explanation.

modules/bastion/iam.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ resource "aws_iam_role_policy" "bastion_host_iam_role" {
7171
"secretsmanager:GetSecretValue"
7272
],
7373
"Resource" : "arn:aws:secretsmanager:*:*:secret:*"
74+
},
75+
{
76+
"Effect" : "Allow",
77+
"Action" : [
78+
"eks:ListClusters",
79+
"eks:DescribeCluster",
80+
"eks:ListNodegroups",
81+
"eks:DescribeNodegroup",
82+
"eks:AccessKubernetesApi"
83+
],
84+
"Resource" : "*"
7485
}
7586
]
7687
})

modules/bastion/scripts/connect.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env bash
2+
3+
##
4+
## Script to connect to an AWS Bastion host or create a tunnel through the bastion host. Usage:
5+
##
6+
## @script.name [option] ARGUMENTS...
7+
##
8+
## Options:
9+
## -h, --help Display this help message
10+
##
11+
## --instance-id=INSTANCE_ID EC2 instance ID of the Bastion host
12+
## --tag=TAG Tag to identify the Bastion host.
13+
## Will be used to retrieve the instance ID.
14+
## If not provided, instance ID must be provided.
15+
## Will be ignored if instance ID is provided.
16+
##
17+
## --key-name=KEY_NAME Name of the SSH key file (default: bastion_key)
18+
## --key-dir=KEY_DIR Directory to store the SSH key (default: ~/.ssh)
19+
##
20+
## --bastion-user=USER Username for the bastion host (default: ubuntu)
21+
## --tunnel=LOCAL_PORT:REMOTE_HOST:REMOTE_PORT
22+
## Create a tunnel from LOCAL_PORT to REMOTE_PORT on REMOTE_HOST through the bastion host.
23+
##
24+
## --tunnel-target-user=USER Username for the tunnel target (optional)
25+
## --tunnel-target-key=KEY Path to the SSH key file for the tunnel target (optional)
26+
27+
# -e: exit on error
28+
set -e
29+
30+
ROOT="$(realpath "$(dirname "$0")"/..)"
31+
SCRIPTS_DIR="${ROOT}/scripts"
32+
33+
# shellcheck disable=SC1091
34+
. "${SCRIPTS_DIR}/easy-options/easyoptions.sh" || exit
35+
36+
# Constants
37+
DEFAULT_SSH_KEY_NAME="bastion_key"
38+
DEFAULT_SSH_KEY_DIR="$HOME/.ssh"
39+
DEFAULT_BASTION_USER="ubuntu"
40+
41+
# Validate required arguments
42+
if [[ -z "$tag" && -z "$instance_id" ]]; then
43+
echo "Error: Either Bastion host tag or instance ID must be provided."
44+
show_help
45+
exit 1
46+
fi
47+
48+
# Set default values if not provided
49+
SSH_KEY_NAME="${key_name:-$DEFAULT_SSH_KEY_NAME}"
50+
SSH_KEY_DIR="${key_dir:-$DEFAULT_SSH_KEY_DIR}"
51+
SSH_KEY_PATH="$SSH_KEY_DIR/$SSH_KEY_NAME"
52+
SSH_PUBLIC_KEY_PATH="$SSH_KEY_PATH.pub"
53+
BASTION_USER="${bastion_user:-$DEFAULT_BASTION_USER}"
54+
55+
# Check if AWS CLI and Session Manager Plugin are installed
56+
if ! command -v aws &>/dev/null; then
57+
echo "AWS CLI could not be found. Please install it before running this script."
58+
exit 1
59+
fi
60+
61+
if ! command -v session-manager-plugin &>/dev/null; then
62+
echo "Session Manager Plugin could not be found. Please install it before running this script."
63+
exit 1
64+
fi
65+
66+
# Generate SSH key pair if not exists
67+
if [ ! -f "$SSH_KEY_PATH" ]; then
68+
echo "Generating SSH key pair..."
69+
ssh-keygen -t rsa -b 2048 -f "$SSH_KEY_PATH" -N ""
70+
fi
71+
72+
# Retrieve Bastion instance ID if not provided
73+
if [[ -z "$instance_id" ]]; then
74+
echo "Retrieving Bastion instance ID..."
75+
instance_id=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$tag" --query "Reservations[*].Instances[*].InstanceId" --output text)
76+
if [[ -z "$instance_id" ]]; then
77+
echo "No Bastion host found with tag: $tag"
78+
exit 1
79+
fi
80+
echo "Bastion Instance ID: $instance_id"
81+
fi
82+
83+
# Send SSH Public Key
84+
echo "Sending SSH public key to Bastion host..."
85+
aws ec2-instance-connect send-ssh-public-key --instance-id "$instance_id" --instance-os-user "$BASTION_USER" --ssh-public-key file://"$SSH_PUBLIC_KEY_PATH"
86+
87+
# Connect to Bastion Host or create a tunnel using SSH through Session Manager
88+
ssh_proxy_command_option="ProxyCommand sh -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'\""
89+
90+
if [[ -z "$tunnel" ]]; then
91+
echo "Connecting to Bastion host using SSH through Session Manager..."
92+
ssh -i "$SSH_KEY_PATH" -o "$ssh_proxy_command_option" "$BASTION_USER@$instance_id"
93+
echo "Connection to Bastion host established."
94+
exit 0
95+
fi
96+
97+
echo "Creating tunnel through Bastion host to $tunnel..."
98+
if [[ -n "$tunnel_target_user" && -n "$tunnel_target_key" ]]; then
99+
tunnel_host=$(echo "$tunnel" | cut -d':' -f2)
100+
ssh_proxy_command_option="ProxyCommand ssh -i $tunnel_target_key -W %h:%p $tunnel_target_user@$tunnel_host"
101+
ssh_tunnel_proxy_command_option="ProxyCommand ssh -i $tunnel_target_key -W %h:%p $tunnel_target_user@$tunnel_host"
102+
ssh -i "$SSH_KEY_PATH" -o "$ssh_proxy_command_option" -L "$tunnel" -o "$ssh_tunnel_proxy_command_option" -N "$BASTION_USER@$instance_id"
103+
else
104+
ssh -i "$SSH_KEY_PATH" -o "$ssh_proxy_command_option" -L "$tunnel" -N "$BASTION_USER@$instance_id"
105+
fi
106+
echo "Tunnel established through Bastion host."

0 commit comments

Comments
 (0)