
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create cloud storage buckets
/ 10
Create the Firestore composite index
/ 10
Create the cloud tasks queue
/ 10
Deploy the Cloud Run services
/ 10
Create a function to parse the Cloud Vision response
/ 10
Create the workflow
/ 10
Create a trigger to start the workflow
/ 10
Test the add image workflow
/ 10
Add a scheduled job to create the collage
/ 10
Trigger a service to delete image files and metadata
/ 10
Serverless computing on Google Cloud lets you develop and deploy highly scalable applications on a fully managed serverless platform. Services are automatically scaled up and down depending on traffic.
Service integration lets you easily integrate your services in an asynchronous, loosely coupled manner, allowing rapid innovation. Eventarc lets you build event-driven architectures without requiring infrastructure management. Workflows lets you easily build reliable applications by orchestrating services and APIs. Cloud Scheduler is a fully managed cron job service for scheduling your workloads. Cloud Tasks is a fully managed service for creating distributed task queues. Eventarc, Workflows, Cloud Scheduler, and Cloud Tasks help you integrate your services while maintaining observability, reliability, and security.
In this lab, you use Workflows to create a workflow that manages the process of creating thumbnails and extracting metadata from incoming images. Eventarc detects when an image is uploaded to a Cloud Storage bucket and starts an execution of your workflow.
The workflow calls the Cloud Vision API to analyze the uploaded photo. Cloud Vision determines whether the image is safe, detects text in the image, and provides labels for the image content. The vision data is extracted using a custom function created in Cloud Run functions. The extracted data is saved in Firestore, Google Cloud's fully managed serverless document database. The workflow also uses Cloud Tasks to queue a task for creating a thumbnail.
Cloud Build is used to build three services that are deployed in Cloud Run, a serverless platform for running containerized services. One of these services creates a thumbnail in response to the task created by the workflow.
The second service creates a photo collage of the most recent images. The collage service runs on a schedule created in Cloud Scheduler.
The third service is triggered by Eventarc when an image is deleted from the Cloud Storage bucket. This service will remove the metadata and thumbnail image for the deleted image.
In this lab, you will learn to:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details panel.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details panel.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
In this task, you enable required APIs. You also create two buckets in Cloud Storage: one for uploading images, and one to store images generated by your application.
To enable the required APIs, run the following command:
This application uses several Google Cloud services, and you must enable each of the APIs for these services.
The APIs being enabled:
In Cloud Shell, create a Cloud Storage bucket for uploading images:
These commands create a public regional bucket with uniform access. Each time an image is copied to the bucket, a workflow will be started to analyze the photo and store the metadata and a thumbnail image.
Create a second Cloud Storage bucket for images generated by the image applications:
In the Google Cloud console, in the Navigation menu (), navigate to Cloud Storage > Buckets.
The two buckets that you created are shown.
Click Check my progress to verify the objective.
In this task, you create the Firestore database that will store image metadata.
To create the Firestore database, in Cloud Shell, run the following command:
A Firestore collection named images will be used to store image metadata. The collage service will search the database to find the most recent images with thumbnails to create the collage.
After the database is created, you must create an index to support this search.
A Firestore composite index is used when you need a single query to reference multiple fields.
To create the composite index, run the following command:
This index will let the collage service find the most recent images that have thumbnails.
The --async parameter indicates that you do not want to wait for the operation to complete.
On the Google Cloud console title bar, in the Search field, type Firestore, click Search, and then click Firestore.
Click on your database id (default)
and from the left pane click Indexes.
You should see that the index on the images collection is being created.
Click Check my progress to verify the objective.
In this task, you create a Cloud Tasks queue for requesting thumbnail creation by the create-thumbnail service.
To create the task queue, in Cloud Shell, enter the following command:
On the Google Cloud console title bar, in the Search field, type Cloud Tasks, click Search, and then click Cloud Tasks.
The queue named thumbnail-task-queue has been created.
Click Check my progress to verify the objective.
In this task, you build and deploy the Cloud Run services that will be used by the image application.
In Cloud Shell, clone the git repository:
Create a soft link shortcut to the root directory for this lab:
Artifact Registry is the next generation of Container Registry. You can store build artifacts inside an Artifact Registry repository.
To create an Artifact Registry repository for Docker images, in Cloud Shell, run the following command:
The thumbnail service creates a thumbnail for an uploaded image and stores it in the generated-images bucket.
In Cloud Shell, click Open Editor.
In Cloud Shell Editor, Navigate to ~/code/cloud-run/create-thumbnail
and examine the files for the create-thumbnail service.
The directory contains three files:
Click Open Terminal.
To build the create-thumbnail service Docker image using Cloud Build, in Cloud Shell, run the following commands:
The service is built and stored in the Artifact Registry repository.
To deploy the service using Cloud Run, in Cloud Shell, run the following commands:
The generated-images bucket name is passed to the application using an environment variable.
The collage service combines the most recently uploaded images into a collage and stores the collage in the generated images bucket.
In Cloud Shell, click Open Editor.
In Cloud Shell Editor, navigate to ~/code/cloud-run/create-collage
and examine the files for the create-collage service.
This directory contains three files: package.json, index.js, and Dockerfile.
Click Open Terminal.
To build and deploy the create-collage service Docker image, in Cloud Shell, run the following commands:
The delete image service removes the thumbnail image from the generated images bucket and deletes the image metadata from the database.
In Cloud Shell, click Open Editor.
In Cloud Shell Editor, navigate to ~/code/cloud-run/delete-image
and examine the files for the delete-image service.
This directory only contains two files: package.json and index.js. There's no Dockerfile.
Instead of building and publishing the container manually, this lab relies on Google Cloud Buildpacks to automatically build the container.
Also note that the service parses the request as a CloudEvent. Eventarc sends events using the standard CloudEvent format.
Click Open Terminal.
To build and deploy the delete-image service to Cloud Run using Google Cloud Buildpacks, in Cloud Shell, run the following commands:
gcloud run deploy warns that an Artifact Registry Docker repository must be created for images created by Google Cloud Buildpacks.
When asked if you want to continue, enter Y
.
On the Google Cloud console title bar, in the Search field, type Artifact Registry, click Search, and then click Artifact Registry.
The repository you created (image-app-repo) and the repository created by Google Cloud Buildpacks (cloud-run-source-deploy) are in the Artifact Registry.
Click image-app-repo.
Stored in image-app-repo are the Docker images you just built for the create-collage and create-thumbnail services.
Return to Artifact Registry > Repositories, and then click cloud-run-source-deploy.
The repository contains the delete-image Docker image that was automatically created by Google Cloud Buildpacks.
In the Google Cloud console, in the Navigation menu (), navigate to Cloud Run > Services.
All three services have been deployed.
Click Check my progress to verify the objective.
In this task, you create a Cloud Run function to parse the image data returned by the Cloud Vision API.
In Cloud Shell, click Open Editor.
In Cloud Editor, navigate to ~/code/cloud-functions/extract-image-metadata
and examine the files for the extract-image-metadata function.
The Node.js service contains index.js and package.json files. Cloud Run functions automatically uses Buildpacks to create the container, so no Dockerfile is required.
Click Open Terminal.
To deploy the function, in Cloud Shell, run the following command:
On the Google Cloud console title bar, in the Search field, type Cloud Run functions, click Search, and then click Cloud Run functions.
The extract-image-metadata function has been deployed.
Click Check my progress to verify the objective.
In this task, you create a workflow to orchestrate your service and API calls.
In Cloud Shell, click Open Editor.
In Cloud Shell Editor, navigate to ~/code/workflows
and examine the image-add-workflow.yaml YAML file.
The workflow specifies a series of steps to be performed when the workflow is initiated. Here are the non-logging steps:
Click Open Terminal.
To create a service account for the workflow identity and add some basic permissions, run the following commands:
The roles permit the following functionality:
To add permissions to call the function and Cloud Run service, run the following commands:
If asked, Would you like to run this command and additionally grant permission to invoke function [extract-image-metadata] (Y/n)?, enter Y
.
These additional roles permit the following functionality:
To deploy the workflow, run the following command:
On the Google Cloud console title bar, in the Search field, type Workflows, click Search, and then click Workflows.
Click the Pin icon next to Workflows.
Click image-add-workflow, and then click the Source tab.
The left pane displays the code for the workflow, and the right pane displays a flow visualization for the steps of the workflow.
Click Check my progress to verify the objective.
In this task, you will create an Eventarc trigger that runs the workflow when a file is added to the uploaded-images bucket.
To create a service account for the workflow trigger, and grant it the necessary permissions, in Cloud Shell, run the following command:
These commands create a service account named workflow-trigger-sa, and add roles to receive events and invoke workflows.
To grant the Cloud Storage service account permission to create events, run the following command:
These commands retrieve the Cloud Storage service account and add the permission to publish Pub/Sub events.
To create the trigger, run the following command:
This trigger will call the workflow whenever a file is written to the uploaded-images bucket.
Click Check my progress to verify the objective.
In this task, you test the steps that occur when an image is added to the uploaded-images bucket.
To upload an image to the uploaded-images bucket, run the following commands:
When the upload finishes, Eventarc detects the upload and starts the workflow.
In the Google Cloud console, in the Navigation menu (), navigate to Workflows > Workflows.
Click image-add-workflow, and then click the Executions tab.
You should soon see a workflow execution.
Click the execution ID to open the Execution Details page.
This page shows the details of the workflow execution.
The Input pane shows the contents of the Cloud Storage event that was sent by the Eventarc trigger.
The Output pane shows the values that were returned at the end of the workflow.
The Logs pane shows log entries created by the workflow.
On the Google Cloud console title bar, in the Search field, type Cloud Run functions, click Search, and then click Cloud Run functions.
Click extract-image-metadata.
This dashboard shows details about the function used to extract the information from the Cloud Vision response.
Click the Logs tab.
Logged data from the function execution is found here. The labels and text for the image were logged.
On the Google Cloud console title bar, in the Search field, type Firestore, click Search, and then click Firestore.
Click (default).
The images collection should show a document named neon.jpg that was written by the workflow. The labels attached to the image and the text found within the image are shown.
In the Google Cloud console, in the Navigation menu (), navigate to Cloud Run > Services, and then click create-thumbnail.
This dashboard shows information about the create-thumbnail service.
Click the Logs tab.
Data logged from the service execution is found here.
In Cloud Shell, run the following commands:
The generated-images bucket should have a thumbnail created by the create-thumbnail service. You can click the links within Cloud Shell to open a new tab and see the uploaded and generated images.
Click Check my progress to verify the objective.
In this task, you will create a Cloud Scheduler job to periodically create a collage of the most recently uploaded photos.
To upload more images to the uploaded-images bucket, run the following commands:
The workflow will run for each image, resulting in a thumbnail for each image.
To create a service account for calling the create-collage service, run the following commands:
To retrieve the service URL for the create-collage service, in Cloud Shell, run the following command:
Copy the URL into the clipboard. You will need this URL when creating the scheduled job.
On the Google Cloud console title bar, in the Search field, type Cloud Scheduler, click Search, and then click Cloud Scheduler.
Click +Create Job.
Specify the following schedule settings:
Property | Value |
---|---|
Name | collage-schedule |
Region |
select |
Frequency | * * * * * |
Timezone | search for UTC and then select Coordinated Universal Time (UTC) |
The frequency specifies the schedule in unix-cron format. * * * * * specifies that the job will run once a minute.
Click Continue, and then select the Target type of HTTP.
Specify the following execution settings:
Property | Value |
---|---|
URL | paste the create-collage service URL |
HTTP method | select POST |
Auth header | select Add OIDC token |
Service account | select collage-schedule-sa |
Audience | paste the create-collage service URL |
Click Continue, and then click Create.
The collage-schedule job is displayed on the Cloud Scheduler Jobs page.
The job should run within a minute.
Click Refresh until the collage-schedule job shows a check mark, indicating success.
Click Check my progress to verify the objective.
In Cloud Shell, run the following commands:
The generated-images bucket should now include the collage.png file. You can click the "generated collage" link within Cloud Shell to open a new tab and see the collage.
In this task, you will create an Eventarc trigger to remove the associated thumbnail image and remove the Firestore document when an image is deleted from the uploaded-images bucket.
To create a service account for the delete-image trigger, and grant it the necessary permissions, in Cloud Shell, run the following command:
These commands create a service account named delete-image-trigger-sa, and add permissions to receive events and invoke the delete-image service.
In the Google Cloud console, in the Navigation menu (), navigate to Cloud Run, and then click delete-image.
On the Service details page, click the the Triggers tab.
Click + Add Trigger, and then select Cloud Storage trigger.
Specify the following trigger settings:
Property | Value |
---|---|
Trigger name | image-delete-trigger |
Event type | select google.cloud.storage.object.v1.deleted |
Bucket | click Browse, then select the uploaded-images bucket, and then click Select |
Service account | select the delete-image-trigger-sa service account |
Service URL path | / |
If you're told that Pub/Sub must be granted a role, click Grant.
Click Save Trigger.
This trigger will call the delete-image service whenever a file is deleted from the uploaded-images bucket.
Click Check my progress to verify the objective.
To list an image in the Cloud Storage buckets and the corresponding document in Firestore, run the following commands:
These commands display the Cloud Storage and Firestore details for a single image. Next, we remove that image.
To delete an image in the uploaded-images bucket, run the following command:
Eventarc will detect the removed file and call the image-delete service, which removes the thumbnail and Firestore document.
To check whether the thumbnail and Firestore document have been removed, run the same commands you ran earlier:
The items have been removed.
In the Google Cloud console, in the Navigation menu (), navigate to Cloud Run > Services, and then click delete-image.
Click Logs.
The logs for the delete-image service show that the thumbnail was deleted from the generated-images bucket, and the Firestore document was deleted from the database.
You successfully used Eventarc to trigger services and workflows. You orchestrated services and APIs using Workflows. You created and used a distributed task queue using Cloud Tasks. Finally, you created a job to run a service using Cloud Scheduler.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one