AWS - Change EC2 Instance Size
AWS - Change EC2 Instance Size
Description
This automation job allows you to safely modify the instance type of an EC2 instance. The job handles the complete workflow including stopping the instance, changing its size, and optionally restarting it.
Prerequisites
- Turn on "Runner as Node" setting on your Runner.
- This requires version 5.8.0 or higher. Adjustments to Node tab may be required for earlier versions.
- AWS CLI installed on your runner.
- AWS EC2 Node Source configured with "Show only Running instances" set to "No". (Details below)
- Sufficient AWS IAM permissions to modify EC2 instances
AWS IAM Permissions
The AWS IAM role or user associated with this job requires the following permissions:
ec2:StopInstances
ec2:StartInstances
ec2:ModifyInstanceAttribute
ec2:DescribeInstances
ec2:DescribeInstanceTypeOfferings
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:StopInstances",
"ec2:StartInstances",
"ec2:ModifyInstanceAttribute",
"ec2:DescribeInstances",
"ec2:DescribeInstanceTypeOfferings"
],
"Resource": "*"
}
]
}
Important Node Source Configuration
When using this job with EC2 instances, a specific Node Source configuration is required:
Why: EC2 instances must be in a stopped state before their instance type can be modified. Therefore, the Node Source must be able to discover both running AND stopped instances.
Required Setting:
- In your EC2 Node Source configuration
- Set "Show only Running Instances" to "No"
If this setting remains set to "Yes", stopped instances will not be visible to Rundeck and the job will fail to locate the target instance.

Job Options
Option Name | Description | Required | Default |
---|---|---|---|
new-instance-type | The target EC2 instance type | Yes | N/A |
start-instance | Whether to restart the instance after changing size | Yes | Yes |
Available instance types include various sizes across these families:
- General Purpose: t3, m5
- Compute Optimized: c5
- Memory Optimized: r5
- Storage Optimized: i3
- Accelerated Computing: p3, g4dn
Job Workflow
The job executes the following steps in sequence:
- Stops the target EC2 instance
- Verifies instance state and performs validation:
- Confirms instance has reached 'stopped' state
- Validates new instance type is supported in the region
- Changes the instance type
- Optionally restarts the instance (based on
start-instance
option)
Script Details
The job uses a combination of AWS CLI commands and a Bash script to:
- Stop the instance safely
- Wait up to 30 seconds for instance to reach stopped state
- Verify the new instance type is supported in the region
- Modify the instance attribute to change the instance type
- Start the instance if requested
Error Handling
The script includes several error checks:
- Verifies instance reaches stopped state before proceeding
- Validates instance type compatibility in the region
- Confirms successful instance type modification
- Exits with appropriate error codes and messages on failures
Notes
- The job will not proceed with the instance type change if the instance fails to stop
- If the change step fails, the instance will remain in a stopped state
- The job supports cross-family instance type changes (e.g., t3 to m5)
- Default node filtering is configured to target EC2 instances using the tag 'ec2', but you must pick the nodes at the time of execution.
Troubleshooting
If you encounter issues:
- Verify the target instance is accessible and in a valid state
- Ensure the new instance type is supported in your region
- Check AWS credentials and permissions
- Review job logs for specific error messages