Repository: aws/aws-cli

Issue:
--private-ip-address option at run-instances does not work 
I think --private-ip-address option can accept private ip address to assign.
but when I ran the command as follows,  

> aws ec2 run-instances \
>   --image-id ami-b1fe9bb0 \
>   --subnet-id subnet-00xxxxxx \
>   --key-name keypair1 \
>   --security-group-ids sg-yyyyyy \
>   --region ap-northeast-1 \
>   --instance-type t1.micro \
>   --associate-public-ip-address \
>   --private-ip-address 10.0.0.222 \
>   --debug

Error message was show as follows.

> ClientError: A client error (InvalidParameterCombination) occurred: Network interfaces and an instance-level private IP address may not be specified on the same request

On the other hand, when executed same command by ec2-api-tools, it worked fine.

Bellow is the differences in API request between awscli and ec2-api-tools

ec2-api-tools

> NetworkInterface.1.PrivateIpAddresses.1.Primary=true&
> NetworkInterface.1.PrivateIpAddresses.1.PrivateIpAddress=10.0.0.222&

awscli

> PrivateIpAddress=10.0.0.222&

This seems to be same as #504.



Discussion:
It does work as long as you do not specify it in combination with the `--associate-public-ip-address`.  If both are specified, we end up with a similar conflict as that discovered in #504.

I'm working on a fix now.  Thanks for reporting.


Resolving patch:
diff --git a/awscli/customizations/ec2runinstances.py b/awscli/customizations/ec2runinstances.py
--- a/awscli/customizations/ec2runinstances.py
+++ b/awscli/customizations/ec2runinstances.py
@@ -100,6 +100,11 @@ def _fix_args(operation, endpoint, params, **kwargs):
             if 'security_group_ids' in params:
                 ni[0]['Groups'] = params['security_group_ids']
                 del params['security_group_ids']
+            if 'private_ip_address' in params:
+                ip_addr = {'PrivateIpAddress': params['private_ip_address'],
+                           'Primary': True}
+                ni[0]['PrivateIpAddresses'] = [ip_addr]
+                del params['private_ip_address']
 
 
 EVENTS = [


Test patch:
diff --git a/tests/unit/ec2/test_run_instances.py b/tests/unit/ec2/test_run_instances.py
--- a/tests/unit/ec2/test_run_instances.py
+++ b/tests/unit/ec2/test_run_instances.py
@@ -191,3 +191,32 @@ def test_group_id_alone(self):
         }
         self.assert_params_for_cmd(args_list, result)
 
+    def test_associate_public_ip_address_and_private_ip_address(self):
+        args = ' --image-id ami-foobar --count 1 '
+        args += '--private-ip-address 10.0.0.200 '
+        args += '--associate-public-ip-address --subnet-id subnet-12345678'
+        args_list = (self.prefix + args).split()
+        result = {
+            'NetworkInterface.1.DeviceIndex': '0',
+            'NetworkInterface.1.AssociatePublicIpAddress': 'true',
+            'NetworkInterface.1.SubnetId': 'subnet-12345678',
+            'NetworkInterface.1.PrivateIpAddresses.1.PrivateIpAddress': '10.0.0.200',
+            'NetworkInterface.1.PrivateIpAddresses.1.Primary': 'true',
+            'ImageId': 'ami-foobar',
+            'MaxCount': '1',
+            'MinCount': '1'
+        }
+        self.assert_params_for_cmd(args_list, result)
+
+    def test_private_ip_address_alone(self):
+        args = ' --image-id ami-foobar --count 1 '
+        args += '--private-ip-address 10.0.0.200'
+        args_list = (self.prefix + args).split()
+        result = {
+            'PrivateIpAddress': '10.0.0.200',
+            'ImageId': 'ami-foobar',
+            'MaxCount': '1',
+            'MinCount': '1'
+        }
+        self.assert_params_for_cmd(args_list, result)
+