How to set static IP Address for AWS EC2 instance


I dev’ing on euwe-2.20171109001029_ and trying to assign static ip addresses to the ec2 instances. I am using

“prov.set_nic_settings idx, {ip_addr: ipAddress, subnet_mask: subnetMask, gateway: gateway, addr_mode: [‘static’, ‘Static’]}”

and can see the ip address value in the evm.log.

MIQ(ManageIQ::Providers::Amazon::CloudManager::Provision#log_clone_options) Prov Options: :ip_addr = “”

but once the the ec2 instance is up in aws console the ip address has not been set.

Setting a static IP address is generally a VM guest operation, so you’ll need some kind of finishing/customisation script to perform this such as a cloud-init script. I’ve been using this cloud-init snippet to set fixed IP addresses in new RHEL 7 VMs provisioned into RHV:

   hostname = evm[:hostname]
   ip_addr = evm[:ip_addr]
   subnet_mask = evm[:subnet_mask]
   gateway = evm[:gateway]
   dns_servers = evm[:dns_servers]
   dns_suffixes = evm[:dns_suffixes]
  ## Setup motd
  - echo Welcome to VM <%= hostname %>, provisioned by Red Hat CloudForms on $(date) > /etc/motd
  - rm -f /root/*
  - nmcli con down 'System eth0'
  - nmcli con mod 'System eth0'
    ipv4.method "manual"
    ipv4.addresses "<%= ip_addr %>/<%= subnet_mask %>"
    ipv4.gateway "<%= gateway %>"
    ipv4.dns "<%= dns_servers %>"
    ipv4.dns-search "<%= dns_suffixes %>"
  - nmcli con up 'System eth0'
  - hostnamectl set-hostname <%= hostname %>
  - systemctl mask cloud-init-local cloud-init cloud-config cloud-final


Thanks @pemcg

Using cloud-init looks viable. I have been experimenting with the public AMi, RHEL-7.3_HVM_GA-20161026-x86_64-1-Hourly2-GP2 The CF cloud-init is executing but the “nmcli con mod” command leaves the instance inaccessible (aws health status is 1/2). Also ran the “nmcli con mod” manually once ssh’ed to the instance (without con down). Rebooting the instance also leaves it inaccessible, again aws health status is 1/2. Finding a way here could end up with having to customize the ami for aws. Ideally we dont want the base rhel os image to drift between vmware and aws. For vmware we’re not using guest customization, just need to set the ip_addr option, any chance of “passing/using” that as part of the aws provider api call too?

It may be that your connection is called something different from this. Try:

nmcli con show

…to see what the connection names are.


Did check that and in this case its ‘System eth0’ too. Before we work on finding a way via cloud-init, I am hoping it can rather be done via the MIQ api call when creating the instance. I am not expert enough to say really, but this does make sense and looking at various google’ed :wink: resources it seems possible. Using the private_ip_address option in network_interfaces with the create_instances call?

The cloud-init way introduces a lot of extra engineering at potentially various levels, which all have ongoing maintenance considerations. The alternative is inject it during the create call… better?