Skip to main content

AWS web server with manual provisioning (in Python)

An example of manually configuring an AWS EC2 virtual machine

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-ec2-provisioners
cd pulumi-examples/aws-py-ec2-provisioners

This demonstrates using the pulumi_command package to accomplish post-provisioning configuration steps.

Using these building blocks, one can accomplish much of the same as Terraform provisioners.

Prerequisites#

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

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Generate an OpenSSH keypair for use with your server, as per the AWS requirements:

    Terminal window
    ssh-keygen -t rsa -f rsa -b 4096 -m PEM

    This will output two files, rsa and rsa.pub, in the current directory. Be sure not to commit these files!

  3. Configure the stack so that the public key is used by your EC2 instance, and the private key is used for subsequent SCP and SSH steps that will configure your server after it is stood up:

    Terminal window
    cat rsa.pub | pulumi config set publicKey --
    cat rsa | pulumi config set privateKey --secret --

    Notice that we’ve used --secret for privateKey. This ensures the private key is stored as an encrypted Pulumi secret.

  4. Set your desired AWS region:

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

    Terminal window
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  6. Deploy the stack. All resources will be provisioned and configured:

    Terminal window
    pulumi up

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.