1. Answers
  2. Building an AWS EC2 Volume Attachment

How Do I Build an AWS EC2 Volume Attachment?

In this guide, we will create an AWS EC2 instance and an EBS volume, then attach the EBS volume to the EC2 instance using Pulumi. This setup is useful for scenarios where you need additional storage for your applications running on EC2 instances.

Key Points

  • We will create an EC2 instance.
  • We will create an EBS volume.
  • We will attach the EBS volume to the EC2 instance.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create a new security group for the EC2 instance
const securityGroup = new aws.ec2.SecurityGroup("web-secgrp", {
    description: "Enable HTTP access",
    ingress: [{
        protocol: "tcp",
        fromPort: 80,
        toPort: 80,
        cidrBlocks: ["0.0.0.0/0"],
    }],
});

// Create an EC2 instance
const server = new aws.ec2.Instance("web-server", {
    instanceType: "t2.micro",
    ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
    securityGroups: [securityGroup.name],
    tags: {
        Name: "web-server",
    },
});

// Create an EBS volume
const volume = new aws.ebs.Volume("web-server-volume", {
    availabilityZone: server.availabilityZone,
    size: 10, // Size in GB
    tags: {
        Name: "web-server-volume",
    },
});

// Attach the EBS volume to the EC2 instance
const volumeAttachment = new aws.ec2.VolumeAttachment("web-server-volume-attachment", {
    deviceName: "/dev/sdh",
    instanceId: server.id,
    volumeId: volume.id,
});

// Export the instance ID and volume ID
export const instanceId = server.id;
export const volumeId = volume.id;
export const volumeAttachmentId = volumeAttachment.id;

Summary

In this guide, we created an EC2 instance and an EBS volume, then attached the EBS volume to the EC2 instance using Pulumi. This setup provides additional storage for applications running on the EC2 instance.

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