There is a very good question on [How to] SSH to Elastic [an] Beanstalk instance, but one thing I noticed is that, through this method, it is only possible to add one SSH key.
How can I add multiple SSH keys to an instance? Is there a way to automatically add multiple keys to new instances?
One way you could accomplish this is to create a user data script which appends the public keys of the additional key-pairs you want to use to ~ec2-user/.ssh/authorized_keys, and launch the instance with that user data, for example:
Following on from Jim Flanagan's answer, you could get the keys added to every instance by creating
.ebextensions/app.config
in your application source directory with contents:No, Elastic Beanstalk only supports a single key pair. You can manually add SSH keys to the
authorized_keys
file, but these will not be known to the Elastic Beanstalk tools.The most dynamic way to add multiple SSH keys to Elastic Beanstalk EC2 instances
Step 1
Create a group in IAM. Call it something like
beanstalk-access
. Add the users who need SSH access to that group in IAM. Also add their public ssh key(s) to their IAMSecurity credentials
.Step 2
The deployment script below will be parsing JSON data from AWS CLI using a handy Linux tool called
jq
(jq official tutorial), so we need to add it in .ebextensions:Step 3
Add the following BASH deployment script to .ebextensions:
Unfortunately, because this is YAML, you can't indent the code to make it more easily readable. But let's break down what's happening:
(In the code snippet directly below) We're removing the default SSH key file to give full control of that list to this deployment script.
(In the code snippet directly below) Using AWS CLI, we're getting the list of users in the
beanstalk-access
group, and then we're piping that JSON list intojq
to extract only that list of `$users.(In the code snippet directly below) Here, we're converting that JSON
$users
list into a BASH array and calling it$users_array
.readarray -t users_array < <(jq -r '.[]' <<<"$users") declare -p users_array
(In the code snippet directly below) We begin looping through the array of users.
(In the code snippet directly below) This can probably be done in one line, but it's grabbing the list of SSH keys associated to each user in the
beanstalk-access
group. It has not yet turned it into a BASH array, it's still a JSON list.(In the code snippet directly below) Now it's converting that JSON list of each users' SSH keys into a BASH array.
(In the code snippet directly below) Now it's converting that JSON list into a BASH array.
(In the code snippet directly below) Now we loop through each user's array of SSH keys.
(In the code snippet directly below) We're adding each SSH key for each user to the
authorized_keys
file.(In the code snippet directly below) Close out both the
$users_array
loop and$users_keys
loop.(In the code snippet directly below) Give the
authorized_keys
file the same permissions it originally had.Step 4
If your Elastic Beanstalk EC2 instance is in a public subnet, you can just ssh into it using:
ssh ec2-user@ip-address -i /path/to/private/key
If your Elastic Beanstalk EC2 instance is in a private subnet (as it should be for cloud security best practices), then you will need to have a "bastion server" EC2 instance which will act as the gateway for tunneling all SSH access to EC2 instances. Look up
ssh agent forwarding
orssh proxy commands
to get an idea of how to accomplish SSH tunneling.Adding new users
All you do is add them to your IAM
beanstalk-access
group and run a deployment, and that script will add them to your Elastic Beanstalk instances.instead of running
echo
and storing your keys on Git, you can upload your public keys to IAM user's on AWS and than do:https://stackoverflow.com/a/16776129/7459377
the simplest method - like @rhunwicks but with one ">" symbol on first copy:
Regards.