Repository: ansible/ansible

Issue:
EC2 instance_initiated_shutdown_behavior default should be more intelligent
From @omarqureshi on 2016-11-29T14:42:53Z
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
EC2

##### ANSIBLE VERSION
```
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/usr/share/ansible']

```
##### OS / ENVIRONMENT
N/A

##### SUMMARY
When creating an instance-store backed instance, the default of "stop" on instance_initiated_shutdown_behavior is not a valid default. Explicitly setting to "terminate" resolves the issue.

##### STEPS TO REPRODUCE
Running a playbook with the following task

<!--- Paste example playbooks or commands between quotes below -->
```
    - name: create web servers instance 1
      ec2:
        image: "{{ image }}"
        instance_type: "{{ instance_type }}"
        keypair: "{{ keypair }}"
        instance_tags:
          Name: "{{ role }}-v2-01"
          service: "tn"
        region: "{{ region }}"
        zone: "{{ region }}a"
        group: "{{ aws_security_group }}"
        wait: true
        monitoring: no
        exact_count: 1
        count_tag: 
          Name: "{{ role }}-v2-01"
      register: ec2_info

```

##### EXPECTED RESULTS
Instance to be created

##### ACTUAL RESULTS
Playbook bombs out
```
fatal: [localhost]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "assign_public_ip": false, 
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "count": 1, 
            "count_tag": "{'Name': 'talis-com-server-v2-01'}", 
            "ebs_optimized": false, 
            "ec2_url": null, 
            "exact_count": 1, 
            "group": [
                "staging", 
                "talis.com-auto-v2"
            ], 
            "group_id": null, 
            "id": null, 
            "image": "ami-bd36b0ce", 
            "instance_ids": null, 
            "instance_initiated_shutdown_behavior": null, 
            "instance_profile_name": null, 
            "instance_tags": {
                "Name": "talis-com-server-v2-01", 
                "service": "tn"
            }, 
            "instance_type": "m1.small", 
            "kernel": null, 
            "key_name": "keypair", 
            "keypair": "keypair", 
            "monitoring": false, 
            "network_interfaces": null, 
            "placement_group": null, 
            "private_ip": null, 
            "profile": null, 
            "ramdisk": null, 
            "region": "eu-west-1", 
            "security_token": null, 
            "source_dest_check": true, 
            "spot_launch_group": null, 
            "spot_price": null, 
            "spot_type": "one-time", 
            "spot_wait_timeout": "600", 
            "state": "present", 
            "tenancy": "default", 
            "termination_protection": null, 
            "user_data": null, 
            "validate_certs": true, 
            "volumes": null, 
            "vpc_subnet_id": null, 
            "wait": true, 
            "wait_timeout": "300", 
            "zone": "eu-west-1a"
        }, 
        "module_name": "ec2"
    }, 
    "msg": "Instance creation failed => InvalidParameterCombination: The attribute instanceInitiatedShutdownBehavior can only be used for EBS-backed images."
}
```

Copied from original issue: ansible/ansible-modules-core#5759



Discussion:
From @omarqureshi on 2016-11-29T14:42:53Z

https://github.com/ansible/ansible-modules-core/blob/52d376c88e3456535e20eb45287392bef379cc70/cloud/amazon/ec2.py#L1131 is where the general check is
From @ansibot on 2016-11-29T14:42:53Z

@ansible, ping. This issue is waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: issue_notify_maintainer --->
cc @lwade @tgerla
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
This will be fixed by #28885.

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py
--- a/lib/ansible/modules/cloud/amazon/ec2.py
+++ b/lib/ansible/modules/cloud/amazon/ec2.py
@@ -227,7 +227,8 @@
   instance_initiated_shutdown_behavior:
     version_added: "2.2"
     description:
-    - Set whether AWS will Stop or Terminate an instance on shutdown
+    - Set whether AWS will Stop or Terminate an instance on shutdown. This parameter is ignored when using instance-store
+      images (which require termination on shutdown).
     required: false
     default: 'stop'
     choices: [ "stop", "terminate" ]
@@ -1193,7 +1194,16 @@ def create_instances(module, ec2, vpc, override_count=None):
                 # (the default) or 'terminate' here.
                 params['instance_initiated_shutdown_behavior'] = instance_initiated_shutdown_behavior or 'stop'
 
-                res = ec2.run_instances(**params)
+                try:
+                    res = ec2.run_instances(**params)
+                except boto.exception.EC2ResponseError as e:
+                    if (params['instance_initiated_shutdown_behavior'] != 'terminate' and
+                            "InvalidParameterCombination" == e.error_code):
+                        params['instance_initiated_shutdown_behavior'] = 'terminate'
+                        res = ec2.run_instances(**params)
+                    else:
+                        raise
+
                 instids = [i.id for i in res.instances]
                 while True:
                     try:


Test patch: