An easy-to-use CLI app for effortlessly managing your local Pub/Sub environment
and automatically syncing it with your Google Cloud Pub/Sub configuration.
The inspiration for pubsub-emulator
stemmed from the challenges faced when integrating Pub/Sub into various projects. A key obstacle was the lack of an effective method to replicate the Pub/Sub setup locally. Although Google Cloud's beta Pub/Sub emulator was a step in the right direction, it started as an empty slate each time, missing the crucial topics and subscriptions needed for communication. To address this gap, this project was developed with three core functionalities:
- Import topics and subscriptions from a GCP project to a local Pub/Sub environment.
- Automatically translate production push endpoints into local URLs.
- Provide a simple CRUD interface for managing topics and subscriptions locally.
- Python
≥3.11
1 - Poetry
- Google Cloud Pub/Sub emulator
Start by creating a new JSON file in your project directory.
You can name it service-mappings.json
(or any name you prefer).
In this file, you'll define a series of key-value pairs where each key is the URL of a cloud service, and the corresponding value is the localhost port to which it should be mapped. For example, if your service URL is https://some-service-k4caugryxh-cha.run.app
and the corresponding local URL is localhost:8080
, then your service-mappings.json
should look like this:
{
"some-service-k4caugryxh-cha.run.app": 8080
}
Set an environment variable that points to the location of your JSON configuration file.
echo 'SERVICE_MAPPINGS_PATH=service-mappings.json' >> .env
Ensure that all necessary dependencies are installed.
poetry install
Set the PROJECT_ID
environment variable to your Google Cloud project ID.
echo 'PROJECT_ID=my-project' >> .env
Start the Pub/Sub emulator by executing this command in a new terminal tab or window. 2
gcloud beta emulators pubsub start
Set up the required environment variables for the Pub/Sub emulator connection to work. 3
$(gcloud beta emulators pubsub env-init)
Synchronize your cloud configuration with the local emulator to ensure that your local environment mirrors the cloud setup with the appropriate local URLs.
make sync
Execute commands in pubsub-emulator
through Poetry using the following syntax:
poetry run python pubsub_emulator <COMMAND>
Replace <COMMAND>
with the specific command you wish to execute.
This method ensures that your commands run within the project's virtual environment.
To view a list of all available commands and their options in pubsub-emulator
, you can use the --help
option.
poetry run python pubsub_emulator --help
poetry run python pubsub_emulator <COMMAND> --help
When creating or updating a subscription with pubsub-emulator
, it's necessary to pass a request object in JSON string format. To ensure you're using the correct structure for this request, refer to the latest subscription structure definitions in Google's official Pub/Sub Library.
This project is licensed under the CC BY-NC 4.0 DEED (Attribution-NonCommercial 4.0 International), which essentially means you are free to share and adapt the material in this repository, provided you give appropriate credit, indicate if changes were made, and, most importantly, do not use it for commercial purposes.
Feel free to reach out through one of the following channels:
Footnotes
-
Since this command can't be executed in the background ↩
-
For more details about this command visit the official Pub/Sub emulator guide ↩