The network-interface-manager, manages the assignment of a pool of network interfaces to instances. When the instance is stopped or terminated, the interface is removed. When a new instance is started, an interface from the pool is assigned to it. The goal is to be keep a fixed internal ip address active, while replacing the instance.
The manager will listen to all EC2 instance state change notifications. When an instance with the tag network-interface-manager-pool
reaches the state running, it will assign a free network interface with the same tag and tag value in it's subnet.
You can start using the network interface manager, in three simple steps:
- deploy the network-interface-manager
- create a pool of tagged network interfaces
- create an auto scaling group of tagged instances
To deploy the provider, type:
git clone https://github.com/binxio/ec2-network-interface-manager.git
cd ec2-network-interface-manager
aws cloudformation create-stack \
--capabilities CAPABILITY_IAM \
--stack-name network-interface-manager \
--template-body file://./cloudformation/network-interface-manager.yaml
aws cloudformation wait stack-create-complete --stack-name network-interface-manager
Create a pool of network interfaces, and tag them with an network-interface-manager-pool
value:
InterfaceAZ1:
Type: AWS::EC2::NetworkInterface
Properties:
Tags:
- Key: network-interface-manager-pool
Value: bastion
InterfaceAZ2:
Type: AWS::EC2::NetworkInterface
Properties:
Tags:
- Key: network-interface-manager-pool
Value: bastion
Create an auto scaling group and apply the tag network-interface-manager-pool
to all the instances:
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
...
Tags:
- Key: network-interface-manager-pool
Value: bastion
PropagateAtLaunch: true
The manager will automatically associate network interfaces to instance tagged with network-interface-manager-pool
. It does
this by subscribing to EC2 state change events. It will not do anything on instances without the
tag network-interface-manager-pool
. The network interface manager also syncs the state every 5 minutes, to ensure that we are eventually
consistent in the face of errors.
That is all. If you want to see it all in action, deploy the demo.
In order to deploy the demo, type:
aws cloudformation create-stack \
--capabilities CAPABILITY_NAMED_IAM \
--stack-name network-interface-manager-demo \
--template-body file://./cloudformation/demo-stack.yaml
aws cloudformation wait stack-create-complete --stack-name network-interface-manager-demo
As network interfaces are bound to an availability zone, the auto scaling group should also be tied to a single availability zone. Otherwise. instances can be rescheduled in another AZ leaving the network interfaces from the pool unattached.
There are two alternative solutions to achieve the same functionality:
- use a network load balancer
- associate an network interfae on instance startup. In my use case, I did not want to spent money on keeping an NLB running nor give the instance all the permissions to attach network interfaces.