# Buggy code
def create_update_parameter(client, module):
    changed = False
    existing_parameter = None
    response = {}

    args = dict(
        Name=module.params.get("name"),
        Value=module.params.get("value"),
        Type=module.params.get("string_type"),
    )

    if module.params.get("overwrite_value") == "always" or "changed":
        args.update(Overwrite=True)
    else:
        args.update(Overwrite=False)

    if module.params.get("description"):
        args.update(Description=module.params.get("description"))

    if module.params.get("string_type") == "SecureString":
        args.update(KeyId=module.params.get("key_id"))

    try:
        existing_parameter = client.get_parameter(
            Name=args["Name"], WithDecryption=True
        )
    except:
        pass

    if existing_parameter:
        if module.params.get("overwrite_value") == "always":
            (changed, response) = update_parameter(client, module, args)

        elif module.params.get("overwrite_value") == "changed":
            if existing_parameter["Parameter"]["Type"] != args["Type"]:
                (changed, response) = update_parameter(client, module, args)

            if existing_parameter["Parameter"]["Value"] != args["Value"]:
                (changed, response) = update_parameter(client, module, args)

            if args["Description"]:
                # Description field not available from get_parameter function so get it from describe_parameters
                describe_existing_parameter = None
                try:
                    describe_existing_parameter = client.describe_parameters(
                        Filters=[{"Key": "Name", "Values": [args["Name"]]}]
                    )
                except ClientError as e:
                    module.fail_json_aws(e, msg="getting description value")

                if (
                    describe_existing_parameter["Parameters"][0]["Description"]
                    != args["Description"]
                ):
                    (changed, response) = update_parameter(client, module, args)
    else:
        (changed, response) = update_parameter(client, module, args)

    return changed, response


# Traceback (KeyError)
TASK [Test aws_ssm_parameter_store] ******************************************************
task path: /home/stevek/deployments/act2-ansible/tests/test-ssm-parameter-store.yml:4
Thursday 19 July 2018  11:30:12 +1000 (0:00:00.639)       0:00:00.671 *********
Using module_utils file /home/stevek/deployments/act2-ansible/module_utils/ec2.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/aws/core.py
Using module_utils file /home/stevek/deployments/act2-ansible/module_utils/aws/__init__.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/_text.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/cloud.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/six/__init__.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/basic.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/parsing/__init__.py
Using module_utils file /usr/local/lib/python2.7/dist-packages/ansible/module_utils/pycompat24.py
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/cloud/amazon/aws_ssm_parameter_store.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: stevek
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_L5Bvi2/ansible_module_aws_ssm_parameter_store.py", line 253, in <module>
main()
File "/tmp/ansible_L5Bvi2/ansible_module_aws_ssm_parameter_store.py", line 248, in main
(changed, response) = invocations[state](client, module)
File "/tmp/ansible_L5Bvi2/ansible_module_aws_ssm_parameter_store.py", line 184, in create_update_parameter
if args['Description']:
KeyError: 'Description'

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_L5Bvi2/ansible_module_aws_ssm_parameter_store.py\", line 253, in <module>\n    main()\n  File \"/tmp/ansible_L5Bvi2/ansible_module_aws_ssm_parameter_store.py\", line 248, in main\n    (changed, response) = invocations[state](client, module)\n  File \"/tmp/ansible_L5Bvi2/ansible_module_aws_ssm_parameter_store.py\", line 184, in create_update_parameter\n    if args['Description']:\nKeyError: 'Description'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
to retry, use: --limit @/home/stevek/.ansible-retry/test-ssm-parameter-store.retry

PLAY RECAP *******************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

# Fixed code
def create_update_parameter(client, module):
    changed = False
    existing_parameter = None
    response = {}

    args = dict(
        Name=module.params.get("name"),
        Value=module.params.get("value"),
        Type=module.params.get("string_type"),
    )

    if module.params.get("overwrite_value") == "always" or "changed":
        args.update(Overwrite=True)
    else:
        args.update(Overwrite=False)

    if module.params.get("description"):
        args.update(Description=module.params.get("description"))

    if module.params.get("string_type") == "SecureString":
        args.update(KeyId=module.params.get("key_id"))

    try:
        existing_parameter = client.get_parameter(
            Name=args["Name"], WithDecryption=True
        )
    except:
        pass

    if existing_parameter:
        if module.params.get("overwrite_value") == "always":
            (changed, response) = update_parameter(client, module, args)

        elif module.params.get("overwrite_value") == "changed":
            if existing_parameter["Parameter"]["Type"] != args["Type"]:
                (changed, response) = update_parameter(client, module, args)

            if existing_parameter["Parameter"]["Value"] != args["Value"]:
                (changed, response) = update_parameter(client, module, args)

            if args.get("Description"):
                # Description field not available from get_parameter function so get it from describe_parameters
                describe_existing_parameter = None
                try:
                    describe_existing_parameter = client.describe_parameters(
                        Filters=[{"Key": "Name", "Values": [args["Name"]]}]
                    )
                except ClientError as e:
                    module.fail_json_aws(e, msg="getting description value")

                if (
                    describe_existing_parameter["Parameters"][0]["Description"]
                    != args["Description"]
                ):
                    (changed, response) = update_parameter(client, module, args)
    else:
        (changed, response) = update_parameter(client, module, args)

    return changed, response