# AWS: Create or Terminate EC2 Instances

Launch new EC2 instances from an AMI, or terminate existing EC2 instances, as part of an incident runbook.


> **Availability:** See the [Incident Workflow Actions Overview table](/workflow-actions) for the plans this workflow is available on.

> **Premium Workflow:** This is a Premium Workflow billed separately from your PagerDuty plan. In the workflow action picker, this action appears as **EC2: Create Instances**.

> **Premium Workflow:** This is a Premium Workflow billed separately from your PagerDuty plan. In the workflow action picker, this action appears as **EC2: Terminate Instances**.

## Description

Launch new EC2 instances from an Amazon Machine Image (AMI), or terminate existing EC2 instances, as part of an incident runbook. Use **EC2: Create Instances** to launch replacement capacity, for example after a bad deploy or an instance failure. Use **EC2: Terminate Instances** to remove instances that are unhealthy or no longer needed.

These actions are useful for:

- Launching one or more replacement instances from a known-good AMI during an incident.
- Terminating instances that are confirmed unhealthy, misconfigured, or no longer needed.
- Automating instance lifecycle steps within a single workflow — create a replacement, verify it, and terminate the old instance.
- Recording created and terminated instance IDs and state transitions for incident notes or audits.

## Instructions

1. If you have not done so, follow the instructions to [Create an Incident Workflow](/ai-automation/automation/incident-workflows#create-an-incident-workflow).
2. When the instructions prompt you to [add actions](/ai-automation/automation/incident-workflows#add-actions), select **this action**.
3. Enter the following **Inputs** and click **Save**.
4. Continue following instructions to **Publish** the Workflow.
5. When the action runs, you will see the **Outputs** listed below.

## Inputs

> **Field References:** Fields with the **\{+\}** icon accept [Field References](/ai-automation/automation/incident-workflows#field-references), which can be useful for referencing incident data or outputs created in prior workflow steps. To add Field References, click **\{+\}**, or enter `{{`, and select relevant fields. Refer to the [Field References](/ai-automation/automation/incident-workflows#field-references) article for more information.

### EC2: Create Instances

| Name                     | Description                                                                                                   |
| :------------------------ | :------------------------------------------------------------------------------------------------------------ |
| Integration (Required)   | Select a [Workflow Integration](/ai-automation/automation/incident-workflows/workflow-integrations) or click **New AWS Connection** to establish a new one. |
| AWS Region               | The AWS region to launch instances in (for example, `us-west-2`). If left blank, the action uses `us-east-1`. |
| AMI ID (Required)        | The Amazon Machine Image ID to launch (for example, `ami-0abcdef1234567890`).                                 |
| Instance Type (Required) | The EC2 instance type (for example, `t3.micro`).                                                              |
| Min Count (Required)     | The minimum number of instances to launch. Must be a positive integer.                                        |
| Max Count (Required)     | The maximum number of instances to launch. Must be a positive integer greater than or equal to **Min Count**. |
| Key Name                 | The EC2 key pair name for SSH access.                                                                         |
| Security Group IDs       | One or more security group IDs, separated by commas.                                                          |
| Subnet ID                | The subnet ID to launch instances into.                                                                       |
| Tags                     | A JSON array of `{"Key": "...", "Value": "..."}` tag objects to apply to the created instances.               |

### EC2: Terminate Instances

| Name                    | Description                                                                                                            |
| :----------------------- | :----------------------------------------------------------------------------------------------------------------------- |
| Integration (Required)  | Select a [Workflow Integration](/ai-automation/automation/incident-workflows/workflow-integrations) or click **New AWS Connection** to establish a new one. |
| AWS Region              | The AWS region where the instances are located (for example, `us-west-2`). If left blank, the action uses `us-east-1`. |
| Instance IDs (Required) | One or more EC2 instance IDs to terminate, separated by commas.                                                        |

## Outputs

### EC2: Create Instances

| Name               | Description                                                                                                |
| :------------------ | :----------------------------------------------------------------------------------------------------------- |
| Created Instances  | A JSON array of created instances. Each object includes the instance ID, current state, and instance type. |
| Result             | Value that shows if the action was successful or not. Either "Success" or "Failed."                        |
| Result Summary     | Brief description of what the action did or if it failed.                                                  |
| Error              | Brief description that is populated if the action failed.                                                  |

### EC2: Terminate Instances

| Name                  | Description                                                                                 |
| :--------------------- | :--------------------------------------------------------------------------------------------- |
| Terminating Instances | A JSON array of instances and their state transitions, from previous state to current state. |
| Result                | Value that shows if the action was successful or not. Either "Success" or "Failed."          |
| Result Summary        | Brief description of what the action did or if it failed.                                    |
| Error                 | Brief description that is populated if the action failed.                                    |

> **Action Limitations:** - **EC2: Terminate Instances** is not Auto Scaling Group (ASG)-aware. It terminates any EC2 instances that you provide and has no option to decrement desired capacity. If an instance belongs to an ASG, terminating it with this action can trigger the ASG to launch a replacement immediately, which causes unexpected capacity changes. For controlled ASG termination, use [AWS: Terminate an EC2 Instance Belonging to an ASG](/workflow-actions/aws/aws-terminate-an-ec2-instance-belonging-to-an-asg), which supports a **Should Decrement Desired Capacity** option.

> **Tips:** - **Termination is irreversible**: After an instance is terminated, its instance state, local instance-store storage, and any EBS volumes not configured to persist are lost. Confirm the instance IDs before you run this action in an automated workflow.
> - **Field references**: Chain these actions with **EC2: Describe Instances**, [AWS: Fetch ASG Membership and Lifecycle Details](/workflow-actions/aws/aws-describe-autoscaling-instances), or incident custom details to populate **Instance IDs** or **AMI ID** dynamically instead of hardcoding them.
> - **Replacement runbooks**: A common pattern is to run **EC2: Create Instances** to launch a replacement, add a health check or a [Logic: Delay](/workflow-actions/logic/delay) step, and then run **EC2: Terminate Instances** to remove the bad instance, all in the same workflow.
