Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Web server using Amazon EC2

Basic example of an AWS web server accessible over HTTP (in Python!)

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set aws-py-webserver
cd pulumi-examples/aws-py-webserver

An example based on the Amazon sample at http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/deploying.applications.html. The example deploys an EC2 instance and opens port 80.

Prerequisites#

  1. Install Pulumi
  2. Configure AWS credentials
  3. Install Python

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init python-webserver-testing
  2. Set the AWS region to deploy into:

    Terminal window
    pulumi config set aws:region us-west-2
  3. Install dependencies:

    Terminal window
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  4. Run pulumi up to preview and deploy changes:

    Terminal window
    pulumi up
    Previewing stack 'python-webserver-testing'
    Previewing changes:
    ...
    Do you want to proceed? yes
    Updating stack 'python-webserver-testing'
    Performing changes:
    #: Resource Type Name Status Extra Info
    1: pulumi:pulumi:Stack webserver-py-python-webserver-testing + created
    2: aws:ec2:SecurityGroup web-secgrp + created
    3: aws:ec2:Instance web-server-www + created
    info: 3 changes performed:
    + 3 resources created
    Update duration: 26.470339302s
  5. View the host name and IP address of the instance via stack output:

    Terminal window
    pulumi stack output
    Current stack outputs (2):
    OUTPUT VALUE
    public_dns ec2-34-217-176-141.us-west-2.compute.amazonaws.com
    public_ip 34.217.176.141
  6. Verify that the EC2 instance exists, by either using the AWS Console or running aws ec2 describe-instances.

Cleaning up#

Once you’re finished experimenting, destroy your stack and remove it to avoid incurring any additional cost:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.