diff --git a/changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml b/changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml new file mode 100644 index 00000000000..8fbe2c09f4d --- /dev/null +++ b/changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml @@ -0,0 +1,2 @@ +bugfixes: + - ec2 - Correctly sets the end date of the Spot Instance request. Sets `ValidUntil` value in proper way so it will be auto-canceled through `spot_wait_timeout` interval. diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py index ceb28f0dde4..20c23b472d4 100644 --- a/lib/ansible/modules/cloud/amazon/ec2.py +++ b/lib/ansible/modules/cloud/amazon/ec2.py @@ -539,6 +539,7 @@ EXAMPLES = ''' ''' import time +import datetime import traceback from ast import literal_eval from distutils.version import LooseVersion @@ -1190,6 +1191,15 @@ def create_instances(module, ec2, vpc, override_count=None): count=count_remaining, type=spot_type, )) + + # Set spot ValidUntil + # ValidUntil -> (timestamp). The end date of the request, in + # UTC format (for example, YYYY -MM -DD T*HH* :MM :SS Z). + utc_valid_until = ( + datetime.datetime.utcnow() + + datetime.timedelta(seconds=spot_wait_timeout)) + params['valid_until'] = utc_valid_until.strftime('%Y-%m-%dT%H:%M:%S.000Z') + res = ec2.request_spot_instances(spot_price, **params) # Now we have to do the intermediate waiting