Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS support for existing EIP (revised) #1292

Merged
merged 5 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ cloud_providers:
size: s-1vcpu-1gb
image: "ubuntu-18-04-x64"
ec2:
# Change the encrypted flag to "true" to enable AWS volume encryption, for encryption of data at rest.
# Warning: the Algo script will take approximately 6 minutes longer to complete.
# Change the encrypted flag to "true" to enable AWS volume encryption, for encryption of data at rest.
# Warning: the Algo script will take approximately 6 minutes longer to complete.
encrypted: false
# Set use_existing_eip to "true" if you want to use a pre-allocated Elastic IP
# Additional prompt will be raised to determine which IP to use
use_existing_eip: true
size: t2.micro
image:
name: "ubuntu-bionic-18.04"
Expand Down
1 change: 1 addition & 0 deletions roles/cloud-ec2/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ec2_vpc_nets:
cidr_block: 172.16.0.0/16
subnet_cidr: 172.16.254.0/23
ec2_venv: "{{ playbook_dir }}/configs/.venvs/aws"
existing_eip: ""
17 changes: 16 additions & 1 deletion roles/cloud-ec2/files/stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -175,13 +181,22 @@ Resources:

ElasticIP:
Type: AWS::EC2::EIP
Condition: AllocateNewEIP
Properties:
Domain: vpc
InstanceId: !Ref EC2Instance
DependsOn:
- 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]
1 change: 1 addition & 0 deletions roles/cloud-ec2/tasks/cloudformation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
PublicSSHKeyParameter: "{{ lookup('file', SSH_keys.public) }}"
ImageIdParameter: "{{ ami_image }}"
WireGuardPort: "{{ wireguard_port }}"
UseThisElasticIP: "{{ existing_eip }}"
tags:
Environment: Algo
register: stack
22 changes: 22 additions & 0 deletions roles/cloud-ec2/tasks/prompts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,25 @@
[{{ 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: >-
What Elastic IP would you like to use?
{% for eip in available_eip_addresses %}
{{ loop.index }}. {{ eip['public_ip'] }}
{% endfor %}

Enter the number of your desired Elastic IP
register: _use_existing_eip

- set_fact:
existing_eip: "{{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }}"
when: cloud_providers.ec2.use_existing_eip