- Newest
- Most votes
- Most comments
There are a few options.
Assuming your EC2 instance is being managed by Systems Manager, you can use Systems Manager AWS-ResizeInstance to change instance type. The console provides AWS CLI command to run, e.g.
aws ssm start-automation-execution --document-name "AWS-ResizeInstance" --document-version "\$DEFAULT" --parameters '{"InstanceId":["i-XXXXX"],"InstanceType":["m7g.xlarge"],"SleepWait":["PT5S"],"AutomationAssumeRole":[""]}' --region ap-southeast-1
Since you are a developer, perhaps modify your script to get EC2 InstanceState. Wait for InstanceState to change from running to stopped before you execute start-instance
aws ec2 describe-instance-status --instance-ids i-XXXXXXXX
{
"InstanceStatuses": [
{
"AvailabilityZone": "ap-southeast-1a",
"InstanceId": "i-XXXXXXXX",
"InstanceState": {
"Code": 16,
"Name": "running"
},
Or you can use Python to do it. Code is something like below (Reference: https://medium.com/@hasnain.hakim25/change-the-instance-type-in-aws-using-python-with-custom-metrics-memory-and-lambda-function-14f9ca5c9b77)
import boto3
import time
client = boto3.client('ec2')
ec2_instance = 'i-XXXXXXXX'
client.stop_instances(InstanceIds=[my_instance])
waiter=client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[my_instance])
client.modify_instance_attribute(InstanceId=ec2_instance, Attribute='instanceType', Value='t3.large')
client.start_instances(InstanceIds=[my_instance])
Hello.
How about managing EC2 with CloudFormation?
By managing EC2 with CloudFormation, you can restart the instance just by changing the instance type parameters within CloudFormation.
Relevant content
- asked a year ago
- asked a year ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 3 years ago
The instance whose type I change regularly is a single developer machine. I want to be able to manually start it, stop it or change the instance type via terminal. Cloud Formation seems to target a different use case (managing many instances) instead.
I think it will be useful even if you only have one EC2. For example, in the template below, just change "t3.micro" to "t3.medium" and run the "aws cloudformation update-stack" command and EC2 will be restarted. In other words, there is no need to execute the EC2 stop and start commands.
After changing the CloudFormation template, just run the following command.