Managing cloud infrastructure through code — known as Infrastructure as Code (IaC) — is one of the key practices in modern DevOps. It ensures consistency, scalability, and repeatability when deploying resources across environments.
In this quick guide, we’ll see how to use Python scripts with Bitbucket Pipelines to automatically create an AWS S3 bucket using the boto3 library.
Step 1: Setup Your Python Script
We’ll start by creating a Python script that provisions an S3 bucket using AWS’s SDK — boto3.
# create_s3_bucket.py
import boto3
import os
def create_bucket(bucket_name, region=None):
try:
if region is None:
s3_client = boto3.client('s3')
s3_client.create_bucket(Bucket=bucket_name)
else:
s3_client = boto3.client('s3', region_name=region)
location = {'LocationConstraint': region}
s3_client.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration=location
)
print(f'✅ Bucket {bucket_name} created successfully.')
except Exception as e:
print(f'❌ Error: {e}')
if __name__ == "__main__":
bucket_name = os.getenv('BUCKET_NAME', 'my-default-bucket')
region = os.getenv('AWS_REGION', 'us-west-2')
create_bucket(bucket_name, region)
This script uses environment variables to fetch the bucket name and AWS region. You can set these securely within Bitbucket.
Step 2: Configure Bitbucket Pipelines
Next, define your pipeline in a file called bitbucket-pipelines.yml:
image: python:3.8
pipelines:
custom:
manual-deploy:
- step:
name: Install Dependencies and Run Script
caches:
- pip
script:
- python -m pip install --upgrade pip
- pip install boto3
- pip list # Verify boto3 installation
- echo "$(which python)"
- python create_s3_bucket.py
definitions:
caches:
pip: ~/.cache/pip
This pipeline uses a lightweight Python image, installs dependencies, and runs your script to create the S3 bucket.
Step 3: Add Environment Variables
In Bitbucket, go to:
Repository Settings → Pipelines → Repository Variables
Add the following secure variables:
AWS_ACCESS_KEY_ID Your AWS access key
AWS_SECRET_ACCESS_KEY Your AWS secret key
AWS_REGION Target AWS region (e.g., us-west-2)
BUCKET_NAME Name of the S3 bucket to create
Bitbucket will automatically inject these variables into your pipeline runtime environment — keeping your credentials safe.
Step 4: Trigger the Pipeline
Once everything is set up:
Commit and push your changes to the repository.
Go to Bitbucket Pipelines.
Trigger the manual-deploy pipeline.
You should see logs confirming the creation of your S3 bucket.
With this straightforward setup, you’ve created a Python-driven Infrastructure as Code workflow within Bitbucket Pipelines. It’s an excellent solution for lightweight automation tasks and serves as a solid foundation before moving on to tools like Terraform or AWS CDK.