amazon.aws.ec2_ami module – Create or destroy an image (AMI) in EC2
Note
This module is part of the amazon.aws collection (version 8.2.1).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install amazon.aws
.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: amazon.aws.ec2_ami
.
New in amazon.aws 1.0.0
Synopsis
Registers or deregisters EC2 images.
Requirements
The below requirements are needed on the host that executes this module.
python >= 3.6
boto3 >= 1.26.0
botocore >= 1.29.0
Parameters
Parameter |
Comments |
---|---|
AWS access key ID. See the AWS documentation for more information about access tokens https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys. The The aws_access_key and profile options are mutually exclusive. The aws_access_key_id alias was added in release 5.1.0 for consistency with the AWS botocore SDK. The ec2_access_key alias has been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
The target architecture of the image to register. Default: |
|
The location of a CA Bundle to use when validating SSL certificates. The |
|
A dictionary to modify the botocore configuration. Parameters can be found in the AWS documentation https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config. |
|
A list of valid billing codes. To be used with valid accounts by AWS Marketplace vendors. |
|
The boot mode of the AMI. See the AWS documentation for more detail https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ami-boot.html. Support for Choices:
|
|
Use a The Choices:
|
|
Delete snapshots when deregistering the AMI. Choices:
|
|
Human-readable string describing the contents and purpose of the AMI. Default: |
|
List of device hashes/dictionaries with custom configurations (same block-device-mapping parameters). |
|
Whether the device should be automatically deleted when the Instance is terminated. Choices:
|
|
The device name. For example |
|
Whether the volume should be encrypted. Choices:
|
|
When using |
|
Suppresses the specified device included in the block device mapping of the AMI. Choices:
|
|
The ID of the Snapshot. |
|
The virtual name for the device. See the AWS documentation for more detail https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html. |
|
The size of the volume (in GiB). |
|
The volume type. Defaults to |
|
URL to connect to instead of the default AWS endpoints. While this can be used to connection to other AWS-compatible services the amazon.aws and community.aws collections are only tested against AWS. The The ec2_url and s3_url aliases have been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
A boolean representing whether enhanced networking with ENA is enabled or not. Choices:
|
|
Image ID to be deregistered. |
|
The S3 location of an image to use for the AMI. |
|
Instance ID to create the AMI from. |
|
The target kernel id of the image to register. |
|
Launch permissions for the AMI. You must pass all desired launch permissions if you wish to modify existing launch permissions (passing just groups will remove all users). |
|
List of group names. |
|
List of The Amazon Resource Name(s) (ARN) of organization(s). |
|
List of The Amazon Resource Name(s) (ARN) of an organizational unit(s) (OU). |
|
List of account IDs. |
|
The name of the new AMI. |
|
Flag indicating that the bundling process should not attempt to shutdown the instance before bundling. If this flag is Choices:
|
|
A named AWS profile to use for authentication. See the AWS documentation for more information about named profiles https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html. The The profile option is mutually exclusive with the aws_access_key, aws_secret_key and security_token options. |
|
If purge_tags=true and tags is set, existing tags will be purged from the resource to match exactly what is defined by tags parameter. If the tags parameter is not set then tags will not be modified, even if purge_tags=True. Tag keys beginning with Choices:
|
|
The ID of the RAM disk. |
|
The AWS region to use. For global services such as IAM, Route53 and CloudFront, region is ignored. The See the Amazon AWS documentation for more information http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region. The Support for the |
|
The root device name of the image to register. |
|
AWS secret access key. See the AWS documentation for more information about access tokens https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys. The The secret_key and profile options are mutually exclusive. The aws_secret_access_key alias was added in release 5.1.0 for consistency with the AWS botocore SDK. The ec2_secret_key alias has been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
AWS STS session token for use with temporary credentials. See the AWS documentation for more information about access tokens https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys. The The security_token and profile options are mutually exclusive. Aliases aws_session_token and session_token were added in release 3.2.0, with the parameter being renamed from security_token to session_token in release 6.0.0. The security_token, aws_security_token, and access_token aliases have been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
Set to simple to enable enhanced networking with the Intel 82599 Virtual Function interface for the AMI and any instances that you launch from the AMI. |
|
Register or deregister an AMI. Choices:
|
|
A dictionary representing the tags to be applied to the resource. If the tags parameter is not set then tags will not be modified. |
|
Set to If the image is configured for NitroTPM support, the value is Requires Requires an instance type that is compatible with Nitro. See the AWS documentation for more detail https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nitrotpm.html. |
|
Base64 representation of the non-volatile UEFI variable store. See the AWS documentation for more detail https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/uefi-secure-boot.html. |
|
When set to Setting validate_certs=false is strongly discouraged, as an alternative, consider setting aws_ca_bundle instead. Choices:
|
|
The virtualization type of the image to register. Default: |
|
Wait for the AMI to be in state ‘available’ before returning. Choices:
|
|
How long before wait gives up, in seconds. Default: |
Notes
Note
Caution: For modules, environment variables and configuration files are read from the Ansible ‘host’ context and not the ‘controller’ context. As such, files may need to be explicitly copied to the ‘host’. For lookup and connection plugins, environment variables and configuration files are read from the Ansible ‘controller’ context and not the ‘host’ context.
The AWS SDK (boto3) that Ansible uses may also read defaults for credentials and other settings, such as the region, from its configuration files in the Ansible ‘host’ context (typically
~/.aws/credentials
). See https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html for more information.
Examples
# Note: These examples do not set authentication details, see the AWS Guide for details.
- name: Basic AMI Creation
amazon.aws.ec2_ami:
instance_id: i-xxxxxx
wait: true
name: newtest
tags:
Name: newtest
Service: TestService
- name: Basic AMI Creation, without waiting
amazon.aws.ec2_ami:
instance_id: i-xxxxxx
wait: false
name: newtest
- name: AMI Registration from EBS Snapshot
amazon.aws.ec2_ami:
name: newtest
state: present
architecture: x86_64
virtualization_type: hvm
root_device_name: /dev/xvda
device_mapping:
- device_name: /dev/xvda
volume_size: 8
snapshot_id: snap-xxxxxxxx
delete_on_termination: true
volume_type: gp2
- name: AMI Creation, with a custom root-device size and another EBS attached
amazon.aws.ec2_ami:
instance_id: i-xxxxxx
name: newtest
device_mapping:
- device_name: /dev/sda1
size: XXX
delete_on_termination: true
volume_type: gp2
- device_name: /dev/sdb
size: YYY
delete_on_termination: false
volume_type: gp2
- name: AMI Creation, excluding a volume attached at /dev/sdb
amazon.aws.ec2_ami:
instance_id: i-xxxxxx
name: newtest
device_mapping:
- device_name: /dev/sda1
size: XXX
delete_on_termination: true
volume_type: gp2
- device_name: /dev/sdb
no_device: true
- name: AMI Creation with boot_mode and tpm_support
amazon.aws.ec2_ami:
name: newtest
state: present
architecture: x86_64
virtualization_type: hvm
root_device_name: /dev/sda1
device_mapping:
- device_name: /dev/sda1
snapshot_id: "{{ snapshot_id }}"
wait: true
region: us-east-1
boot_mode: uefi
uefi_data: data_file.bin
tpm_support: v2.0
- name: Deregister/Delete AMI (keep associated snapshots)
amazon.aws.ec2_ami:
image_id: "{{ instance.image_id }}"
delete_snapshot: false
state: absent
- name: Deregister AMI (delete associated snapshots too)
amazon.aws.ec2_ami:
image_id: "{{ instance.image_id }}"
delete_snapshot: true
state: absent
- name: Update AMI Launch Permissions, making it public
amazon.aws.ec2_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:
image_id: "{{ instance.image_id }}"
state: present
launch_permissions:
user_ids: ['123456789012']
- name: Update AMI Launch Permissions, share AMI across an Organization and Organizational Units
amazon.aws.ec2_ami:
image_id: "{{ instance.image_id }}"
state: present
launch_permissions:
org_arns: ['arn:aws:organizations::123456789012:organization/o-123ab4cdef']
org_unit_arns: ['arn:aws:organizations::123456789012:ou/o-123example/ou-1234-5example']
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Architecture of image. Returned: when AMI is created or already exists Sample: |
|
Block device mapping associated with image. Returned: when AMI is created or already exists Sample: |
|
Creation date of image. Returned: when AMI is created or already exists Sample: |
|
Description of image. Returned: when AMI is created or already exists Sample: |
|
Specifies whether enhanced networking with ENA is enabled. Returned: when AMI is created or already exists Sample: |
|
Type of hypervisor. Returned: when AMI is created or already exists Sample: |
|
ID of the image. Returned: when AMI is created or already exists Sample: |
|
The owner alias ( amazon | aws-marketplace). Returned: when AMI is created or already exists Sample: |
|
Type of image. Returned: when AMI is created or already exists Sample: |
|
Whether image is public. Returned: when AMI is created or already exists Sample: |
|
The kernel associated with the image, if any. Only applicable for machine images. Returned: when AMI is created or already exists Sample: |
|
Permissions allowing other accounts to access the AMI. Returned: when AMI is created or already exists Sample: |
|
Location of image. Returned: when AMI is created or already exists Sample: |
|
AMI name of image. Returned: when AMI is created or already exists Sample: |
|
Owner of image. Returned: when AMI is created or already exists Sample: |
|
Platform of image. Returned: when AMI is created or already exists Sample: |
|
Any product codes associated with the AMI. Returned: when AMI is created or already exists Sample: |
|
The RAM disk associated with the image, if any. Only applicable for machine images. Returned: when AMI is created or already exists |
|
Root device name of image. Returned: when AMI is created or already exists Sample: |
|
Root device type of image. Returned: when AMI is created or already exists Sample: |
|
A list of snapshot ids deleted after deregistering image. Returned: after AMI is deregistered, if delete_snapshot=true Sample: |
|
Specifies whether enhanced networking with the Intel 82599 Virtual Function interface is enabled. Returned: when AMI is created or already exists Sample: |
|
State of image. Returned: when AMI is created or already exists Sample: |
|
The reason for the state change. Returned: when AMI is created or already exists Sample: |
|
A dictionary of tags assigned to image. Returned: when AMI is created or already exists Sample: |
|
Image virtualization type. Returned: when AMI is created or already exists Sample: |