Skip to content

Commit

Permalink
ec2_ami - Tag the image on creation when creating an image from an in…
Browse files Browse the repository at this point in the history
…stance (#551)

ec2_ami - Tag the image on creation when creating an image from an instance

SUMMARY
Tagging an instance during creation avoids the need to make an additional "tag" call on an untagged resource.
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_ami
ADDITIONAL INFORMATION
fixes: #550

Reviewed-by: Andy Thompson <None>
Reviewed-by: Mark Chappell <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
  • Loading branch information
tremble authored Oct 26, 2021
1 parent 88bd86e commit 788066e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/551-ec2_ami-tag-on-create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_ami - when creating an AMI from an instance pass the tagging options at creation time (https://github.com/ansible-collections/amazon.aws/pull/551).
7 changes: 5 additions & 2 deletions plugins/modules/ec2_ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,10 @@
from ..module_utils.core import AnsibleAWSModule
from ..module_utils.core import is_boto3_error_code
from ..module_utils.ec2 import AWSRetry
from ..module_utils.ec2 import boto3_tag_list_to_ansible_dict
from ..module_utils.ec2 import ensure_ec2_tags
from ..module_utils.ec2 import add_ec2_tags
from ..module_utils.tagging import boto3_tag_list_to_ansible_dict
from ..module_utils.tagging import boto3_tag_specifications
from ..module_utils.waiters import get_waiter


Expand Down Expand Up @@ -494,6 +495,8 @@ def create_image(module, connection):
if instance_id:
params['InstanceId'] = instance_id
params['NoReboot'] = no_reboot
if tags and module.botocore_at_least('1.19.30'):
params['TagSpecifications'] = boto3_tag_specifications(tags, types=['image', 'snapshot'])
image_id = connection.create_image(aws_retry=True, **params).get('ImageId')
else:
if architecture:
Expand Down Expand Up @@ -524,7 +527,7 @@ def create_image(module, connection):
waiter = get_waiter(connection, 'image_available')
waiter.wait(ImageIds=[image_id], WaiterConfig=dict(Delay=delay, MaxAttempts=max_attempts))

if tags:
if tags and 'TagSpecifications' not in params:
image_info = get_image_by_id(module, connection, image_id)
add_ec2_tags(connection, module, image_id, tags)
if image_info and image_info.get('BlockDeviceMappings'):
Expand Down

0 comments on commit 788066e

Please sign in to comment.