Systemd - Assign AWS reserved IP

For Ubuntu 16.04 and higher

Systemd Configuration

Description=<Name of Service>



Then the script can be a standard bash shell script like so


Commands that run here as the ubuntu user

Conincidentally AWS command to get the current network interface

current=$(aws ec2 describe-network-interfaces --network-interface-id <reserved_network_id> --region <region> | grep -Po '"AttachmentId": "\K.*?(?=")')

And to attach the network interface to the EC2 instance

aws ec2 attach-network-interface --network-interface-id <reserved_network_id> --instance-id $(ec2metadata --instance-id) --device-index <network_interface_id> --region <region>

network_interface_id should be 2 or higher so it won't collide with the automatic assigned one