| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2019, Tom De Keyser (@tdekeyser) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: stepfunctions_state_machine |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage AWS Step Functions state machines |
| 12 |
description: |
| 13 |
- Create, update and delete state machines in AWS Step Functions. |
| 14 |
- Calling the module in C(state=present) for an existing AWS Step Functions state machine |
| 15 |
will attempt to update the state machine definition, IAM Role, or tags with the provided data. |
| 16 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_step_functions_state_machine). |
| 17 |
The usage did not change. |
| 18 |
|
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- Name of the state machine. |
| 23 |
required: true |
| 24 |
type: str |
| 25 |
definition: |
| 26 |
description: |
| 27 |
- The Amazon States Language definition of the state machine. See |
| 28 |
U(https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html) for more |
| 29 |
information on the Amazon States Language. |
| 30 |
- Required when I(state=present). |
| 31 |
type: json |
| 32 |
role_arn: |
| 33 |
description: |
| 34 |
- The ARN of the IAM Role that will be used by the state machine for its executions. |
| 35 |
- Required when I(state=present). |
| 36 |
type: str |
| 37 |
state: |
| 38 |
description: |
| 39 |
- Desired state for the state machine. |
| 40 |
default: present |
| 41 |
choices: [ present, absent ] |
| 42 |
type: str |
| 43 |
|
| 44 |
author: |
| 45 |
- Tom De Keyser (@tdekeyser) |
| 46 |
|
| 47 |
extends_documentation_fragment: |
| 48 |
- amazon.aws.common.modules |
| 49 |
- amazon.aws.region.modules |
| 50 |
- amazon.aws.tags |
| 51 |
- amazon.aws.boto3 |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
# Create a new AWS Step Functions state machine |
| 56 |
- name: Setup HelloWorld state machine |
| 57 |
community.aws.stepfunctions_state_machine: |
| 58 |
name: "HelloWorldStateMachine" |
| 59 |
definition: "{{ lookup('file','state_machine.json') }}" |
| 60 |
role_arn: arn:aws:iam::123456789012:role/service-role/invokeLambdaStepFunctionsRole |
| 61 |
tags: |
| 62 |
project: helloWorld |
| 63 |
|
| 64 |
# Update an existing state machine |
| 65 |
- name: Change IAM Role and tags of HelloWorld state machine |
| 66 |
community.aws.stepfunctions_state_machine: |
| 67 |
name: HelloWorldStateMachine |
| 68 |
definition: "{{ lookup('file','state_machine.json') }}" |
| 69 |
role_arn: arn:aws:iam::123456789012:role/service-role/anotherStepFunctionsRole |
| 70 |
tags: |
| 71 |
otherTag: aDifferentTag |
| 72 |
|
| 73 |
# Remove the AWS Step Functions state machine |
| 74 |
- name: Delete HelloWorld state machine |
| 75 |
community.aws.stepfunctions_state_machine: |
| 76 |
name: HelloWorldStateMachine |
| 77 |
state: absent |
| 78 |
""" |
| 79 |
|
| 80 |
RETURN = r""" |
| 81 |
state_machine_arn: |
| 82 |
description: ARN of the AWS Step Functions state machine |
| 83 |
type: str |
| 84 |
returned: always |
| 85 |
""" |
| 86 |
|
| 87 |
try: |
| 88 |
from botocore.exceptions import BotoCoreError |
| 89 |
from botocore.exceptions import ClientError |
| 90 |
except ImportError: |
| 91 |
pass # caught by AnsibleAWSModule |
| 92 |
|
| 93 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 94 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 95 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 96 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 97 |
|
| 98 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 99 |
|
| 100 |
|
| 101 |
def manage_state_machine(state, sfn_client, module): |
| 102 |
state_machine_arn = get_state_machine_arn(sfn_client, module) |
| 103 |
|
| 104 |
if state == "present": |
| 105 |
if state_machine_arn is None: |
| 106 |
create(sfn_client, module) |
| 107 |
else: |
| 108 |
update(state_machine_arn, sfn_client, module) |
| 109 |
elif state == "absent": |
| 110 |
if state_machine_arn is not None: |
| 111 |
remove(state_machine_arn, sfn_client, module) |
| 112 |
|
| 113 |
check_mode(module, msg="State is up-to-date.") |
| 114 |
module.exit_json(changed=False, state_machine_arn=state_machine_arn) |
| 115 |
|
| 116 |
|
| 117 |
def create(sfn_client, module): |
| 118 |
check_mode(module, msg="State machine would be created.", changed=True) |
| 119 |
|
| 120 |
tags = module.params.get("tags") |
| 121 |
sfn_tags = ansible_dict_to_boto3_tag_list(tags, tag_name_key_name="key", tag_value_key_name="value") if tags else [] |
| 122 |
|
| 123 |
state_machine = sfn_client.create_state_machine( |
| 124 |
name=module.params.get("name"), |
| 125 |
definition=module.params.get("definition"), |
| 126 |
roleArn=module.params.get("role_arn"), |
| 127 |
tags=sfn_tags, |
| 128 |
) |
| 129 |
module.exit_json(changed=True, state_machine_arn=state_machine.get("stateMachineArn")) |
| 130 |
|
| 131 |
|
| 132 |
def remove(state_machine_arn, sfn_client, module): |
| 133 |
check_mode(module, msg=f"State machine would be deleted: {state_machine_arn}", changed=True) |
| 134 |
|
| 135 |
sfn_client.delete_state_machine(stateMachineArn=state_machine_arn) |
| 136 |
module.exit_json(changed=True, state_machine_arn=state_machine_arn) |
| 137 |
|
| 138 |
|
| 139 |
def update(state_machine_arn, sfn_client, module): |
| 140 |
tags_to_add, tags_to_remove = compare_tags(state_machine_arn, sfn_client, module) |
| 141 |
|
| 142 |
if params_changed(state_machine_arn, sfn_client, module) or tags_to_add or tags_to_remove: |
| 143 |
check_mode(module, msg=f"State machine would be updated: {state_machine_arn}", changed=True) |
| 144 |
|
| 145 |
sfn_client.update_state_machine( |
| 146 |
stateMachineArn=state_machine_arn, |
| 147 |
definition=module.params.get("definition"), |
| 148 |
roleArn=module.params.get("role_arn"), |
| 149 |
) |
| 150 |
sfn_client.untag_resource(resourceArn=state_machine_arn, tagKeys=tags_to_remove) |
| 151 |
sfn_client.tag_resource( |
| 152 |
resourceArn=state_machine_arn, |
| 153 |
tags=ansible_dict_to_boto3_tag_list(tags_to_add, tag_name_key_name="key", tag_value_key_name="value"), |
| 154 |
) |
| 155 |
|
| 156 |
module.exit_json(changed=True, state_machine_arn=state_machine_arn) |
| 157 |
|
| 158 |
|
| 159 |
def compare_tags(state_machine_arn, sfn_client, module): |
| 160 |
new_tags = module.params.get("tags") |
| 161 |
current_tags = sfn_client.list_tags_for_resource(resourceArn=state_machine_arn).get("tags") |
| 162 |
return compare_aws_tags( |
| 163 |
boto3_tag_list_to_ansible_dict(current_tags), new_tags if new_tags else {}, module.params.get("purge_tags") |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
def params_changed(state_machine_arn, sfn_client, module): |
| 168 |
""" |
| 169 |
Check whether the state machine definition or IAM Role ARN is different |
| 170 |
from the existing state machine parameters. |
| 171 |
""" |
| 172 |
current = sfn_client.describe_state_machine(stateMachineArn=state_machine_arn) |
| 173 |
return current.get("definition") != module.params.get("definition") or current.get("roleArn") != module.params.get( |
| 174 |
"role_arn" |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
def get_state_machine_arn(sfn_client, module): |
| 179 |
""" |
| 180 |
Finds the state machine ARN based on the name parameter. Returns None if |
| 181 |
there is no state machine with this name. |
| 182 |
""" |
| 183 |
target_name = module.params.get("name") |
| 184 |
all_state_machines = sfn_client.list_state_machines(aws_retry=True).get("stateMachines") |
| 185 |
|
| 186 |
for state_machine in all_state_machines: |
| 187 |
if state_machine.get("name") == target_name: |
| 188 |
return state_machine.get("stateMachineArn") |
| 189 |
|
| 190 |
|
| 191 |
def check_mode(module, msg="", changed=False): |
| 192 |
if module.check_mode: |
| 193 |
module.exit_json(changed=changed, output=msg) |
| 194 |
|
| 195 |
|
| 196 |
def main(): |
| 197 |
module_args = dict( |
| 198 |
name=dict(type="str", required=True), |
| 199 |
definition=dict(type="json"), |
| 200 |
role_arn=dict(type="str"), |
| 201 |
state=dict(choices=["present", "absent"], default="present"), |
| 202 |
tags=dict(default=None, type="dict", aliases=["resource_tags"]), |
| 203 |
purge_tags=dict(default=True, type="bool"), |
| 204 |
) |
| 205 |
module = AnsibleAWSModule( |
| 206 |
argument_spec=module_args, |
| 207 |
required_if=[("state", "present", ["role_arn"]), ("state", "present", ["definition"])], |
| 208 |
supports_check_mode=True, |
| 209 |
) |
| 210 |
|
| 211 |
sfn_client = module.client("stepfunctions", retry_decorator=AWSRetry.jittered_backoff(retries=5)) |
| 212 |
state = module.params.get("state") |
| 213 |
|
| 214 |
try: |
| 215 |
manage_state_machine(state, sfn_client, module) |
| 216 |
except (BotoCoreError, ClientError) as e: |
| 217 |
module.fail_json_aws(e, msg="Failed to manage state machine") |
| 218 |
|
| 219 |
|
| 220 |
if __name__ == "__main__": |
| 221 |
main() |