Build a Serverless App with Cloud Run that Creates PDF Files
Enable the Cloud Run API
- Open the navigation menu and select APIs & Services > Library. Then in the search bar, enter in “Cloud Run” and select the Cloud Run API from the results list.
- Click Enable and then hit the back button in your browser twice. Your Console should now resemble the following:
Trigger your Cloud Run service when a new file is uploaded
- Run the following command to create a bucket in Cloud Storage for the uploaded docs:
gsutil mb gs://$GOOGLE_CLOUD_PROJECT-upload
- And another bucker for the processed PDFs:
gsutil mb gs://$GOOGLE_CLOUD_PROJECT-processed
- In Cloud Shell run the following command to tell Cloud Storage to send a Pub/Sub notification whenever a new file has finished uploading to the docs bucket:
gsutil notification create -t new-doc -f json -e OBJECT_FINALIZE gs://$GOOGLE_CLOUD_PROJECT-upload
The notifications will be labeled with the topic “new-doc”.
Deploy a simple Cloud Run service
- Open a new Cloud Shell session and run the following command to clone the Pet Theory repository:
git clone https://github.com/webmagicinformatica/Build-a-Serverless-App-with-Cloud-Run-that-Creates-PDF-Files.git
- Then change your current working directory
cd Build-a-Serverless-App-with-Cloud-Run-that-Creates-PDF-Files - Now run the following commands in Cloud Shell to install the packages that your conversion script will be using:
npm install express npm install body-parser npm install child_process npm install @google-cloud/storage
- To build and deploy the REST API, use Google Cloud Build. Run this command to start the build process
gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/pdf-converter
- The command builds a container with your code and puts it in the Container Registry of your project.
- Return to the Cloud Console, open the navigation menu, and select Container Registry > Images. You should see your container hosted:
- Return to your code editor tab and in Cloud Shell run the following command to deploy your application:
gcloud beta run deploy pdf-converter \ --image gcr.io/$GOOGLE_CLOUD_PROJECT/pdf-converter \ --platform managed \ --region us-central1 \ --memory=2Gi \ --no-allow-unauthenticated \ --set-env-vars PDF_BUCKET=$GOOGLE_CLOUD_PROJECT-processed
- Create the environment variable $SERVICE_URL for the app so you can easily access it:
SERVICE_URL=$(gcloud beta run services describe pdf-converter \ --platform managed --region us-central1 --format="value(status.url)") echo $SERVICE_URL
- Make an anonymous POST request to your new service:
curl -X POST $SERVICE_URL
This will result in an error message saying “Your client does not have permission to get the URL”. This is good; you don’t want the service to be callable by anonymous users.
- Now try invoking the service as an authorized user:
curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" $SERVICE_URL
If you get the response “OK” you have successfully deployed a Cloud Run service. Well done!
- Then create a new service account which Pub/Sub will use to trigger the Cloud Run services:
gcloud iam service-accounts create pubsub-cloud-run-invoker --display-name "PubSub Cloud Run Invoker"
- Give the new service account permission to invoke the PDF converter service:
gcloud beta run services add-iam-policy-binding pdf-converter --member=serviceAccount:pubsub-cloud-run-invoker@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com --role=roles/run.invoker --platform managed --region us-central1
- Find your project number by running this command:
gcloud projects list
- Create a PROJECT_NUMBER environment variable, replacing [project number] with the Project Number from the last command:
PROJECT_NUMBER=[project number]
- Then enable your project to create Cloud Pub/Sub authentication tokens:
gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT --member=serviceAccount:service-$PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com --role=roles/iam.serviceAccountTokenCreator
- Finally, create a Pub/Sub subscription so that the PDF converter can run whenever a message is published on the topic “new-doc”.
gcloud beta pubsub subscriptions create pdf-conv-sub --topic new-doc --push-endpoint=$SERVICE_URL --push-auth-service-account=pubsub-cloud-run-invoker@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com
See if the Cloud Run service is triggered when files are uploaded to Cloud Storage
- Copy some test files into your upload bucket:
gsutil -m cp gs://spls/gsp644/* gs://$GOOGLE_CLOUD_PROJECT-upload
- Once the upload is done, return to your Cloud Console tab, open the navigation menu, and select Logging from under the Operations section.
- In the first dropdown, filter your results to Cloud Run Revision and click Add. Then click Run Query.
- In the Query results, look for a log entry that starts with file: and click it. It shows a dump of the file data that Pub/Sub sends to your Cloud Run service when a new file is uploaded.
- Can you find the name of the file you uploaded in this object?
- You can also go to the Processed Bucket to check PDF generated
Comments are closed, but trackbacks and pingbacks are open.