ABBY CAREY: Hey, I'm Abby, and I'm
a developer advocate for Google Cloud Developer Experiences.
NAFIUL ISLAM: And I'm Nafiul from JetBrains.
And we're going to walk you through how
to build a Cloud Run service using a little plugin
in PyCharm called Cloud Code.
ABBY CAREY: Cloud Code is a plugin
that makes your local development experience
with Google Cloud Services, like GKE and Cloud Run,
more streamlined and convenient.
It's supported for a variety of IDEs,
including PyCharm and IntelliJ.
Nafiul, how can everyone get in on the PyCharm and Cloud Code
action?
NAFIUL ISLAM: Well, first, you need PyCharm installed.
Now to install Cloud Code, head to the marketplace
by clicking Preferences, then Plugins,
and search for Cloud Code.
You might be prompted to restart PyCharm.
Once that's done, Cloud Code should be ready.
You'll need Docker installed and running in the background
to use Cloud Code.
While Cloud Code automatically installs
command-line dependencies, like the Cloud SDK, for you,
you might need to manually install it
if you have certain permissions blocking the install.
Later in this video, I'll show you
how to add a Cloud SQL database connection to both your Cloud
Run service and PyCharm's database panel.
For that step, you'll need to install Cloud SQL Proxy.
Check the description for more info on how to do this.
Abby, want to show how to get started with a Cloud Run
sample?
ABBY CAREY: Sure thing.
Once you install Cloud Code, you'll
notice some new UI in PyCharm.
To the right, there are Cloud Code-specific panels
that align with different Google Cloud flows.
We'll be focusing on Cloud Run, so I'll open Cloud Code's Cloud
Run panel.
Here, you can view your deployed Cloud Run services
and right click them to view their URLs.
Getting started with Cloud Run has a slight learning curve
if you choose to build your image with Docker.
With Cloud Code, you can create a new Cloud Run
project that comes with a handy Dockerfile,
or build with buildpacks instead.
To create a new project, select New Project,
go over to Cloud Code, Cloud Run, and then give it a name.
You can then choose between Flask or Django
for your project.
We're going to choose Flask and hit Create.
Now that the sample is loaded, you
have a boilerplate project for Flask.
It's a simple Hello World service,
and here's that Dockerfile I mentioned.
To run a Cloud Run service locally,
Cloud Code comes packaged with a Cloud Run emulator.
When you run your service locally in the emulator,
you get to test and debug your code
in a production-like environment.
This sample's emulator is already configured,
but what if you wanted to add environment variables
or connect this service to a Cloud SQL database?
You can edit your configuration, and under Advanced Revision
Settings, you can update your service's CPU allocation,
Environment Variables, and specify Cloud SQL database
Connections.
To connect your Cloud Run service to a Cloud SQL
database, just put your instance connection name here
and you're good to go.
In a minute, Nafiul will walk through how
to connect PyCharm to a Cloud SQL database
to make working with databases easier.
You can also specify a Service Account,
which is very important for authenticating your Cloud Run
service with Google Cloud APIs.
And towards the bottom of my run configuration,
I can pick my builder.
I'll stick with using my Dockerfile,
but if I didn't have one, I could select Buildpacks,
which analyzes your application structure
and determines the best way to containerize it.
But everything is looking good.
Now I can either debug or run my service in the emulator.
I'm only going to run this service in this video.
But if I were to debug this, it would look exactly
like the PyCharm debugging experience you're used to.
All right, now let's run this.
PyCharm is now running the application locally.
The first time you spin up the emulator
can take a couple minutes, but future runs are much faster.
Now we got a local URL, so let's try clicking on it.
And boom, your app is hosted locally
in the Cloud Run emulator and logs
are displayed in the output.
This service is ready to deploy to Cloud Run.
But before we do that, though, Nafiul,
could you talk about PyCharm's database support?
NAFIUL ISLAM: Yeah.
PyCharm Pro comes with integrated database tools.
PyCharm's Databases panel allows you to seamlessly check
your Cloud SQL database.
This lets you check to see if your code is making the desired
changes in your database.
To connect to a Cloud SQL database from PyCharm,
you'll need to install and run the Cloud SQL
Proxy from a terminal.
Then all we need to do is give it
the name of the instance we want to connect to
and the port we want to forward that connection to.
This creates a proxy to our localhost on that port.
After doing this, we can head into our Databases tab
and add a database.
In this case, I'm adding Postgres.
I've setup a username and password, so I'll put that in.
Now I'll test the connection.
In general, it's always good to check that your connection is
working with the configurations and credentials you've
given it.
Once we have that setup, we can see
what schemas are available to us and what
tables are available in that particular schema.
That's the quick and easy way to work with Cloud SQL databases
from PyCharm.
Now I think it's time to deploy our service.
ABBY CAREY: To deploy your service,
you now need to edit the Deploy Configurations
and give it a Google Cloud project.
From here, you can set extra configurations,
like we did for the emulator.
Everything seems to be in order.
I'm building my image locally with Docker,
but you can also build in the Cloud with Cloud Build.
Because we're building this from a Dockerfile,
we can hit Apply and then OK.
Now we can press that green Play button and run this.
This tells PyCharm to start deploying.
Cloud Code is now building and pushing my container image.
And once that's complete, we get a service
URL and my-first-service appears in the Cloud Run panel.
We also get a handy link to the logs of my deployed service.
And congratulations, your service
is now running on Google Cloud.
Anyone can access it.
That was a look at getting started with Cloud Run using
PyCharm and Cloud Code.
I encourage you to install Cloud Code from your IDE's
marketplace, and if you have any questions,
you can reach out to us on our Slack channel.
NAFIUL ISLAM: Thank you so much for watching.
If you want to learn more about how to use PyCharm,
check out our YouTube channel.
We're constantly putting out new videos
that will teach you how to use PyCharm
and also how we make PyCharm.