1. Answers
  2. How do I build an AWS ec2 eip with Pulumi?

How do I build an AWS ec2 eip with Pulumi?

To build an Elastic IP (EIP) for an EC2 instance on AWS, you’ll need to follow these steps.

  1. Provider Configuration: Initialize the AWS provider to specify that we will be working with AWS resources.
  2. EC2 Instance Creation: Create an EC2 instance that will be associated with the new EIP.
  3. Elastic IP Allocation: Allocate a new EIP to be used by the EC2 instance.
  4. Elastic IP Association: Associate the allocated Elastic IP with the created EC2 instance.

These steps will ensure that the EC2 instance has a public IP address that remains constant even if the instance is stopped and started again.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create a security group to allow SSH traffic
const example = new aws.ec2.SecurityGroup("example", {
    name: "example",
    description: "Allow SSH inbound traffic",
    ingress: [{
        fromPort: 22,
        toPort: 22,
        protocol: "tcp",
        cidrBlocks: ["0.0.0.0/0"],
    }],
    egress: [{
        fromPort: 0,
        toPort: 0,
        protocol: "-1",
        cidrBlocks: ["0.0.0.0/0"],
    }],
});
// Create an AWS EC2 instance
const exampleInstance = new aws.ec2.Instance("example", {
    ami: "ami-0c55b159cbfafe1f0",
    instanceType: aws.ec2.InstanceType.T2_Micro,
    keyName: "your-key-name",
    tags: {
        Name: "example-instance",
    },
    vpcSecurityGroupIds: [example.id],
});
// Allocate a new Elastic IP
const exampleEip = new aws.ec2.Eip("example", {vpc: true});
// Associate the Elastic IP with the EC2 instance
const exampleEipAssociation = new aws.ec2.EipAssociation("example", {
    instanceId: exampleInstance.id,
    allocationId: exampleEip.id,
});
export const instanceId = exampleInstance.id;
export const eip = exampleEip.publicIp;

Summary

In this example, an AWS provider was configured for the us-west-2 region. A security group allowing SSH access on port 22 was created and attached to an EC2 instance. An Elastic IP was allocated in the VPC and associated with the EC2 instance. Finally, the instance ID and EIP were output to identify the resources.

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