Automatically terminate AWS EC2 instances

Latest Dynatrace

AWS EC2 AWS Connector offers a wide range of instance types optimized for different use cases, enabling appropriate scaling of cloud resources. Often, such instances are left running or stopped but never terminated, increasing recurring costs. The AWS Connector action called EC2: Terminate instances can automatically terminate such instances based on highly customizable conditions.

Target audience

This page is intended for cloud engineers looking for a mechanism to terminate already-stopped AWS EC2 instances automatically.

What will you learn

You'll learn how to set up and run a workflow with the EC2: Terminate instances action to terminate already stopped AWS EC2 instances.

Prerequisites

Set up AWS Connector.

Steps

To set up a workflow to terminate already stopped AWS EC2 instances

  1. On the Workflows Workflows overview page, select Add Workflow.

  2. Select the default title Untitled Workflow, and copy and paste the workflow title Terminate every stopped AWS EC2 instance.

  3. In the Select a trigger panel, select the trigger type On Demand.

  4. Select Add Add task on the trigger node.

  5. Add the EC2: Describe Instances action.

    1. In the Choose action section, search for EC2.
    2. Select the EC2: Describe Instances action.
    3. Rename it to list-stopped-instances. This action will fetch all instances that are stopped currently:
    4. In the Input tab
      1. Select a Region.
      2. Add an entry to Filters with the key instance-state-code and value 80 to filter for stopped instances.
  6. Add a Run JavaScript action, and rename it to extract-instanceids. This action will perform data mapping for the subsequent actions.

    1. In the Input tab, add the following code

      import { execution } from '@dynatrace-sdk/automation-utils';
      import { executionsClient, actionExecutionsClient } from '@dynatrace-sdk/client-automation';
      export default async function ({ execution_id, action_execution_id }) {
      const item = (await actionExecutionsClient.getActionExecution({ id: action_execution_id })).loopItem['reservations']
      const instanceIds = item.Instances.map(instance => instance.InstanceId);
      return { InstanceIds: instanceIds };
      }
    2. In the Options tab

      1. Toggle on the Loop task option.
      2. In the Item variable name, enter reservations.
      3. In the List box, enter {{ result("list-stopped-instances").Reservations }}.
  7. Add the EC2: Terminate instances action, and rename it to terminate-instances. This action will terminate the instances that were returned in the previous step.

    1. In the Input tab
      1. Select a Region.
      2. In the InstanceIds box, enter {{ _.instances.InstanceIds }}.
    2. In the Options tab
      1. Toggle on the Loop task option.
      2. In the Item variable name, enter instances.
      3. In the List box, enter {{ result("extract-instanceids") }}.
  8. Add an EC2: Create tags action, and rename it to create-termination-date-tag. This action will add a tag to the instances with the time of termination.

    1. In the Input tab
      1. Select a Region.
      2. In Resources box, enter {{ _.instances.InstanceIds }}.
      3. In Tags box, enter the key Termination_date and the value {{ now() }}.
    2. In the Options tab
      1. Toggle on the Loop task option.
      2. In the Item variable name, enter instances.
      3. In the List box, enter {{ result("extract-instanceids") }}.
  9. Select Save.

    Screenshot of the Terminate every stopped AWS EC2 instances workflow with terminate instances action

  10. Select Run.

    Screenshot of a successful run of the Terminate stopped AWS EC2 instances workflow

Summary

Now, you can save costs by using a Dynatrace workflow that automatically terminates every stopped AWS EC2 instance.