Build: Run AWS Commands in a Python Script Using LocalStack
Build a Python Menu for S3 File Actions
If you’ve been playing with LocalStack from the command line, it’s time to level up. Let’s write a Python script that uses Boto3 to talk to your local AWS emulation — all from a friendly little menu. We'll show you how to create this script, where to place it, and how to run it inside your virtual environment.
The script you’ll build lets you upload and download files from a simulated S3 bucket — just like the real thing. You’ll also be able to list files locally and inside your mock S3 environment. This gives you a fully self-contained AWS experience, all running on your local machine.
Here’s what the program looks like once launched:
In this tutorial, you'll install Docker, set up a Python virtual
environment, install LocalStack and its CLI tools, create an S3 bucket, and
finally run the Python script to move files between your local filesystem and
the bucket. Let’s dive in.
If you’ve been playing with LocalStack from the command line, it’s time to level up. Let’s write a Python script that uses Boto3 to talk to your local AWS emulation — all from a friendly little menu. We'll show you how to create this script, where to place it, and how to run it inside your virtual environment.
The script you’ll build lets you upload and download files from a simulated S3 bucket — just like the real thing. You’ll also be able to list files locally and inside your mock S3 environment. This gives you a fully self-contained AWS experience, all running on your local machine.
Here’s what the program looks like once launched:
Step 1: Install Docker (System-Wide)
LocalStack runs inside Docker, so your first move is to install Docker outside any Python environment.
Update and upgrade your system packages:
Install Docker:
Add your user to the Docker group so you can run Docker without
sudo:
Reboot or restart your Linux session to activate the new group.
LocalStack runs inside Docker, so your first move is to install Docker outside any Python environment.
Update and upgrade your system packages:
Step 2: Set Up a Python Virtual Environment
This keeps your LocalStack CLI isolated and avoids the externally-managed-environment pip error.
Install the required Python tools:
Create a virtual environment for LocalStack:
Activate the virtual environment:
Install LocalStack CLI and supporting tools inside the venv:
Confirm the installation:
Check that awslocal installed correctly:
Step 3: Launch LocalStack in One Terminal
Open your first terminal.
Activate the venv:
Start LocalStack in foreground mode to see live logs:
The terminal will stay open and stream logs. Wait for the Ready.
message.
This keeps your LocalStack CLI isolated and avoids the externally-managed-environment pip error.
Install the required Python tools:
Open your first terminal.
Activate the venv:
Step 4: Run CLI Commands in a Second Terminal
Open a second terminal window.
Activate the venv again:
Now create your first local S3 bucket:
If you see make_bucket: my-test-bucket, you’re golden.
Look back at Terminal 1—you'll see log output like:
That's proof your local AWS environment is fully operational.
Open a second terminal window.
Activate the venv again:
Look back at Terminal 1—you'll see log output like:
Step 5: Write the Python Script in a Third Terminal
Open a third terminal window.
Activate your venv once more:
Use nano or vi to create the script:
Paste the following Python code:
Step 6: Run the Python Script
Recap: What Are the Three Terminals Doing?
Terminal 2 (CLI):
Open a third terminal window.
Activate your venv once more:
Step 6: Run the Python Script
Now it's type to run the Python script and use it:
Sample Output from a Real Session
Recap: What Are the Three Terminals Doing?
- Terminal 1 - Run LocalStack and stream logs
- Terminal 2 - Use awslocal CLI (e.g., mb)
- Terminal 3 - Write and run the Python script
Graceful Shutdown and Cleanup
Terminal 3 (Python):
Terminal 3 (Python):
- Choose "exit" to quite the script if it is still running deactivate
- Exit (deactivate) the Python virtual environment:
Terminal 2 (CLI):
Remove the S3 bucket and contents:
Exit LocalStack:
Terminal 1 (LocalStack):
Type Ctrl C to stop LocalStack logs and
services
Quit (deactivate) the Python virtual environment:
Conclusion
Next time, we can expand this with folders,
object metadata, or even logging. But for now? You just took command of a
local AWS environment with pure Python. 🚀📃
Need AWS Expertise?
We'd love to help you with your AWS projects. Feel free to reach out to us at info@pacificw.com.
Written by Aaron Rose, software engineer and technology writer at Tech-Reader.blog.
Comments
Post a Comment