The sole task of this demo is to show the bastion and target host setup in AWS using terraform.
- Bastion Host - The Bastion Host is an AWS EC2 instance which is publicly accessible over ssh on port 22.
- Target Host - The Target Host is an AWS EC2 instance that is on a private subnet. The Target Host should be accessible via ssh from the Bastion Host.
- Store any keys used in AWS Secrets Manage.
Being a pure demo this setup doesn't strive to provide a complete quality solution. For simplicity only terraform is used. It's worth mentioning that a production ready solution:
- Might use terragrunt or other tools to support multienv.
- Might not use terraform at all :)
- Might not use SSH bastion/jump host approach at all. For instance there's a small article (How to securely connect EC2 via SSH with AWS Systems Manager) which suggests that there're better ways, but it contains links to better detailed sources.
This demo doesn't contain any work-from-zero development, it basically wrapps up usage (copy-pastes) of good-quality terraform modules, mostly from Cloud Posse. Again there are many good-quality modules out there (like those from Anton Babenko), this is just matter of taste and match for a specific task.
This demo doesn't deviate much from available examples, though it's worth mentioning that in this demo:
- The target host is shipped with the recent Ubuntu LTS flavor.
- ec2-instance-module has its built-in security group disabled, since the demo creates another one instead. This security group independently from the VPC default security group manages access from the bastion host to the target host.
- This demo writes the private ssh key into AWS Secrets Manager without any IAM permissions configuration, just as is, to purely show that the secret data might or should be stored in cloud provider's secret management solution.
It's supposed that the reader has AWS account, has already set the environment up and installed terraform. This demo doesn't explain or suggests how to do it, since this a considarable topic itself which might vary depending on the use-case. If the mentioned setup has been performed, you are ready to apply the configuration:
terraform init
terraform apply
terraform output
After applying configuration the output provides all the necessary details, specifically on how to connect to either the bastion or to the target host:
public_dns = "ec2-108-136-96-151.ap-southeast-3.compute.amazonaws.com"
ssh_bastion_command = "ssh -i ./secrets/eg-demo-bastion-ssh-key ec2-user@ec2-108-136-96-151.ap-southeast-3.compute.amazonaws.com"
ssh_target_command = <<EOT
# Add the private key into agent (done once)
ssh-add ./secrets/eg-demo-bastion-ssh-key
# Direct hop through bastion into the target host
ssh -At ec2-user@ec2-108-136-96-151.ap-southeast-3.compute.amazonaws.com "ssh ubuntu@172.16.20.121"
EOT
It's suggested to add the private key into ssh-agent for ease of usage (for more details on ssh-agent, please refer to man ssh-agent
). Bellow I shortly describe what commands in the output do and how to connect to the target host:
- We add the private key into the agent
ssh-add ./secrets/eg-demo-bastion-ssh-key
- We connect to the bastion and execute ssh command on it to jump into the target (mind
-At
flags)ssh -At ec2-user@ec2-108-136-96-151.ap-southeast-3.compute.amazonaws.com "ssh ubuntu@172.16.20.121"
. Let's shortly cover these flags.-A
enables agent forwarding, i.e. makes ssh-keys available for the bastion ssh session. While-t
is required for pseudo-terminal allocation, since the inner ssh command is interactive.
Clean up all of the resources from AWS.
terraform destroy
No requirements.
Name | Version |
---|---|
aws | 4.62.0 |
Name | Source | Version |
---|---|---|
aws_key_pair | cloudposse/key-pair/aws | 0.18.3 |
ec2_bastion | cloudposse/ec2-bastion-server/aws | 0.30.1 |
ec2_instance | cloudposse/ec2-instance/aws | 0.47.1 |
instance_profile_label | cloudposse/label/null | 0.25.0 |
secrets-manager | lgallard/secrets-manager/aws | 0.7.0 |
security_group | cloudposse/security-group/aws | 2.0.1 |
subnets | cloudposse/dynamic-subnets/aws | 2.1.0 |
this | cloudposse/label/null | 0.25.0 |
vpc | cloudposse/vpc/aws | 2.0.0 |
Name | Type |
---|---|
aws_iam_instance_profile.test | resource |
aws_iam_role.test | resource |
aws_ami.ubuntu | data source |
aws_iam_policy_document.test | data source |
Name | Description | Type | Default | Required |
---|---|---|---|---|
additional_tag_map | Additional key-value pairs to add to each map in tags_as_list_of_maps . Not added to tags or id .This is for some rare cases where resources want additional configuration of tags and therefore take a list of maps with tag key, value, and additional configuration. |
map(string) |
{} |
no |
ami | The AMI to use for the target instance. By default uses Ubuntu 22.04 | string |
"" |
no |
ami_owner | Owner of the given AMI (ignored if ami unset, required if set) |
string |
"" |
no |
attributes | ID element. Additional attributes (e.g. workers or cluster ) to add to id ,in the order they appear in the list. New attributes are appended to the end of the list. The elements of the list are joined by the delimiter and treated as a single ID element. |
list(string) |
[] |
no |
availability_zones | List of Availability Zones where subnets will be created | list(string) |
n/a | yes |
bastion_instance_type | Bastion instance type | string |
"t3.micro" |
no |
bastion_user_data | User data content | list(string) |
[] |
no |
context | Single object for setting entire context at once. See description of individual variables for details. Leave string and numeric variables as null to use default value.Individual variable settings (non-null) override settings in context object, except for attributes, tags, and additional_tag_map, which are merged. |
any |
{ |
no |
delimiter | Delimiter to be used between ID elements. Defaults to - (hyphen). Set to "" to use no delimiter at all. |
string |
null |
no |
descriptor_formats | Describe additional descriptors to be output in the descriptors output map.Map of maps. Keys are names of descriptors. Values are maps of the form {<br> format = string<br> labels = list(string)<br>} (Type is any so the map values can later be enhanced to provide additional options.)format is a Terraform format string to be passed to the format() function.labels is a list of labels, in order, to pass to format() function.Label values will be normalized before being passed to format() so they will beidentical to how they appear in id .Default is {} (descriptors output will be empty). |
any |
{} |
no |
enabled | Set to false to prevent the module from creating any resources | bool |
null |
no |
environment | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | string |
null |
no |
id_length_limit | Limit id to this many characters (minimum 6).Set to 0 for unlimited length.Set to null for keep the existing setting, which defaults to 0 .Does not affect id_full . |
number |
null |
no |
instance_name | The name of private instance | string |
n/a | yes |
instance_type | The type of the private instance | string |
n/a | yes |
label_key_case | Controls the letter case of the tags keys (label names) for tags generated by this module.Does not affect keys of tags passed in via the tags input.Possible values: lower , title , upper .Default value: title . |
string |
null |
no |
label_order | The order in which the labels (ID elements) appear in the id .Defaults to ["namespace", "environment", "stage", "name", "attributes"]. You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. |
list(string) |
null |
no |
label_value_case | Controls the letter case of ID elements (labels) as included in id ,set as tag values, and output by this module individually. Does not affect values of tags passed in via the tags input.Possible values: lower , title , upper and none (no transformation).Set this to title and set delimiter to "" to yield Pascal Case IDs.Default value: lower . |
string |
null |
no |
labels_as_tags | Set of labels (ID elements) to include as tags in the tags output.Default is to include all labels. Tags with empty values will not be included in the tags output.Set to [] to suppress all generated tags.Notes: The value of the name tag, if included, will be the id , not the name .Unlike other null-label inputs, the initial setting of labels_as_tags cannot bechanged in later chained modules. Attempts to change it will be silently ignored. |
set(string) |
[ |
no |
name | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. This is the only ID element not also included as a tag .The "name" tag is set to the full id string. There is no tag with the value of the name input. |
string |
null |
no |
namespace | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | string |
null |
no |
regex_replace_chars | Terraform regular expression (regex) string. Characters matching the regex will be removed from the ID elements. If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits. |
string |
null |
no |
region | AWS region | string |
n/a | yes |
security_group_rules | A list of maps of Security Group rules for the private instance. The values of map is fully complated with aws_security_group_rule resource. To get more info see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule . |
list(any) |
n/a | yes |
ssh_key_path | Save location for ssh public keys generated by the module | string |
n/a | yes |
stage | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | string |
null |
no |
subnets_per_az_count | The number of subnet of each type (public or private) to provision per Availability Zone. | number |
1 |
no |
subnets_per_az_names | The subnet names of each type (public or private) to provision per Availability Zone. This variable is optional. If a list of names is provided, the list items will be used as keys in the outputs named_private_subnets_map , named_public_subnets_map ,named_private_route_table_ids_map and named_public_route_table_ids_map |
list(string) |
[ |
no |
tags | Additional tags (e.g. {'BusinessUnit': 'XYZ'} ).Neither the tag keys nor the tag values will be modified by this module. |
map(string) |
{} |
no |
tenant | ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for | string |
null |
no |
Name | Description |
---|---|
public_dns | Public DNS of instance (or DNS of EIP) |
ssh_bastion_command | ssh command to connect to the bastion host |
ssh_target_command | ssh command to connect to the target host |