Repository: ansible-collections/amazon.aws

Issue:
ec2_vpc_nat_gateway connectivity_type:private still allocates uneeded elastic IP
### Summary

When setting connectivity_type: private no elastic IP should be provisioned since it is not used by the private nat gateway,

### Issue Type

Bug Report

### Component Name

ec2_vpc_nat_gateway

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.11.11] 

```


### Collection Versions

```console (paste below)
$ ansible-galaxy collection list
amazon.aws           5.5.0  
community.aws        5.5.0  

```


### AWS SDK versions

```console (paste below)
$ pip show boto boto3 botocore

WARNING: Package(s) not found: boto
Name: boto3
Version: 1.26.123
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: 
License: Apache License 2.0
Location: /tmp/venv/lib/python3.10/site-packages
Requires: botocore, jmespath, s3transfer
Required-by: onelogin-aws-assume-role
---
Name: botocore
Version: 1.29.130
Summary: Low-level, data-driven core of boto 3.
Home-page: https://github.com/boto/botocore
Author: Amazon Web Services
Author-email: 
License: Apache License 2.0
Location: /tmp/venv/lib/python3.10/site-packages
Requires: jmespath, python-dateutil, urllib3
Required-by: awscli, boto3, s3transfer

```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed

```


### OS / Environment

_No response_

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
    - name: Create private nat gateway for customer gateway
      amazon.aws.ec2_vpc_nat_gateway:
        state: present
        subnet_id:  '{{ private_subnetid_here }}'
        wait: true
        region: '{{ region_here }}'
        if_exist_do_not_create: true
        connectivity_type: private
        tags:
          Name: 'ngw-private'


```


### Expected Results

No elastic IP should be allocated

### Actual Results

```console (paste below)
One new elastic IP ( unused ) is allocated
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
!component +plugins/modules/ec2_vpc_nat_gateway.py

Resolving patch:
diff --git a/plugins/modules/ec2_vpc_nat_gateway.py b/plugins/modules/ec2_vpc_nat_gateway.py
--- a/plugins/modules/ec2_vpc_nat_gateway.py
+++ b/plugins/modules/ec2_vpc_nat_gateway.py
@@ -746,12 +746,11 @@ def pre_create(client, module, subnet_id, tags, purge_tags, allocation_id=None,
             )
             return changed, msg, results
         else:
-            changed, msg, allocation_id = (
-                allocate_eip_address(client, module)
-            )
+            if connectivity_type == "public":
+                changed, msg, allocation_id = allocate_eip_address(client, module)
 
-            if not changed:
-                return changed, msg, dict()
+                if not changed:
+                    return changed, msg, dict()
 
     elif eip_address or allocation_id:
         if eip_address and not allocation_id:


Test patch:
diff --git a/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml b/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml
--- a/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml
+++ b/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml
@@ -919,6 +919,7 @@
       - create_ngw.changed
       - create_ngw.connectivity_type == 'private'
       - '"create_time" in create_ngw'
+      - '"allocation_id" not in create_ngw.nat_gateway_addresses[0]'
 
   - name: 'set facts: NAT gateway ID'
     set_fact: