Skip to main content
In this lesson, you’ll learn how to create and manage an AWS S3 bucket using Pulumi’s AWS library with Python. The example below demonstrates the basic structure of a Pulumi program that imports necessary libraries, creates an S3 bucket, and exports its identifier.
This guide covers the process of defining, deploying, and managing an S3 bucket using Pulumi’s Infrastructure as Code approach.

Example: Creating an S3 Bucket with Pulumi

Below is the initial Python code that imports Pulumi alongside the AWS S3 library, creates an S3 bucket (named internally as “my-bucket”), and exports the bucket’s ID:
When you run this program, Pulumi deploys an S3 bucket with a name based on the internal identifier “my-bucket”. AWS will append a unique suffix to ensure the bucket name’s uniqueness.

Deploying the Pulumi Configuration

To deploy the infrastructure defined in your Python code, run the following command in your terminal:
During the deployment process, Pulumi inspects your configuration and provides a preview of the changes. The output might look similar to this:
When prompted, Pulumi will display the resources that are about to be created. For a new project, it will show that two resources are scheduled for creation, as demonstrated below:
Upon confirming by selecting “yes,” Pulumi proceeds to create the stack and deploy the specified resources. The S3 bucket is assigned a unique name (e.g., “my-bucket-5d138fe”) during creation.

Deployment Output

After deploying, your terminal will display detailed output similar to the snippet below:
You can verify the creation of the S3 bucket directly in the AWS console. Although the Pulumi configuration uses the internal name “my-bucket,” AWS assigns the bucket a unique name that includes an automatically generated suffix.

Re-running the Deployment

If you run pulumi up with the identical configuration:
and then execute:
Pulumi compares the current configuration with the deployed infrastructure. If no changes are detected, you will see output indicating that the resources are unchanged:
Pulumi enforces that the deployed infrastructure always aligns with the configuration declared in your code, updating resources only when discrepancies occur.

Summary

This lesson demonstrated the process of creating, deploying, and managing an AWS S3 bucket using Pulumi and Python. By continuously running pulumi up, you ensure that your infrastructure remains consistent with the defined configuration. This approach provides a robust example of Infrastructure as Code, helping prevent the accidental creation of duplicate resources. For further reading on managing cloud infrastructure with Pulumi, be sure to explore the Pulumi Documentation.

Watch Video