1. Answers
  2. Adding Multiple Network Interfaces to an EC2 Instance

How do I add multiple network interfaces to an EC2 instance?

In this guide, we will demonstrate how to add multiple network interfaces to an AWS EC2 instance using Pulumi. Network interfaces allow you to manage multiple IP addresses for your EC2 instance, which can be useful for various networking scenarios such as hosting multiple websites or isolating network traffic.

Key Points

  • We will create an EC2 instance.
  • We will create multiple network interfaces.
  • We will attach these network interfaces to the EC2 instance.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create a VPC
const vpc = new aws.ec2.Vpc("myVpc", {
    cidrBlock: "10.0.0.0/16",
});

// Create a subnet for the VPC
const subnet = new aws.ec2.Subnet("mySubnet", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
});

// Create a security group
const securityGroup = new aws.ec2.SecurityGroup("mySecurityGroup", {
    vpcId: vpc.id,
    ingress: [
        { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, // SSH
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }, // HTTP
    ],
    egress: [
        { protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] }, // All outbound traffic
    ],
});

// Create the primary network interface
const primaryNetworkInterface = new aws.ec2.NetworkInterface("primaryNetworkInterface", {
    subnetId: subnet.id,
    privateIps: ["10.0.1.10"],
    securityGroups: [securityGroup.id],
});

// Create a secondary network interface
const secondaryNetworkInterface = new aws.ec2.NetworkInterface("secondaryNetworkInterface", {
    subnetId: subnet.id,
    privateIps: ["10.0.1.11"],
    securityGroups: [securityGroup.id],
});

// Create an EC2 instance and attach the primary network interface
const ec2Instance = new aws.ec2.Instance("myInstance", {
    instanceType: "t2.micro",
    ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
    networkInterfaces: [{
        deviceIndex: 0,
        networkInterfaceId: primaryNetworkInterface.id,
    }],
});

// Attach the secondary network interface to the EC2 instance
const secondaryNetworkInterfaceAttachment = new aws.ec2.NetworkInterfaceAttachment("secondaryNetworkInterfaceAttachment", {
    instanceId: ec2Instance.id,
    networkInterfaceId: secondaryNetworkInterface.id,
    deviceIndex: 1,
});

export const instanceId = ec2Instance.id;
export const primaryNetworkInterfaceId = primaryNetworkInterface.id;
export const secondaryNetworkInterfaceId = secondaryNetworkInterface.id;

Summary

In this guide, we created an AWS VPC, subnet, and security group. We then created two network interfaces and attached them to an EC2 instance. This setup allows the EC2 instance to have multiple IP addresses, enabling more complex networking configurations.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up