---
title: AMI shared with multiple accounts
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Infrastructure as Code (IaC)
  Security > IaC Security Rules > AMI shared with multiple accounts
---

# AMI shared with multiple accounts

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**Id:** `ansible-aws-ami-shared-with-multiple-accounts` 

**Provider:** AWS

**Platform:** Ansible

**Severity:** Medium

**Category:** Access Control

#### Learn More{% #learn-more %}

- [Provider Reference](https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_ami_module.html)

### Description{% #description %}

AMIs must not be broadly shared. Granting multiple AWS accounts or group-based access increases the attack surface and can expose embedded credentials, custom configurations, or vulnerable images to unintended parties.

For Ansible tasks using the `amazon.aws.ec2_ami` or `ec2_ami` modules, `launch_permissions` should be restricted to at most one explicit AWS account and must not include `group_names`. This rule flags tasks where `launch_permissions.group_names` is present or where `launch_permissions.user_ids` contains more than one entry.

Secure example with a single allowed account:

```yaml
- name: Register AMI with restricted launch permissions
  amazon.aws.ec2_ami:
    name: my-ami
    image_id: ami-0123456789abcdef0
    launch_permissions:
      user_ids:
        - "123456789012"
```

## Compliant Code Examples{% #compliant-code-examples %}

```yaml
- name: Allow AMI to be launched by another account V2
  amazon.aws.ec2_ami:
    name: my-ami
    image_id: '{{ instance.image_id }}'
    state: present
    launch_permissions:
      user_ids: ['123456789012']
```

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```yaml
- name: Update AMI Launch Permissions, making it public
  amazon.aws.ec2_ami:
    name: my-ami
    image_id: "{{ instance.image_id }}"
    state: present
    launch_permissions:
      group_names: ['all']
- name: Allow AMI to be launched by another account
  amazon.aws.ec2_ami:
    name: my-ami
    image_id: "{{ instance.image_id }}"
    state: present
    launch_permissions:
      user_ids: ['123456789012', '121212']
```
