turbot/steampipe-mod-aws-compliance

Control: 2.12 Ensure EBS volumes attached to an EC2 instance is marked for deletion upon instance termination

Description

This rule ensures that Amazon Elastic Block Store volumes that are attached to Amazon Elastic Compute Cloud (Amazon EC2) instances are marked for deletion when an instance is terminated. If an Amazon EBS volume isn’t deleted when the instance that it’s attached to is terminated, it may violate the concept of least functionality.

Remediation

From the Console:

  1. At this time the delete on termination setting for existing instances can only be changed using AWS CLI.

From the CLI:

  1. Run the modify-instance-attribute command using the list of instances collected in the audit.
aws ec2 modify-instance-attribute --instance-id i-123456abcdefghi0 --block-
device-mappings "[{\"DeviceName\":
\"/dev/sda\",\"Ebs\":{\"DeleteOnTermination\":true}}]"
  1. Repeat steps no. 1 with the other instances discovered in all AWS regions.

Note - If you get any errors running the modify-instance-attribute command confirm the instance id and the Device Name for that instance is correct. The above command is referencing the typical default device name.

Usage

Run the control in your terminal:

powerpipe control run aws_compliance.control.cis_compute_service_v100_2_12

Snapshot and share results via Turbot Pipes:

powerpipe login
powerpipe control run aws_compliance.control.cis_compute_service_v100_2_12 --share

SQL

This control uses a named query:

with ebs_volume_with_delete_on_termination_enabled as (
select
count(*) as count,
arn
from
aws_ec2_instance,
jsonb_array_elements(block_device_mappings) as p
where
p -> 'Ebs' ->> 'DeleteOnTermination' = 'false'
group by
arn
)
select
i.arn as resource,
case
when e.count > 0 then 'alarm'
else 'ok'
end as status,
case
when e.count > 0 then ' EBS volume(s) attached to ' || title || ' has delete on termination disabled.'
else ' EBS volume(s) attached to ' || title || ' has delete on termination enabled.'
end as reason
, region, account_id
from
aws_ec2_instance as i
left join ebs_volume_with_delete_on_termination_enabled as e on e.arn = i.arn;

Tags