Elastic Cloud Compute (EC2)¶
Query¶
Basic JMESPath Query (see
c7n.filters.ValueFilter)Valid EC2 Query Filters (see EC2 Describe Instances)
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¶
Standard Value Filter (see Generic Filters)
ebsFilter 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": { "$ref": "#/definitions/filters_common/comparison_operators" }, "operator": { "enum": [ "and", "or" ] }, "skip-devices": { "items": { "type": "string" }, "type": "array" }, "value": { "$ref": "#/definitions/filters_common/value" }, "value_from": { "$ref": "#/definitions/filters_common/value_from" }, "value_type": { "$ref": "#/definitions/filters_common/value_types" } }
image-ageFilter on the age of the instance AMI based on the
ImageIdCreationDateSchema:
{ "days": { "type": "number" }, "op": { "$ref": "#/definitions/filters_common/comparison_operators" } }
imageFilter on the ImageId of the instance
Schema:
{ "default": { "type": "object" }, "key": { "type": "string" }, "op": { "$ref": "#/definitions/filters_common/comparison_operators" }, "value": { "$ref": "#/definitions/filters_common/value" }, "value_from": { "$ref": "#/definitions/filters_common/value_from" }, "value_type": { "$ref": "#/definitions/filters_common/value_types" } }
offhourFilter for
c7n.resources.ec2.InstanceOffHourSchema:
{ "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" } }
onhourFilter for
c7n.resources.ec2.InstanceOnHourSchema:
{ "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" } }
ephemeralFilter for instances that have ephemeral drives
Schema:
{}instance-uptimeFilter based on instance
LaunchTimein daysSchema:
{ "days": { "type": "number" }, "op": { "$ref": "#/definitions/filters_common/comparison_operators" } }
instance-ageFilter based on the
AttachTimeof the EBS Volumes in daysSchema:
{ "days": { "type": "number" }, "hours": { "type": "number" }, "minutes": { "type": "number" }, "op": { "$ref": "#/definitions/filters_common/comparison_operators" } }
termination-protectedFilter based on the
disableApiTerminationinstance attribute.Schema:
{}user-dataFilter for EC2’s with user data matching the value given.
Schema:
{ "default": { "type": "object" }, "key": { "type": "string" }, "op": { "$ref": "#/definitions/filters_common/comparison_operators" }, "value": { "$ref": "#/definitions/filters_common/value" }, "value_from": { "$ref": "#/definitions/filters_common/value_from" }, "value_type": { "$ref": "#/definitions/filters_common/value_types" } }
Actions¶
- Mark
Tag instances with mark instances matching filters with a
c7n_statustag 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)