recently encountered a problem with my EB instances that I could not figure out. The only way to figure out what was happening was to access the instances directly via SSH. However, accessing AWS Elastic Beanstalk Linux instances via SSH is not that simple. To access an EB instance via SSH, you first have to enable it.
Let's now see what needs to be done to enable SSH access in the AWS Elastic Beanstalk environment and to access EB Linux instances via SSH.
Prerequisites
Before getting started, you need to set up a valid AWS EC2 key pair.
"A key pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an Amazon EC2 instance. Amazon EC2 stores the public key on your instance, and you store the private key." — Amazon EC2 key pairs and Linux instances
The AWS doc states that "for Linux instances, the private key allows you to securely SSH into your instance." Therefore, this is why you need an AWS EC2 key pair.
Follow this guide from the AWS official documentation to learn how to set up a valid SSH key pair.
Specifically, retrieve a .pem
or .ppk
SSH private key. Now, you need to place your key in the .ssh
directory under your home directory.
If you are a Linux or macOS user, put your SSH private key under the following directory:
~/.ssh
On a Windows machine, place the SSH key file in the following directory:
C:\Users\<YOUR_ACCOUNT _NAME>\.ssh
Make sure to replace <YOUR_ACCOUNT_NAME>
with the name of your Windows account.
In both cases, if you do not have a .ssh
directory, create one.
SSH Into AWS Elastic Beanstalk Linux Instances
First, enter your local Elastic Beanstalk application directory. Then, launch the following command:
eb ssh
If SSH is not enabled in your EB environment, you will get the following error:ERROR: This environment is not set up for SSH. Use "eb ssh --setup" to set up SSH for the environment.
Now, as suggested by the error message, run the command below:
eb ssh --setup
The EB CLI will print:
WARNING: You are about to setup SSH for environment "<your_eb_environment_name>". If you continue, your existing instances will have to be terminated and new instances will be created. The environment will be temporarily unavailable. To confirm, type the environment name:
Type the name of your EB environment. Note that you can copy it from the <your_eb_environment_name>
variable you can find in the first sentence of the message.
Now, eb
will ask you to select the SSH private key you created earlier.
Select a keypair.1. <your_SSH_key_name>
2. [ Create new KeyPair ]
(default is 1):
Type Enter
or 1
to select your SSH private key. If you did not place the SSH private key in the right directory, as shown above, you would get an error, and the procedure will stop.
Now, as mentioned in the WARNING
message above, eb
will kill your existing instances and replace them with new instances accessible via SSH.
You can now SSH into your EB instances with the eb ssh
command. When launched, you will be asked to select the EB instance you want to access via SSH, as shown below:
Select an instance to ssh into
1) i-0670eb7302902c011
2) i-0898ccba32f30f4b8
(default is 1):
Type the number associated with the EB instance you want to access, and you will get the following result:
Amazon Linux 2 AMI
This EC2 instance is managed by AWS Elastic Beanstalk. Changes made via SSH WILL BE LOST if the instance is replaced by auto-scaling. For more information on customizing your Elastic Beanstalk environment, see our documentation here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
As mentioned in this message, keep in mind that any changes made via SSH will be lost as soon as the instance is destroyed to scale down.
You can now launch bash commands in the command line via SSH.
Et voilà! You just learned how to access your EB instances via SSH. This is especially useful for monitoring them, accessing logs, or debugging.
Conclusion
In this article, you learned how to enable SSH with eb
. Specifically, you looked at how to access AWS Elastic Beanstalk Linux instances via SSH. First, you saw how to generate an EC2 key pair. Then, you saw how to set up eb
and use your SSH private key to SSH into an EB Linux instance. This will allow you to launch bash commands directly into your instances.
Thanks for reading! I hope that you found this article helpful.
The post "How To Connect to an AWS Elastic Beanstalk Instance via SSH" appeared first on Writech.
Top comments (0)