Skip to content

Commit

Permalink
cloudformation_stack_set - Add a waiter to ensure that running operat…
Browse files Browse the repository at this point in the history
…ions against existing stacksets complete (ansible-collections#1790)

cloudformation_stack_set - Add a waiter to ensure that running operations against existing stacksets complete

SUMMARY
Add a waiter to ensure that running operations against existing stacksets complete. Current code would fail in cases where new instances need to be added since the previous update_stack_set(module, stack_params, cfn) would still be running.
Fixes ansible-collections#1608
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
cloudformation_stack_set
ADDITIONAL INFORMATION
I initially thought that the module was not idempotent since new instances wouldn't be added to my existing stack sets. Upon closer examination, the issue had to do with the fact that we had prior calls being made before adding new instances to existing stack sets:
raise error_class(parsed_response, operation_name)\nbotocore.errorfactory.OperationInProgressException: 
    An error occurred (OperationInProgressException) when calling the UpdateStackInstances operation:
    Another Operation on StackSet arn:aws:cloudformation:us-east-1:XXXXXX:stackset/aws-config-stackset:2bcb419a-f263-48ca-9fe0-cdef11fb59de is in progress
The error got triggered because of a missing waiter after this operation:
changed |= update_stack_set(module, stack_params, cfn)
This change add a waiter function after the update operation, which, in turn, ensure that the subsequent call to add stack instances to the stack set properly run.

Reviewed-by: Mark Chappell
  • Loading branch information
rmahroua authored May 5, 2023
1 parent acb9daf commit 2c91768
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/20230424-cloudformation_stack_set.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- cloudformation_stack_set - add a waiter to ensure that update operation complete before adding stack instances (https://github.com/ansible-collections/community.aws/issues/1608).
41 changes: 36 additions & 5 deletions plugins/modules/cloudformation_stack_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@
description: Test stack in two accounts
state: present
template_url: https://s3.amazonaws.com/my-bucket/cloudformation.template
accounts: [1234567890, 2345678901]
accounts:
- 123456789012
- 234567890123
regions:
- us-east-1
- us-east-1
- name: on subsequent calls, templates are optional but parameters and tags can be altered
community.aws.cloudformation_stack_set:
Expand All @@ -195,9 +197,11 @@
tags:
foo: bar
test: stack
accounts: [1234567890, 2345678901]
accounts:
- 123456789012
- 234567890123
regions:
- us-east-1
- us-east-1
- name: The same type of update, but wait for the update to complete in all stacks
community.aws.cloudformation_stack_set:
Expand All @@ -209,7 +213,26 @@
tags:
foo: bar
test: stack
accounts: [1234567890, 2345678901]
accounts:
- 123456789012
- 234567890123
regions:
- us-east-1
- name: Register new accounts (create new stack instances) with an existing stack set.
community.aws.cloudformation_stack_set:
name: my-stack
state: present
wait: true
parameters:
InstanceName: my_restacked_instance
tags:
foo: bar
test: stack
accounts:
- 123456789012
- 234567890123
- 345678901234
regions:
- us-east-1
"""
Expand Down Expand Up @@ -655,6 +678,14 @@ def main():
stack_params["OperationPreferences"] = get_operation_preferences(module)
changed |= update_stack_set(module, stack_params, cfn)

await_stack_set_operation(
module,
cfn,
operation_id=stack_params["OperationId"],
stack_set_name=stack_params["StackSetName"],
max_wait=module.params.get("wait_timeout"),
)

# now create/update any appropriate stack instances
new_stack_instances, existing_stack_instances, unspecified_stack_instances = compare_stack_instances(
cfn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
aws_secret_key: "{{ secondary_aws_secret_key }}"
security_token: "{{ secondary_security_token }}"
region: "{{ aws_region }}"
no_log: yes
no_log: true

- name: cloudformation_stack_set tests
- name: cloudformation_stack_set tests
collections:
- amazon.aws

Expand Down

0 comments on commit 2c91768

Please sign in to comment.