Skip to content

Files

Latest commit

 

History

History
 
 

eventarc

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Google Cloud Eventarc Python Samples

Open in Cloud Shell

This directory contains samples for Google Cloud Eventarc.

Samples

Sample Description Deploy
Events – Pub/Sub Event-driven service with Events for Cloud Run for Pub/Sub -
Anthos Events – Pub/Sub Event-driven service with Events for Cloud Run on Anthos for Pub/Sub -
Events – GCS Event-driven service with Events for Cloud Run for GCS -
Anthos Events – GCS Event-driven service with Events for Cloud Run on Anthos for GCS -

For more Cloud Run samples beyond Python, see the main list in the Cloud Run Samples repository.

Setup

  1. Set up for Cloud Run development

  2. Clone this repository:

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    

    Note: Some samples in the list above are hosted in other repositories. They are noted with the symbol "➥".

How to run a sample locally

Install pip and virtualenv if you do not already have them.

You may want to refer to the Python Development Environment Setup Guide for Google Cloud Platform for instructions.

  1. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

    virtualenv env
    source env/bin/activate
  2. Install the dependencies needed to run the samples.

    pip install -r requirements.txt
  3. Start the application

    python main.py

How to run a sample in a container

  1. Install docker locally

  2. Build the sample container:

    export SAMPLE=<SAMPLE_NAME>
    cd $SAMPLE
    docker build --tag $SAMPLE .
    
  3. Run containers locally

    With the built container:

    PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
    

    Overriding the built container with local code:

    PORT=8080 && docker run --rm \
        -p 8080:${PORT} -e PORT=${PORT} \
        -v $PWD:/app $SAMPLE
    

    Injecting your service account key for access to GCP services:

    # Set the name of the service account key within the container
    export SA_KEY_NAME=my-key-name-123
    
    PORT=8080 && docker run --rm \
        -p 8080:${PORT} \
        -e PORT=${PORT} \
        -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \
        -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \
        -v $PWD:/app $SAMPLE
    
    • Use the --volume (-v) flag to inject the credential file into the container (assumes you have already set your GOOGLE_APPLICATION_CREDENTIALS environment variable on your machine)

    • Use the --environment (-e) flag to set the GOOGLE_APPLICATION_CREDENTIALS variable inside the container

    Learn more about testing your container image locally.

Deploying a Cloud Run service

  1. Set an environment variable with your GCP Project ID

    export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
    
  2. Submit a build using Google Cloud Build

    gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
    
  3. Deploy to Cloud Run

    gcloud run deploy $SAMPLE --image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
    

Choose a particular sample for information about triggering the service with an event.

See Building containers and Deploying container images for more information.