Modify the Program

Now that we have an instance of our Pulumi program deployed, let’s update it to do something a little more interesting.

Replace the entire contents of index.js index.ts __main__.py with the following:

"use strict";
const pulumi = require("@pulumi/pulumi");
const aws = require("@pulumi/aws");
const awsx = require("@pulumi/awsx");

const size = "t2.micro"; // t2.micro is available in the AWS free tier

// Look up the right AMI for running Ubuntu Trusty in our region
const ami = pulumi.output(aws.getAmi({
    filters: [
        { name: "name", values: ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] },
        { name: "virtualization-type", values: ["hvm"] },
    ],
    owners: ["099720109477"], // Canonical
    mostRecent: true,
})).apply(result => result.id);

// Create a new security group for port 80
const group = new aws.ec2.SecurityGroup("web-secgrp", {
    ingress: [
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Create a simple web server using the startup script for the instance
const userData =
`#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`;

const server = new aws.ec2.Instance("web-server-www", {
    instanceType: size,
    securityGroups: [ group.name ], // reference the group object above
    ami: ami,
    userData: userData
});

// Export the host name
exports.host = server.publicDns;
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

const size = "t2.micro"; // t2.micro is available in the AWS free tier

// Look up the right AMI for running Ubuntu Trusty in our region
const ami = pulumi.output(aws.getAmi({
    filters: [
        { name: "name", values: ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] },
        { name: "virtualization-type", values: ["hvm"] },
    ],
    owners: ["099720109477"], // Canonical
    mostRecent: true,
})).apply(result => result.id);

// Create a new security group for port 80
const group = new aws.ec2.SecurityGroup("web-secgrp", {
    ingress: [
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Create a simple web server using the startup script for the instance
const userData =
`#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`;

const server = new aws.ec2.Instance("web-server-www", {
    instanceType: size,
    securityGroups: [ group.name ], // reference the group object above
    ami: ami,
    userData: userData
});

// Export the host name
export const host = server.publicDns;
import pulumi
from pulumi_aws import ec2

size = 't2.micro'

ami = 'ami-6869aa05' # AMI for us-east-1 (Virginia)
# ami  = 'ami-c55673a0' # AMI for us-east-2 (Ohio)
# ami  = 'ami-31490d51' # AMI for us-west-1 (California)
# ami  = 'ami-7172b611' # AMI for us-west-2 (Oregon)
# ami  = 'ami-f9dd458a' # AMI for eu-west-1 (Ireland)
# ami  = 'ami-ea26ce85' # AMI for eu-central-1 (Frankfurt)

# Create a new security group for port 80
group = ec2.SecurityGroup('web-secgrp',
    ingress=[
        { 'protocol': 'tcp', 'from_port': 80, 'to_port': 80, 'cidr_blocks': ['0.0.0.0/0'] }
    ])

# Create a simple web server using the startup script for the instance
user_data = """
#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &"""

server = ec2.Instance('web-server-www',
    instance_type=size,
    security_groups=[group.name],
    ami=ami,
    user_data=user_data)

# Export the host name
pulumi.export('host', server.public_dns)

Our program now creates a simple EC2 virtual machine running a Python web server.

Update the ami variable based on your AWS region.

Next, we’ll deploy the changes.