Systemd - Assign AWS reserved IP
For Ubuntu 16.04 and higher
Systemd Configuration
[Unit]
Description=<Name of Service>
After=network.target
After=systemd-user-sessions.service
After=network-online.target
[Service]
User=ubuntu
ExecStart=path_to_bash_script.sh
[Install]
WantedBy=multi-user.target
Then the script can be a standard bash shell script like so
#!/bin/bash
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