Support for associating to existing AWS Elastic IP

Signed-off-by: Elliot Murphy <statik@users.noreply.github.com>
This commit is contained in:
Elliot Murphy 2018-12-30 14:36:11 -05:00
parent 5981bb9cad
commit 1e2c8f39ff
4 changed files with 43 additions and 2 deletions

View file

@ -1 +1 @@
ansible==2.5.2
ansible==2.6.11

View file

@ -11,6 +11,12 @@ Parameters:
Type: String
WireGuardPort:
Type: String
UseThisElasticIP:
Type: String
Default: ''
Conditions:
AllocateNewEIP: !Equals [!Ref UseThisElasticIP, '']
AssociateExistingEIP: !Not [!Equals [!Ref UseThisElasticIP, '']]
Resources:
VPC:
Type: AWS::EC2::VPC
@ -187,6 +193,7 @@ Resources:
ElasticIP:
Type: AWS::EC2::EIP
Condition: AllocateNewEIP
Properties:
Domain: vpc
InstanceId: !Ref EC2Instance
@ -194,6 +201,14 @@ Resources:
- EC2Instance
- VPCGatewayAttachment
ElasticIPAssociation:
Type: AWS::EC2::EIPAssociation
Condition: AssociateExistingEIP
Properties:
AllocationId: !Ref UseThisElasticIP
InstanceId: !Ref EC2Instance
Outputs:
ElasticIP:
Value: !Ref ElasticIP
Value: !GetAtt [EC2Instance, PublicIp]

View file

@ -12,6 +12,7 @@
PublicSSHKeyParameter: "{{ lookup('file', SSH_keys.public) }}"
ImageIdParameter: "{{ ami_image }}"
WireGuardPort: "{{ wireguard_port }}"
UseThisElasticIP: "{{ use_existing_eip }}"
tags:
Environment: Algo
register: stack

View file

@ -53,3 +53,28 @@
[{{ default_region }}]
register: _algo_region
when: region is undefined
- block:
- name: Get existing available Elastic IPs
ec2_eip_facts:
register: raw_eip_addresses
- set_fact:
available_eip_addresses: "{{ raw_eip_addresses.addresses | selectattr('association_id', 'undefined') | list }}"
- pause:
prompt: |
Do you want to use a pre-allocated Elastic IP?
(leave blank to allocate a new IP)
{% for eip in available_eip_addresses %}
{{ loop.index }}. {{ eip['public_ip'] }}
{% endfor %}
['']
register: _use_existing_eip
- set_fact:
use_existing_eip: >-
{% if _use_existing_eip.user_input is defined and _algo_region.user_input != "" %}
{{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }}
{%- else %}{% endif %}
when: use_existing_eip is undefined