 And after talking about Monterey pools, we're going to talk about Flask in Google Cloud. Evie, are you there? Oh, hey, yes, I am here. Hello, do you have your slides ready as well? Yes, let me go ahead and present them. You're talking about Flask in Google Cloud, right? Yes, but this is mostly gonna be focusing on the cloud aspect of things. Cool. Let me get my speaker out. All right, so should I go ahead and go? Okay, all right, so, hey everyone, this is a talk and demo focused around developing Flask apps for Google Cloud. But I'm assuming that most of you are already familiar with Flask, so this talk is a bit more focused around the cloud aspect of things. And you don't need to be a Google Cloud expert to grasp this. This is just a beginner level talk and I'll cover the basics. So a little bit about me, my name is Evie Carey and I am a developer advocate at Google Cloud and I have been mostly focusing on DevTools. My Python experience is, I'm fairly beginner, but I've been working on it on and off for a couple years now. And if you want to give me a follow, I am at Abby Applebee's on Twitter. So what am I gonna be covering today? First, we're gonna take a look at Cloud Run and Cloud Code. Cloud Run is a platform and Cloud Code is a DevTool that I'll be using in my demo. So now I'm gonna move on to my demo and show you how to get started with Flask and Cloud Run development locally. And to make things a little bit more excited, I'm going to build on that application a little bit and add a couple Cloud APIs to it. And then finally, I'm gonna deploy my application to the cloud. So let's take a look at serverless and Cloud Run. So Cloud Run is one of Google serverless offerings and the word serverless is thrown out a lot, but what does that mean exactly? So this is the serverless dependency pyramid. What do you need to do? So those two cubes at the top or squares are all that you need to worry about, which is application development and performance monitoring. But everything else that's grayed out, Cloud Run handles for you. It's all managed, including scaling to your workload for traffic, as well as your physical servers, hence the name serverless. So basically, this is a long-winded way of saying that you just have to focus on your code. Okay, so a little bit more about Cloud Run. So Cloud Run is container-based and it lets you deploy stateless HTTP containers. It is super fast and I'll show you in a bit. It does not take a long time to deploy a Cloud Run application. We're talking in a minute and if you're developing locally, a couple seconds. You get the flexibility of configuring your own custom domain. So once you deploy the cloud, if you don't like the URL it gives you at the end there, you can add your own custom one. And it's worth custom binaries. So any language or library is fair game and I mean like anything. And because it's container-based, it's portable, so you can take it to a different cloud provider if you want to. And one of the biggest appeals I like about Cloud Run is that when no one is accessing my service, it can scale down to zero, which means it doesn't cost anything. All right, so now I'm gonna go over a couple of Cloud Run use cases and the most straightforward one is building a website with Cloud Run. And this is what I'm gonna show in my demo in a little bit. But so you can build a website with your preferred tech stack and have it connect to a cloud SQL database. Another use case would be data processing. So in this diagram here, I have a cloud storage bucket. And then if I were to upload a CSV file to this bucket, I could then have it trigger my Cloud Run service. And then my Cloud Run service would take that CSV, perform some data transformation on it and then store it to BigQuery. So this pipeline takes lightweight data and turns it into structured data that can later be analyzed. Okay, and then there is automation. So I think this one's a pretty cool one. So for this example, with Cloud Scheduler, you can schedule a monthly job which generates an invoice using a Cloud Run service. And my Cloud Run service could have LibreOffice in it and then it could generate a PDF and then store it in Cloud Storage based off of a customer information connected through your Cloud SQL database. Okay, so now that was a bit about Cloud Run. What about Cloud Code? Which is that DevTool I mentioned. So Cloud Code is an IDE plugin that automate development workflows for cloud native applications. So this is a plugin that's composed of a bunch of tools that assist you in a variety of workflows and it integrates these tools right into the UI of your IDE. And its goal is to automate repetitive processes seen in your inner development loop and overall just to save you time and make you more productive when developing on Google Cloud and in particular Cloud Run. So who can get in on the Cloud Code action? And there are six ways and that is in four of them happen to be JetBrains IDEs including PyCharm and IntelliJ as well as Visual Studio Code and our cloud-based editor, Cloud Shell Editor. And we support five languages including Java, Python, Go and Node.js. And if you're working in the Visual Studio Code version we support .NET as well. But all you have to do is install Cloud Code from your extensions marketplace. Okay, so what does Cloud Code, how does Cloud Code help out your Cloud Run development? So first it helps you get started fast and with the Cloud Code we provide a sample starter applications to get you started that are ready to be containerized and they show best practices along the way. And then we have the whole build, push and deploy scenario. So these repetitive tasks are condensed into a simple UI. So it's easy to repeat it over and over again. I have to type in like a bunch of terminal commands. It's just like one button. And auto redeploy on changes. So Cloud Code has a feature where as I'm developing locally if I hit save it'll automatically do a rebuild and redeploy to my local development emulator which I will show in a minute here. And just watching that while iterating on your code that way hopefully speeds up your development time. And then you can also leverage Cloud APIs. So we have a Cloud API Explorer and from there you can view all the Cloud APIs, quickly enable them and get documentation. You need an overall the goal is to just keep you on your IDE and focus on your code. And I actually have an interactive demo if any of you all want to try it out. It is at go.gle slash cloud dash run dash demo. And that's in our cloud-based IDE and it just walks you through creating a Cloud Run application and then deploying it to the cloud. Okay, so I'm about to dive into my demo now but I want to highlight a very crucial part. Oh, where is this? Nope. I want to highlight a very crucial part of creating an application that's going to be deployed to the cloud and that is service accounts. So service accounts you would use them when running workloads you don't want tied to the life cycle of a human user. So ideally every Cloud Run service would have a service account attached to it. And so with service accounts as an identity you can grant roles on them and that will allow you to access resources like projects or call APIs. And as a resource you can grant roles to other users and to let them manage your services, your service accounts, sorry. Okay, and looking at this picture here if you have an application running on a, if you have a cloud application running on Cloud Run and you want your application to only have access to create objects in a cloud storage bucket you would assign it storage.object creator role. Yeah, my first started developing a Cloud Run applications this was a little confusing for me. So just wanted to highlight that here. Okay, and now it is demo time. What I'm going to do is I'm going to create a base Flask application iterate on a little bit add a couple of cloud APIs and then deploy it to the cloud. Okay, so I'm going to open up Visual Studio Code because that's where I'm going to be running my demo today. And let's see here, got my notes. So I'll be running my demo in Visual Studio Code today but this flow is very similar to if you're running it in PyCharm. So I've installed it from my extensions marketplace and once I've done that I get this UI over here for working with Google Cloud products including Kubernetes, Cloud Run, Cloud APIs and secret manager. But today I'm not focusing on Kubernetes because that's a little scary. So what I'm going to do now is create a base application to build on. So I'm going to click Cloud Code here and I'm going to select new application and I'm going to do a Cloud Run application and I'm going to select the Python Flask Cloud Run sample and clone it. All right, so that's going to download and it's going to give me a great base to start with that's already ready to be containerized and deployed. Cool, okay. So I got this handy reading here but I'm not using that today. So let's take a look at the source code. I have a templates folder here with index.html and this is just going to render a webpage that displays a message and a little bit of congratulations text and we're also going to pull in some environment variables here in my app.py file. Okay, and now here in app.py, so here's my Flask application and I've created my message here, it's running and here is where we're going to pull in some environment variables about this service and then I'm going to send that over to the template and render it. I also have a Docker file and so this is here so I can containerize it easily but I want to call out that you don't need to know how to make a Docker file. I could just delete this file and I'd be totally fine deploying this to Cloud Run because I could build with build packs instead and what build packs do is it analyzes your application and determines the best way to containerize it so that I don't need to deal with making this file. Okay, so this is ready to be deployed to the cloud but maybe I want to iterate on it a little bit which I am going to do, add some APIs to it. What I'm going to do is run this locally in the Cloud Run emulator, so I'm going to select Cloud Run emulator and what this does is it emulates the Cloud Run production environment locally so there aren't any surprises when I actually do go to deploy this to the cloud. So in build settings I have Docker selected but I could also go with build packs since I already have a Docker file I'll keep it as Docker. In this advanced service settings area this is where I can configure my emulator and so I already have my service name pipeline based off of my folder name, the port that we'll be listening on and this is important that service account field. I will not be able to call any cloud APIs without having a service account specified here. So I've already created one beforehand let me grab the email it's kind of silly their email addresses that's their format they're in because it's an identity so that's kind of interesting but to note I've already applied a couple of special permissions on this service account so it can interact with two APIs I'll be using later. For dedicated CPUs I'm going to set to one virtual CPU to simulate a workload and I'm also going to call environment variables I can set some Cloud Run environment variables here and Cloud SQL connections. I don't have time to hook up a Cloud SQL database in this demo but if I were to this is where I would specify it and then everything would just work great. So now I'm going to hit run and now Cloud Code is doing the building, pushing and deploying to my Cloud Run emulator and this should take a second. Go on the frozen one second. Okay, might need to restart one sec. Okay, let's try this one more time. Cloud Code, run on Cloud Run emulator. Okay, okay, it's going up. Oh, I was getting nervous for a second. Okay, so all right, so now we are deploying to our emulator, let me open this up now and I will show you what we have. Okay, so this is my application that is, this is my Cloud Run service that I've deployed to my local Cloud Run emulator and you can see that it's running message here and those environment variables that we pulled in but I want to make this application a little more interesting. So what I'm going to do is I'm going to take this it's running text and use a Cloud API, the translation API to translate this from English to French. I think the Cloud Translation API is just one of the more visually interesting ones. That's why we're going with that today. Okay, pull my notes back up. So now I need to, I'm going to keep it running though. I'm keeping the emulator running so that I can iterate on it. So I'm going to go to the API Explorer because the first thing I need to do is make sure that the translation API is enabled. So I'm going to go over here, select it and I've actually already enabled this for my project which is why I got that green check mark but if I didn't, this would be a little button to enable it and there's some information here on how to authenticate and install the client library but I'm going to show you how to do that now. So first thing I need to do is I'm going to go to requirements.txt and I'm going to add in my package for the translate client library and I'm going to save that and then in app.py, now I'm going to import it. So from google.cloud import translate. All right, so now I'm ready to call the translation API. So down here I'm going to add a new function and I've stuck it in a snippet so that it is easier. You don't have to watch me type for like a half hour. Okay, so this is my translate text function and I'm going to take a string and then also my project ID and so what's going to happen is I'm going to create a translation service client so I can interact with the client library and then I'm going to coordinate a string that creates my parent and the parent is important because this is my caller information for the API and it's tied with billing and things like that. So now I am going to use my client and call the translate text function and I'm going to pass in my parent, the string I want to translate and I'm going to specify the mime type and I learned that this is important because if you don't apostrophes and quotes won't render correctly and then I'm setting my source language, which is English and then my target language, which is French and then I'm going to return the translated text for that first piece of translation that I do. All right, so now I'm going to translate this, it's a running message up here. So I'm going to do translate, what did I call it? Oh, translate text. Translate text, give it, it's running and then I'm going to say Avikar project which is my project ID and I'm going to hit save and hopefully, oops, let me go. Back here to our output, it's building now and hopefully we get a piece of translated message in the end, deploying, okay, and we're done and let me drag this on over and give it a refresh. Oops, it's just redeploying right now. Okay, there we go. This is my translated text. I think this says it's running in French. Maybe some of you can correct me, but I trust the API here. All right, so that was how I added the cloud translation API and I was able to call that API successfully because my service account has the translation API user role assigned to it, very important. Okay, now I want to talk about like a security element that I could add to this. So let's say you work with API keys or you have your database credentials inside of your application. Not exactly the best thing to have exposed, especially when you deploy it, when you host it somewhere on GitHub where everyone can see it. So that is where a secret manager can come in. I don't necessarily have anything I need to make secret in here, but for this example, I'm going to pass a secret into here to translate. Secrets just hold strings. So what I'm first going to do is I'm going to create a secret and I already have from the API Explorer, I already enabled the secret manager API, so I'm ready to go. So I'm gonna go over to the secret manager explorer here and I've already created some secrets, but let's make a new one. All right, and the secret name is gonna be Euro secret. And it's gonna say, cheese omelet. That's a cool word to translate. All right, so now it's ready. I'm gonna create it and we should see it pop up over here. We got some instructions on how to add to my code, but I'm gonna close that out and I'll show you how to do it. So Euro secret is now on my list here. And if I open it up, I see that I have one version of my secret. All right, so now let's add this secret to my application. Let me, first I'm gonna go to requirements.txt and I'm going to add in the package. Boom, all right, now I'm gonna go to app.py and I'm going to import secret manager. Awesome, and now I'm gonna use a snippet. Again, you have to watch me type a lot for a secret manager, my secret manager function for accessing my secret. All right, so this function I have here, this access secret version, this takes the version ID of a secret and then it's gonna grab the string out of it. So first in here, I'm gonna create secret manager service client so I can interact with the client library. Then I'm going to use the client library's access secret version function and pass in my version ID of my secret. And then I'm going to decode that payload and then return the string. So now I'm going to try and call it. So I'm gonna go up here to my message. I'm gonna create a new variable, secret message. I'm gonna set it equal to, oops, underscore. I'm going to set it equal to access secret version and then now I need to pass in my version ID and to get that, I can go to my secret manager explorer and then just click this little clipboard button and that just copied the resource ID for my version. Paste that in there. It's good to go now. And now I'm just gonna call the secret message instead. Okay, so ideally this will translate my secret message and we'll see it on my little app. So I'm building and deploying again. Let me find my application. Okay, still building, deploying. It looks like it's done. I'm gonna give it a second so it finalizes. Okay, now it's trying to refresh. All right, there is my secret that has now been translated and displayed in my cloud run emulator locally. So this looks awesome and now I want to deploy it to the cloud. So how am I gonna do that? I'm gonna stop this. And now I am going to click the cloud code button. Oh, no, actually I'm going to go to the cloud run explorer. And in here you can see a bunch of cloud run services I've already deployed, but now I'm gonna deploy a new one. Okay, so we're creating a new service. This is good. I can also redeploy a revision of an existing service. The name I'll call it Euro service. I can select my region. I'll keep it US central one. And I'm going to leave allow unauthenticated implications on because this would then make it a public website. If you are in some of those use cases I showed where you're doing some sort of data pipeline you would probably want to make this authentication required so it's not publicly exposed. All right, so revision. I'm gonna rename it Euro service. It's my container image URL. Service account cannot forget this one. Gotta put that in. Let me grab it. Oh, and I should also call out that I was able to access that secret from my application because I had the secret manager access role on it. Okay, and in advanced settings you'll notice this looks a lot very similar to the emulator. I can set CPU locations, environment variables, cloud SQL connections. And I'm gonna be building locally with Docker but you could build with cloud build remotely if you don't want to take up resources on your machine. That also incurs cost as well. So I'm gonna be doing the local. I'm gonna keep my builder as Docker instead of build packs and then I'm gonna select deploy. And hopefully this will deploy and then we will see it appear over here. It takes a second but you can view the detailed logs and see what's going on behind the scenes. All the containerizing action going on. Oh, and yeah. So I think I'm a bit out of time but this will deploy and then it will be, oh, it's done. I'm just gonna click it real quick. And here is my deployed cloud run service that is ready to be accessed by anyone on the web. All right, now I'm just gonna go back to my slides real quick and wrap things up. All right, so thank you for watching my demo and this is look at cloud code being used as about cloud run applications in particular Python Flask applications. And you wanna follow us on Slack. We got some, we have a Slack that you can connect with us on if you have questions. And also I have a link to our repo of Python cloud run samples if you wanna try them out. Thank you so much, Abby. Got you a couple of questions. So can we use cloud run with GCP data store? If yes, can we utilize it to automate generation of reports? How fast is the generation of reports? Yes, you can use it with data store. I'm not, I haven't tried it myself before so I can't comment on how fast it is but yes, you definitely can use it with cloud run. Cool. And last question. Can I run cloud code in FIM or Emacs? I didn't see that on your slide. No, unfortunately not. Yeah, we only have the, we only support the, those big IDEs, those JetBrains IDEs and little studio code, unfortunately. But if you want that as a feature, maybe you should file a feature request on our repo. Right. Is there a command line tool though? Not for cloud code, but we do have a G Cloud command line tool which you can do everything I showed from the terminal. Right. Cool. But yeah. Yeah. Yeah. Good to know. Thanks, Abby. Yeah, thank you. Thanks for having me.