skip to Main Content

My machine image requires 2 network interfaces. I want to avoid having to manually do that after the instance is brought up. Is there a way to add the 2nd network interface during Launch? either in the AWS GUI or scripted way, I am open to both.

I know how to add the second interface to an instance after it has been created. I would like to minimize the amount of work I have to do after the fact. I want to streamline this part of the process to make it easier for the users to consume this image.

2

Answers


  1. Yes you can add ENIs to an EC2 during configuration via the Console. To do this, start launching an instance and go to Network Settings > Advanced Network Configuration > Add Network Interface.

    Just know the number of ENIs you can add is dependent on your instance type. This info can be found here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html#AvailableIpPerENI

    If you want this automated, use Infrastructure as Code or the create a script that uses the AWS CLI

    Login or Signup to reply.
  2. If you don’t want to manually add another network interface, you can try add this script at the user data of EC2 instances

    #!/bin/bash
    
    # Install AWS CLI (if not already installed, this is install command for amazon linux)
    yum install -y aws-cli
    
    # Get the instance ID of the current instance
    INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    
    # Create a new network interface
    NEW_INTERFACE_ID=$(aws ec2 create-network-interface --subnet-id subnet-xxxxxxxx --groups sg-xxxxxxxx --query 'NetworkInterface.NetworkInterfaceId' --output text)
    
    # Attach the new network interface to the instance
    aws ec2 attach-network-interface --network-interface-id $NEW_INTERFACE_ID --instance-id $INSTANCE_ID --device-index 1
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search