# Buggy code
def main():
    argument_spec = ec2_argument_spec()
    argument_spec.update(
        dict(
            name=dict(type="str", required=True),
            state=dict(
                type="str",
                default="present",
                choices=["present", "absent", "stopped", "running", "restarted"],
            ),
            zone=dict(type="str"),
            blueprint_id=dict(type="str"),
            bundle_id=dict(type="str"),
            key_pair_name=dict(type="str"),
            user_data=dict(type="str"),
            wait=dict(type="bool", default=True),
            wait_timeout=dict(default=300, type="int"),
        )
    )

    module = AnsibleModule(argument_spec=argument_spec)

    if not HAS_BOTO3:
        module.fail_json(msg='Python module "boto3" is missing, please install it')

    if not HAS_BOTOCORE:
        module.fail_json(msg='Python module "botocore" is missing, please install it')

    try:
        core(module)
    except (botocore.exceptions.ClientError, Exception) as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())


# Traceback (ParamValidationError)
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_Vslj3B/ansible_module_lightsail.py", line 467, in main
core(module)
File "/tmp/ansible_Vslj3B/ansible_module_lightsail.py", line 429, in core
changed, instance_dict = create_instance(module, client, name)
File "/tmp/ansible_Vslj3B/ansible_module_lightsail.py", line 219, in create_instance
keyPairName=key_pair_name,
File "/home/jack/git/lightsail/ansible/env/local/lib/python2.7/site-packages/botocore/client.py", line 324, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/jack/git/lightsail/ansible/env/local/lib/python2.7/site-packages/botocore/client.py", line 596, in _make_api_call
api_params, operation_model, context=request_context)
File "/home/jack/git/lightsail/ansible/env/local/lib/python2.7/site-packages/botocore/client.py", line 631, in _convert_to_request_dict
api_params, operation_model)
File "/home/jack/git/lightsail/ansible/env/local/lib/python2.7/site-packages/botocore/validate.py", line 291, in serialize_to_request
raise ParamValidationError(report=report.generate_report())
ParamValidationError: Parameter validation failed:
Invalid type for parameter keyPairName, value: None, type: <type 'NoneType'>, valid types: <type 'basestring'>

fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"aws_access_key": null,
"aws_secret_key": null,
"blueprint_id": "ubuntu_16_04",
"bundle_id": "nano_1_0",
"ec2_url": null,
"key_pair_name": null,
"name": "test",
"profile": null,
"region": "eu-west-1",
"security_token": null,
"state": "present",
"user_data": null,
"validate_certs": true,
"wait": true,
"wait_timeout": "500",
"zone": "eu-west-1a"
}
},
"msg": "Parameter validation failed:\nInvalid type for parameter keyPairName, value: None, type: <type 'NoneType'>, valid types: <type 'basestring'>"
}

# Fixed code
def main():
    argument_spec = dict(
        name=dict(type="str", required=True),
        state=dict(
            type="str",
            default="present",
            choices=[
                "present",
                "absent",
                "stopped",
                "running",
                "restarted",
                "rebooted",
            ],
        ),
        zone=dict(type="str"),
        blueprint_id=dict(type="str"),
        bundle_id=dict(type="str"),
        key_pair_name=dict(type="str"),
        user_data=dict(type="str", default=""),
        wait=dict(type="bool", default=True),
        wait_timeout=dict(default=300, type="int"),
    )

    module = AnsibleAWSModule(
        argument_spec=argument_spec,
        required_if=[["state", "present", ("zone", "blueprint_id", "bundle_id")]],
    )

    client = module.client("lightsail")

    name = module.params.get("name")
    state = module.params.get("state")

    if state == "present":
        create_instance(module, client, name)
    elif state == "absent":
        delete_instance(module, client, name)
    elif state in ("running", "stopped"):
        start_or_stop_instance(module, client, name, state)
    elif state in ("restarted", "rebooted"):
        restart_instance(module, client, name)