Elastic Cloud Compute (EC2)

Query

EC2_VALID_FILTERS = {
    'architecture': ('i386', 'x86_64'),
    'availability-zone': str,
    'iam-instance-profile.arn': str,
    'image-id': str,
    'instance-id': str,
    'instance-lifecycle': ('spot',),
    'instance-state-name': (
        'pending',
        'terminated',
        'running',
        'shutting-down',
        'stopping',
        'stopped'),
    'instance.group-id': str,
    'instance.group-name': str,
    'tag-key': str,
    'tag-value': str,
    'tag:': str,
    'tenancy': ('dedicated', 'default', 'host'),
    'vpc-id': str}

Filters

ebs
Filter based on Volumes attached to Instance
Filter by State Transition Filter

Filter instances by state (see Instance Lifecycle)

Schema:
{
  "default": {
    "type": "object"
  },
  "key": {
    "type": "string"
  },
  "op": {
    "enum": [
      "regex",
      "glob",
      "not-in",
      "equal",
      "gt",
      "less-than",
      "not-equal",
      "lte",
      "contains",
      "in",
      "intersect",
      "ne",
      "greater-than",
      "eq",
      "difference",
      "ge",
      "gte",
      "lt",
      "le",
      "ni"
    ]
  },
  "operator": {
    "enum": [
      "and",
      "or"
    ]
  },
  "skip-devices": {
    "items": {
      "type": "string"
    },
    "type": "array"
  },
  "value": {
    "oneOf": [
      {
        "type": "array"
      },
      {
        "type": "string"
      },
      {
        "type": "boolean"
      },
      {
        "type": "number"
      },
      {
        "type": "null"
      }
    ]
  },
  "value_from": {
    "additionalProperties": "False",
    "properties": {
      "expr": {
        "oneOf": [
          {
            "type": "integer"
          },
          {
            "type": "string"
          }
        ]
      },
      "format": {
        "enum": [
          "csv",
          "json",
          "txt",
          "csv2dict"
        ]
      },
      "url": {
        "type": "string"
      }
    },
    "required": [
      "url"
    ],
    "type": "object"
  },
  "value_type": {
    "enum": [
      "age",
      "integer",
      "expiration",
      "normalize",
      "size",
      "cidr",
      "cidr_size",
      "swap",
      "resource_count",
      "expr",
      "unique_size"
    ]
  }
}
image-age

Filter on the age of the instance AMI based on the ImageId CreationDate

Schema:
{
  "days": {
    "type": "number"
  },
  "op": {
    "enum": [
      "regex",
      "glob",
      "not-in",
      "equal",
      "gt",
      "less-than",
      "not-equal",
      "lte",
      "contains",
      "in",
      "intersect",
      "ne",
      "greater-than",
      "eq",
      "difference",
      "ge",
      "gte",
      "lt",
      "le",
      "ni"
    ],
    "type": "string"
  }
}
image

Filter on the ImageId of the instance

Schema:
{
  "default": {
    "type": "object"
  },
  "key": {
    "type": "string"
  },
  "op": {
    "enum": [
      "regex",
      "glob",
      "not-in",
      "equal",
      "gt",
      "less-than",
      "not-equal",
      "lte",
      "contains",
      "in",
      "intersect",
      "ne",
      "greater-than",
      "eq",
      "difference",
      "ge",
      "gte",
      "lt",
      "le",
      "ni"
    ]
  },
  "value": {
    "oneOf": [
      {
        "type": "array"
      },
      {
        "type": "string"
      },
      {
        "type": "boolean"
      },
      {
        "type": "number"
      },
      {
        "type": "null"
      }
    ]
  },
  "value_from": {
    "additionalProperties": "False",
    "properties": {
      "expr": {
        "oneOf": [
          {
            "type": "integer"
          },
          {
            "type": "string"
          }
        ]
      },
      "format": {
        "enum": [
          "csv",
          "json",
          "txt",
          "csv2dict"
        ]
      },
      "url": {
        "type": "string"
      }
    },
    "required": [
      "url"
    ],
    "type": "object"
  },
  "value_type": {
    "enum": [
      "age",
      "integer",
      "expiration",
      "normalize",
      "size",
      "cidr",
      "cidr_size",
      "swap",
      "resource_count",
      "expr",
      "unique_size"
    ]
  }
}
offhour

Filter for c7n.resources.ec2.InstanceOffHour

Schema:
{
  "default_tz": {
    "required": true,
    "type": "string"
  },
  "offhour": {
    "maximum": 23,
    "minimum": 0,
    "required": true,
    "type": "integer"
  },
  "opt-out": {
    "type": "boolean"
  },
  "skip-days": {
    "items": {
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}",
      "type": "string"
    },
    "type": "array"
  },
  "skip-days-from": {
    "additionalProperties": "False",
    "properties": {
      "expr": {
        "oneOf": [
          {
            "type": "integer"
          },
          {
            "type": "string"
          }
        ]
      },
      "format": {
        "enum": [
          "csv",
          "json",
          "txt",
          "csv2dict"
        ]
      },
      "url": {
        "type": "string"
      }
    },
    "required": [
      "url"
    ],
    "type": "object"
  },
  "tag": {
    "type": "string"
  },
  "weekends": {
    "type": "boolean"
  },
  "weekends-only": {
    "type": "boolean"
  }
}
onhour

Filter for c7n.resources.ec2.InstanceOnHour

Schema:
{
  "default_tz": {
    "required": true,
    "type": "string"
  },
  "onhour": {
    "maximum": 23,
    "minimum": 0,
    "required": true,
    "type": "integer"
  },
  "opt-out": {
    "type": "boolean"
  },
  "skip-days": {
    "items": {
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}",
      "type": "string"
    },
    "type": "array"
  },
  "skip-days-from": {
    "additionalProperties": "False",
    "properties": {
      "expr": {
        "oneOf": [
          {
            "type": "integer"
          },
          {
            "type": "string"
          }
        ]
      },
      "format": {
        "enum": [
          "csv",
          "json",
          "txt",
          "csv2dict"
        ]
      },
      "url": {
        "type": "string"
      }
    },
    "required": [
      "url"
    ],
    "type": "object"
  },
  "tag": {
    "type": "string"
  },
  "weekends": {
    "type": "boolean"
  },
  "weekends-only": {
    "type": "boolean"
  }
}
ephemeral

Filter for instances that have ephemeral drives

Schema:
{}
instance-uptime

Filter based on instance LaunchTime in days

Schema:
{
  "days": {
    "type": "number"
  },
  "op": {
    "enum": [
      "regex",
      "glob",
      "not-in",
      "equal",
      "gt",
      "less-than",
      "not-equal",
      "lte",
      "contains",
      "in",
      "intersect",
      "ne",
      "greater-than",
      "eq",
      "difference",
      "ge",
      "gte",
      "lt",
      "le",
      "ni"
    ],
    "type": "string"
  }
}
instance-age

Filter based on the AttachTime of the EBS Volumes in days

Schema:
{
  "days": {
    "type": "number"
  },
  "hours": {
    "type": "number"
  },
  "minutes": {
    "type": "number"
  },
  "op": {
    "enum": [
      "regex",
      "glob",
      "not-in",
      "equal",
      "gt",
      "less-than",
      "not-equal",
      "lte",
      "contains",
      "in",
      "intersect",
      "ne",
      "greater-than",
      "eq",
      "difference",
      "ge",
      "gte",
      "lt",
      "le",
      "ni"
    ],
    "type": "string"
  }
}
termination-protected

Filter based on the disableApiTermination instance attribute.

Schema:
{}

Actions

Mark

Tag instances with mark instances matching filters with a c7n_status tag by default and configurable value. Here’s an example of renaming an extant tag:

policies:
  - name: ec2-tag-instances
    resource: ec2
    filters:
      - "tag:CostCenter": foobar
    actions:
      - type: mark
        tag: CostCenter
        msg: barrum
Start

Start a set of instances (presumably) already stopped, the start action will automatically filter instances to those that are already in the correct state.

Schema:
{}

This example will restart all stopped instances.

policies:
  - name: ec2-start
    resources: ec2
    actions:
      - start
Stop

Will stop the instances. Stopped instances do not incur EC2 instance costs.

Schema:
{
  "terminate-ephemeral": {
    "type": "boolean"
  }
}
Terminate

Will terminate the instances. Use with caution!

Schema:
{
  "force": {
    "type": "boolean"
  }
}
Snapshot

Snapshots the instances’ attached EBS volumes.

Schema:
{
  "copy-tags": {
    "items": {
      "type": "string"
    },
    "type": "array"
  }
}

This example will create snapshots for all instances and copy the Owner tag value from the instance to the new snapshot.

policies:
   - name: ec2-nightly-backup
     resource: ec2
     actions:
       - type: snapshot
         copy-tags:
           - Owner
PropagateSpotTags

In case the EC2 instance is a Spot Instance, created by a Spot Instance Request, this action will propagate all (or a subset) of the Tags that are attached to the original Spot Instance Request. Spot Instance Request do not propagate their tags to the Spot Instances. (see Tagging Spot Instance Requests)

However, Spot Fleets are said to propagate their Tags. (see Tag Your Spot Fleet EC2 Instances)

Schema:
{
  "only_tags": {
    "items": {
      "type": "string"
    },
    "type": "array"
  }
}

This example will copy the Name and the BillingTag tag values from the Spot Instance Request to the pending EC2 instances (only if they are Spot Instances)