1. Attaching Multiple Network Interfaces to EC2 Instances

    TypeScript

    When managing AWS EC2 instances with Pulumi, you often need to attach multiple network interfaces. Each AWS EC2 instance comes with a primary network interface (the eth0 interface), but sometimes, you may require additional interfaces, either to separate traffic (e.g., public vs. private) or to provide additional networking features such as increased network throughput.

    To attach multiple network interfaces to an EC2 instance using Pulumi, you'll want to follow these steps:

    1. Define the EC2 instance you want to work with.
    2. Define additional network interfaces (aws.ec2.NetworkInterface).
    3. Attach these additional network interfaces to the EC2 instance (aws.ec2.NetworkInterfaceAttachment).

    Here is a program written in TypeScript that shows how you can accomplish this. Make sure to replace any placeholder strings with actual values like the VPC, subnet id, and Image id that match your AWS environment setup.

    This program uses aws.ec2.Instance to create a new EC2 instance, aws.ec2.NetworkInterface for creating additional network interfaces, and aws.ec2.NetworkInterfaceAttachment for attaching them to the EC2 instance. We also set the deviceIndex to 1 for the first additional interface, and if there were more, they would be numbered incrementally (2, 3, etc.).

    import * as pulumi from '@pulumi/pulumi'; import * as aws from '@pulumi/aws'; const ami = 'ami-0c55b159cbfafe1f0'; // replace with the AMI ID of your choice const instanceType = 't2.micro'; // use the instance type suitable for your project // Create a new EC2 instance const server = new aws.ec2.Instance('my-instance', { ami: ami, instanceType: instanceType, subnetId: 'subnet-xxxxxxx', // replace with your Subnet ID // ... add any other mandatory properties like keyName, securityGroupIds, etc. }); // Create additional network interfaces const secondaryNetworkInterface = new aws.ec2.NetworkInterface('secondary-nic', { subnetId: 'subnet-ccccccc', // replace with the Subnet ID for the secondary interface tags: { Name: 'secondary-nic', }, // ... add any other properties like security groups, or specific private IPs as needed }); // Attach the secondary network interface to the EC2 instance const attachment = new aws.ec2.NetworkInterfaceAttachment('nic-attachment', { instanceId: server.id, networkInterfaceId: secondaryNetworkInterface.id, deviceIndex: 1, // device index 1 is for the first additional network interface }, { dependsOn: [server] }); // ensure this attachment is created after the EC2 instance // Export the IDs of the resources export const instanceId = server.id; export const secondaryNicId = secondaryNetworkInterface.id; export const attachmentId = attachment.id;

    This program will result in an EC2 instance being created with an additional network interface attached to it. Please note that proper AWS permissions are required to create and attach network interfaces. The dependsOn option in the attachment resource is used to signal Pulumi about resource dependencies which in this case, ensures that the attachment is only attempted after the instance has been created.

    Remember:

    • You may need to handle the permissions (attachment policy) for the Network Interface.
    • The deviceIndex starts at 1 for additional network interfaces since 0 is used by the default interface.
    • If you need to ensure the interfaces are created before the instance starts, use dependsOn to declare dependency relations.

    Finally, after running this Pulumi program, you can interact with your AWS resources, and your instance will have an extra network interface attached to it, as specified.