Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change EC2 Size Solution #1612

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/.vuepress/sidebar-menus/learning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export default [{
collapsible: true,
children: [
{ link: '/learning/solutions/cost-management/index.md', text: 'Solution Summary' },
{ link: '/learning/solutions/cost-management/aws-change-ec2-size.md', text:'AWS - Change EC2 Size' },
{ link: '/learning/solutions/cost-management/aws-list-unused-vpcs.md', text: 'AWS - Identify Unused VPCs' },
{ link: '/learning/solutions/cost-management/aws-list-unused-lambda.md', text: 'AWS - Identify Unused Lambda Functions' },
{ link: '/learning/solutions/cost-management/aws-list-unused-securitygroups.md', text: 'AWS - Identify Unused Security Groups' },
Expand Down
114 changes: 114 additions & 0 deletions docs/learning/solutions/cost-management/aws-change-ec2-size.md
fdevans marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# 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](/administration/runner/runner-management/node-dispatch.html#runner-as-a-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`

```json
{
"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.

![](/assets/img/solutions-aws-change-ec2-nodesource.png)

## 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:

1. Stops the target EC2 instance
2. Verifies instance state and performs validation:
- Confirms instance has reached 'stopped' state
- Validates new instance type is supported in the region
3. Changes the instance type
4. 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:

1. Stop the instance safely
2. Wait up to 30 seconds for instance to reach stopped state
3. Verify the new instance type is supported in the region
4. Modify the instance attribute to change the instance type
5. 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:
1. Verify the target instance is accessible and in a valid state
2. Ensure the new instance type is supported in your region
3. Check AWS credentials and permissions
4. Review job logs for specific error messages
1 change: 1 addition & 0 deletions docs/learning/solutions/cost-management/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PagerDuty provides a solution that helps users start automating diagnostics quic

| Examples |
| --- |
| <img src="/assets/img/aws-logo.png" width="30" height="30"> [AWS - Change EC2 Size](/learning/solutions/cost-management/aws-change-ec2-size.md) |
| <img src="/assets/img/aws-logo.png" width="30" height="30"> [AWS - Identify Unused VPCs](/learning/solutions/cost-management/aws-list-unused-vpcs.md) |
| <img src="/assets/img/aws-logo.png" width="30" height="30"> [AWS - Identify Unused Lambda Functions](/learning/solutions/cost-management/aws-list-unused-lambda.md) |
| <img src="/assets/img/aws-logo.png" width="30" height="30"> [AWS - Identify Unused Security Groups](/learning/solutions/cost-management/aws-list-unused-securitygroups.md) |
Loading