Skip to content

Instantly share code, notes, and snippets.

@aardbol
Last active September 4, 2024 14:44
Show Gist options
  • Save aardbol/c63d05843c645e7dea84207980677f7e to your computer and use it in GitHub Desktop.
Save aardbol/c63d05843c645e7dea84207980677f7e to your computer and use it in GitHub Desktop.
Ansible SSH connections via AWS EC2 Instance Connect Endpoint
# The dynamic inventory
# https://docs.ansible.com/ansible/latest/collections/amazon/aws/aws_ec2_inventory.html
plugin: amazon.aws.aws_ec2
profile: "{{ lookup('env', 'AWS_PROFILE') | default('aws_profile', true) }}"
regions:
- ap-northeast-1
- eu-central-1
cache: True
cache_timeout: 600
cache_plugin: "jsonfile"
cache_connection: '~/.ansible/tmp_staging'
hostnames:
- tag:Name
keyed_groups:
- key: tags
prefix: tag
groups:
cloud_aws: 'true'
filters:
instance-state-name: running
compose:
ansible_host: instance_id
tag_env: tags.Environment
tag_name: tags.Name
tag_provider: "'aws'"
@aardbol
Copy link
Author

aardbol commented Sep 4, 2024

In the vars file:

---
ansible_ssh_common_args: >-
  -o ProxyCommand='aws ec2-instance-connect open-tunnel --region {{ placement['region'] }} --instance-id {{ ansible_host }}'

You can still use ansible.cfg combination with it, e.g.:

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=1500s -o ForwardAgent=yes

Command to test after being authenticated with AWS:

ssh user@i-xxx -o ProxyCommand='aws ec2-instance-connect --region eu-central-1 open-tunnel --instance-id i-xxx'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment