Elastic Beanstalk实例停止时自动终止,密钥对配置求助
Hey there! Let's walk through why you're seeing that unexpected instance termination and how to get your key pair set up correctly for SSH access.
Why Stopping the Instance Causes Termination
Elastic Beanstalk environments are almost always managed by an Auto Scaling Group (ASG). When you stop an instance in the ASG, the group detects that the instance is no longer "healthy" or running, and automatically spins up a replacement to maintain your desired capacity. That's why your instance gets terminated instead of just stopping.
Solution 1: Update Elastic Beanstalk Environment to Use the Key Pair (Recommended)
This is the cleanest approach, as it ensures all future instances in your environment use the key pair, and keeps everything under Elastic Beanstalk's management:
- Open the Elastic Beanstalk console and navigate to your environment.
- Go to the Configuration tab, then find the Instances configuration category.
- Click Edit next to this category.
- In the EC2 key pair dropdown menu, select the key pair you want to use for SSH access.
- Save the configuration changes.
Elastic Beanstalk will now perform an environment update: it'll terminate existing instances one by one and launch new ones with your selected key pair. Once the update completes, you can use the private key from your key pair to SSH into any of the new instances.
Solution 2: Manually Add Key Pair to an Existing Instance (For Special Cases)
If you need to keep the current instance (e.g., it has local data you can't easily migrate), you can remove it from the Auto Scaling Group first to prevent automatic termination:
- Go to the EC2 Console, find your running Elastic Beanstalk instance.
- Click Actions > Instance settings > Remove from Auto Scaling Group. Confirm the removal when prompted.
- Now stop the instance (it won't be replaced since it's no longer in the ASG).
- With the instance stopped, click Actions > Instance settings > Edit key pair. Select your desired key pair and save.
- Start the instance back up. You can now SSH into it using your key pair.
⚠️ Important Note: This instance is no longer managed by Elastic Beanstalk. Any future environment updates (like configuration changes or application deployments) won't affect it, and it won't be replaced if it fails. Use this only as a temporary fix.
Key Pre-Requisite
Make sure the key pair you're trying to use is created in the same AWS Region as your Elastic Beanstalk environment. Key pairs are region-specific and can't be cross-region used.
内容的提问来源于stack exchange,提问作者Ramesh

