1. Answers
  2. Setting up an AWS EC2 Instance with Pulumi

How do I set up an AWS EC2 instance with Pulumi?

In this guide, we will set up an AWS EC2 instance using Pulumi. We will create an EC2 instance, specify its properties, and configure its security group to allow SSH access. This example assumes you have already configured your Pulumi CLI and AWS account.

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

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

// Create a new 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",
    },
});

// Export the public IP of the instance
export const publicIp = server.publicIp;
export const publicHostName = server.publicDns;

Key Points

  • Security Group: We created a security group to allow SSH access from anywhere.
  • EC2 Instance: We provisioned an EC2 instance using a specified AMI and instance type.
  • Exports: The public IP and DNS of the instance are exported for easy access.

Summary

In this guide, we set up an AWS EC2 instance using Pulumi. We created a security group to allow SSH access, provisioned an EC2 instance, and exported its public IP and DNS. This setup is useful for deploying a server that can be accessed remotely via SSH.

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