How to create AWS EC2 instances with Pulumi

This reference shows how to use Pulumi to define an AWS EC2 resource using pure code which can then be deployed to AWS and managed as infrastructure as code.

What is AWS EC2?


AWS EC2 is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Find out more at AWS here.

Create an AWS EC2 resource using @pulumi/aws

The @pulumi/aws library enables fine-grained control over the AWS EC2 resource meaning it can be coded, deployed, and managed entirely in code.

const aws = require("@pulumi/aws");

const eip = new aws.ec2.Eip("myeip");

const securityGroup = new aws.ec2.SecurityGroup("mysecuritygroup", {
    ingress: [
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: [""] },

const vpc = new aws.ec2.Vpc("myvpc", {
    cidrBlock: ""

const internetGateway = new aws.ec2.InternetGateway("myinternetgateway", {

const publicRouteTable = new aws.ec2.RouteTable("myroutetable", {
    routes: [
            cidrBlock: "",