Cloud OnAir: Build Your First Serverless Application With Google Cloud Functions
Welcome. To cloud on air live webinars, from Google cloud we, do these every week, so be sure to come back next week and see all the great things you can do with Google cloud my. Name is Bret McGowen and I'm gonna be talking about building, your first service, application, with, Google cloud functions. We. Will have Googlers on standby, to answer your questions so feel free to submit them through the platform as we go. Let's. Get started what, is Google cloud functions, cloud, functions are these. Functions of code that run automatically. In response, to events in the cloud so, these could be explicit, Google cloud platform events. They could be HTTP, events and more, and broadly. This category, of compute is called functions, as a service, or faz, but. Let's, back up a question and a back up a minute ask the question why. Functions, not applications, we've been building applications, for a long time so, why do we want to deploy just individual. Small snippets, it sounds like a lot of work well. Let's rewind a few. Years ago when we were running all of our software in datacenters either data, centers that we owned or maybe, we were co-locating. In some hosting. Provider we, had shared machines, and that's because servers were expensive, and to get a new server you had to purchase one and had to get shipped and set up so, to optimize, we would put multiple, applications, and multiple. Services, on the same physical server and this, worked ok but. There were some issues for, example if your services, had conflicting, dependencies. Maybe, third-party, libraries, or frameworks they. Could conflict and interact with each other and moreover, if one, of your services started, using too much CPU, or Ram or disk space it could impact the performance of other services, on this same server. So. Then virtual machines came along and this, is great because you could basically give a whole operating, system to each service and it could do whatever it wanted install, whatever dependencies. Have, whatever framework versions, and that's, great but. It's still a lot of overhead to manage an entire operating system just, for one small service, and it, takes a while to boot, up a virtual machine sometimes, minutes in some cases, so. Then Along Came containers. And this, is great because the same fundamental, operating, system kernel is shared, across multiple services. Even, though within the container each application, or each service thinks, it has its own OS and there's, some great ways using, your container configuration. To, define. What is your runtime environment, what operating system what, libraries. Would modules, but. There's still a lot of management. Involved, in that you have to see how, many versions of this or how many copies. Of this container do I need is.
Are, They up and running how do i reboot them how does networking, work who's connecting, disk, space there's, still a lot of infrastructure. Management, so. Then we move into the world of platform-as-a-service. And then ultimately functions. As a service and somewhere. In the spectrum, we get into the world of what we call serverless, which, we'll talk a little bit more about later but. The basic premise of service server lists is you just write your code and deploy it and that's all you need to, worry about, but. Still why. Functions, what's, the advantage we get but just having functions, well. Let's talk briefly about micro, services, and micro, services is a huge, deep, topic, that we could talk for hours about but, just very briefly. Imagine. You have a typical monolithic. Application, the, kind of app we've written for years and years let's, say it's an e-commerce application. It has all these sub components, and sub modules, so things, like a pricing service or credit, card payment processor, authorization. Service all the things you need to run an e-commerce website, so. We want to move these out and extract. Them into their own micro. Services, but. Why what's the advantage so. One, advantage is independence. Of scale if one, of your services say the, authentication service, needs, a lot more resources, or different types of resources, than the rest of your application, extracting. It into its own micro service lets you scale it independently. So instead of having to scale up and scale down your entire application, you. Can just scale one, particular, service, similarly. If other, services, have different, release Cadence's or release cycles, so, say your credit card payment processing, service is really delicate it handles money you, only want to update that maybe once a month but, maybe you're pricing. Service you want update everyday as you experiment with new pricing models and discounts, and so forth you don't want to have to necessarily deploy. Those at the same time so, making them independent lets.
Request. Is the data that's coming in from the user and response. Is what's being returned to them so, you can directly do things like set HTTP, status code 200 okay, and send, a message back to the user if, you've, written any Express, for nodejs, you'll recognize this pattern because, it is using Express j/s under, the hood but. One thing I want to point out is that with just three, lines of code and, no additional configuration. You automatically. Get a fully, qualified domain name an, HTTP. Endpoint and an SSL, surgeon, rated for you without any other work so, it's a really easy way to deploy an HTTP, endpoint. Automatically. That scales and has sub second billing. Just. Want to talk a little bit about some, automatic, request, parameter. Process, parsing, so. Cloud. Functions will look at your, query string for example or say, you're building an API a REST API and, you want, to handle an HTTP, POST well. Cloud functions will automatically, parse the, body of your post so, if it's JSON you can access it at request body, dot property, or request, query, dot property for, query string values. But. Let's, take a look at a demo. So. I wanted to do something a little bit more complicated than hello world so, we're gonna do hola world which is an internationalized. Version, of hello world so, in just a few seconds we're gonna create a hello, world application that, can say hello world in any, language, I'm gonna, go ahead and start the deploy because, sometimes it can take a bit a few, seconds so I'm gonna do g-cloud which is our command-line, utility, for interacting, with Google Cloud so. G-cloud functions, deploy, I'm gonna name the function hola world and I, wanted to be triggered by HTTP. So. Let me go ahead and start that and. While. That's deploying. I'm. Gonna talk through the code very briefly so. Here I'm going to reference, the, Google, Cloud translation. API so, we have a set of nodejs, npm modules that wrap a bunch of the api's in google cloud like our vision API translation. Api and more, and this just lets you interact with it nicely. In very no js' friendly way and, you'll notice we don't give it any service, account we don't give it any API key it's again, by default automatically, authenticated, to our cloud project, here's. The request and the response object they talked about before so. If the user gives us a message in the query string will use it if not will default to HelloWorld if, they give us a language in the query string we'll use it and if, not will default to Spanish, here, we'll actually invoke the translation API return. Status 200, okay to the user and then send back the translation, and for. Dependencies, we just use the natural nodejs. Package, JSON, format, which is how you define dependencies, and again because we do out the build in the cloud, you only need to deploy these two files package JSON an index and we'll. Do the build for you in the cloud when, we deploy, so. You can see that it's done deploying let.
Me Show you a couple of commands so g-cloud functions. List this, will show me all of the cloud functions, that I have in my project, and here's, hola world that we just deployed so, I'll do G cloud functions. Describe. And we'll see some data about our function. So. You can see things like what version it was when it was deployed we, set the timeout which by default here, is 60 seconds, you. Can see that it uses 256. Meg's of RAM and you can configure how much RAM and memory is available to your function and that affects pricing, the more memory you allocate towards it the more expensive your function will be when, it runs but. For this case here is the URL that again was automatically, generated for us so, I'm gonna copy that and then let's curl it and see what it does again. By default we, were looking for hello world and I'll translate it to Spanish so, let's see what happens. Hello. World hola mundo so that's great so now let's actually do a different message let's give it the message of hello, and I'm gonna do some manual URL, encoding here hello, friends, and then the language will, be let's. Say French. Motorola. Me and. What. About Japanese. I don't, speak Japanese so I hope that's right German. So just like that we created a global hello, world function. In, cloud functions. All. Right so those are HTTP, functions, so now let's take a look at background, functions. So. For. Example you can listen to events on Google Cloud storage. So finalize. Delete, archives, to finalize is when a file is finished writing so say on a new write our a, replacement or an update delete is obvious and, so forth how, you deploy them similar to an HTTP, function g-cloud, functions, deploy the name of your function you give it the resource which is the cloud storage bucket you want to listen for changes on and then what type of event so in this case we want to listen for a finalized, event. Let's. Look at an example of this in action, so.
Cloud Storage I'm gonna upload a, picture of an adorable baby raccoon, I love, raccoons so I tried to find the cutest possible, image and this is that so. When I upload this image it's, going to trigger a Google cloud function, to take some action, in this, case I'm gonna submit it to the Google cloud vision, API to. Do some analysis, here. The vision API will tell us what is this a photo of and you, can see here that it's 99%, confident, that it's a raccoon but a hundred percent confident, that it's adorable and. Here's. Where it architecture might look a little bit different, in a cloud functions, world versus, a traditional application. So. If you wanted to do some additional, processing besides, just, run and call the vision API we. Might typically in the same cloud function call the vision API and then, take our next step so for example if, we wanted to create multiple, versions, of our image so a small, version for a phone or a large version for desktop, medium, for tablet etc we, could do it in the same cloud function, either. Linearly, or maybe, potentially, create, different threads one to do the vision API one, to do the thumbnailing. But. In this in our world we actually can create a totally, separate cloud function that, triggers off the same file event. Independently. Creating, those thumbnails so this is sort of the microservices perspective, where these two functions are totally, unrelated they don't know anything about each other you can deploy them differently you can scale them differently. Without. Having to intermingle that code, so. Let's take a look at that, same demo in action. So. I have here my cloud storage, bucket which, is currently empty so I need to upload a photo, and I figure the best thing to do is clearly, to take a selfie in front of you so, I'm gonna take a live photo let me get down here and pose. And we'll see what the vision API has, to say. All. Right let. Me drag this into cloud functions. You. Can see it doesn't upload sorry, drag. This into cloud storage, so. There's our photo so, that was the in bucket so now we're gonna go down to, the out bucket, so, we're gonna see what does. The vision, API have to say and was it able to create a thumbnail so. Thumbnail, here yes we have a tiny version of our thumbnail but, let's look and see what. The. Vision API had, to say make. This a little bigger so you can see, so. From my photo it said finger. Chin, hand, thumb, professional. Well that's a little questionable sign. Language I guess maybe because I was doing thumbs up and facial, hair and if, you're curious about the vision API it also has these confidence, scores so it's 89% confident, that I have a finger well that's good to know. All. Right what did the code look like it's a little bit different than, the HTTP. Function which had a request and a response object. The parameters. Because. These are our background, or asynchronous, functions we can't directly respond to it instead, we get an event object and the, event object has two properties, one is called context. Which, describes, what, kind of event were responding, to is it, a cloud storage event, is it a pubsub, event is it a firebase event and so forth and the. Data event describes, the data specific. To this event so, in this case because, something is happening in cloud storage it's, a file object and we have different properties like file name well bucket it came from and so forth here. We'll submit it to the cloud vision API to. Get back response, so it can give me things like thumb. Or sign, language or, professional. And. Now I'm gonna talk about another source of events for something called firebase I won't, dig too deep into firebase, because it's a huge topic and it does a ton of great stuff but, if you don't know firebase, it's a cross-platform, set. Of tools for building applications, mobile, web, and more and there, are SDKs for Android. For iOS for. Web there's, a unity plugin for a CTR plugin for unity games and a C++ plugin for embedded devices and there, are a ton of tools in firebase everything, from the brand-new, fire store database, to, the classic, real-time database, authentication. Events there's, a static file hosting. Messaging. Remote. Cloud. Test, lab full of Android, devices in the sky and so much more and you. Can actually trigger cloud functions, based, on several of these, events, so for example the, cloud, fire store database you. Can watch certain, locations. In the fire store database and run, a cloud function automatically, when something happens a write event an insert event delete and so forth so, you could say a new value has been written to the database let. Me take some action, like data, validation, make sure it's the right format or maybe I want to replicate this value to another database maybe some analytics.
Database That have some data warehouse tool, you. Can also plug into firebase, authentication events, Google. Analytics events if for firebase if you don't know Google Analytics or firebase think the, traditional Google Analytics that you maybe be used on websites but, for your mobile devices so you can be tracking sort, of events. As your user moves, through your mobile app and when, certain events trigger you can have a cloud function, run in the cloud in response to that pretty, cool. How. Do you deploy cloud functions, you. Can deploy from the command line like I did earlier using the G cloud command, line tool you, can write. It directly in your browser if you want this always kind of terrifies me but if that's into you if you're into that super easy just browse there in the Google Cloud console. UI and you can edit your code directly. And you, also could just interact with the cloud functions, API directly maybe, you have a CI CD, system and you want to automate. It as part of your workflow whatever use case is the, API can support, it and. We. Also, have a local emulator. Now, this is nice because you can actually use the tools you're already used to for writing code and, set. Breakpoints and, watch points so you can watch local values step, in and out of functions all the nice normal, debugging. Tools you're used to using, you can do through the local emulator to make sure your function is in good shape before, you deploy it up to the cloud. So. What are some use cases that we're seeing people use cloud functions for things. Like web hooks which I talked about integrating, with third-party services, so when, commit happens and your github repo it could invoke your cloud function, when, your. Temperature, goes up in your house it could invoke a temp cloud function there was some smart device API. Endpoints, whether you want to create an API for your personal, application, or your internal application, or if, you want to create a public API for, external. Developers, and users to consume you can do that with cloud functions. Lightweight. Data transformation. So, we sort of did that with the photo upload it. Takes some image some binary, data does, some processing maybe writes it to a database but. Also think about a use case like uploading, a CSV file, to cloud storage your, cloud function could run process. The data in the CSV file clean, it up munge it put it in whatever format you want and then maybe write it to a real database so like cloud fire store or Google. Bigquery and. Then. IOT, devices cloud. Pub/sub is our messaging, asynchronous, messaging system that, can scale up to thousands, and millions of requests, at a time so, if you have a bunch of IOT devices they're all streaming, their data temperature. Location, whatever into pub/sub you, can have cloud functions, run in response, to, those messages as they come in in fact, we have a whole set of tools called, cloud IOT, core that, addresses, this so if IOT is interesting to you check out cloud IOT core on Google cloud platform and. Finally. I'm a developer I've written applications, my whole career so I tend to think of how can I use cloud functions, to write and build apps but, it's also great in sort of a DevOps context. If you have infrastructure. That you want to manage you can do it with cloud functions, because, you can have cloud functions, watch, your. Logs, of your Google cloud platform project. And it, can do things like oh I saw this app crash. 10, times on this, server I'm just gonna automatically, reboot the server or oops. I saw a delete event for my server my, database server that's, no good let me notify someone or maybe, a user was added, removed from, some admin pool so take some action maybe you let someone knows that a human can double check it so, in a way you can use your server list tech to manage your servers your traditional infrastructure pretty, cool.
How. Our car is how, our cloud functions being used today here, are some actual customers, so companies like meetup smart parking home away and Turner so. Our parking has a really interesting use case they're building a platform for smart cities which. Means they do things like track, how many cars, are moving, downtown and, change the way the traffic lights work based on those traffic patterns and they, use cloud functions dozens, and dozens of, cloud functions, all these, independent little micro services to handle that and, cloud, functions actually is nice for their use case because they've really, uneven. And spiky, traffic patterns so think about the use case where in rush-hour, downtown, you have a bunch of cars all moving around but, maybe a three o'clock in the morning it's completely, quiet what. Cloud functions will scale directly. To the workload and the use case or the usage of those. Devices. Turner. Also for example uses, it for, a, health. Check monitoring. For a lot of their brands on their online brands, things like make sure your uptime is good and it's performing, well and. Then of course we've got companies doing a lot of work, on business. Intelligence and analytics. At. BOTS and also similar to like, Google home and actions on Google. So. Finally I want to wrap up with how. Do you go beyond the hello world what are some of the tools available and, strategies, for, creating more, complicated. Apps and specifically. I want to talk about how do you chain, multiple, functions, together and how. Do you handle monitoring, and error reporting which is really important when you want a production, grade system. So. Let's talk a little bit more detail about cloud pub/sub as I, alluded to cloud pub/sub is our fully managed. Asynchronous. Message, delivery system and it lets you connect different. Applications, together using, messaging it. Works over HTTP, and G RPC, and just think of it as a way to throw, a bunch of JSON at. A service in the sky and endpoint that is able to absorb and request, and. Send out. And, deliver those messages based, on topics. So. Let's look at this in action so. Say you have a system for onboarding, new employees, the, first thing that happens is a employee, record, is written to the database in this case let's say Cloud firestore, so. Now you want to take some action I want to off of, that insert. The user into say our HR, system, now. Say that's a dependency. On a next, step in the chain so, we can't just automatically, move, on with the next step we need to wait until the user is finished, being, inserted into the HR system before. We can do the next step so. We could just go. On and do additional processing, in this same cloud function, or you, can wait once the user is inserted, in our cloud function we, can publish a message, a JSON, message to cloud pub/sub saying. Okay we've created a new user our topic is new user here's the relevant information about, the user and now whatever other steps in the chain that, need to take action, based on that can, spring into action because. Pub/sub. Will deliver it there's. The natural integration, with cloud functions, which will automatically, invoke if you have it listening for example on the new user topic, and. It could do something like insert. The users badge ID into the the badge system for the building, but. Because pub/sub, natively, talks HTTP. Integrating. With third other third party services. Is pretty easy so say you have an IT help desk system that, speaks HTTP, it, can actually directly, subscribe, to pub/sub on and, receive that same message on the same topic and take, another action so for example, create, a ticket for someone to go set up the user's desktop in this. Way pub/sub, lets you create really complex and really flexible, workflows. As you send messages throughout, your system. And. Then. Finally I want to talk about monitoring, and error reporting so. Again these are crucial pieces for, building a production grade, application.
We. Have a tool in Google cloud called stackdriver, and there's a tool called stack driver logging, and this basically contains all of your logging across, all of the tools in Google cloud platform and, to, take advantage of it you don't have to do anything it's, automatically, baked in with cloud functions, it, will capture all of your console, log messages, and if, you're not in nodejs to just, know that console.log. Is your, standard print, line print out to the console, it, will map log messages, and error messages, to the appropriate, severity and, stackdriver. Logging, is really powerful you can do things like text searching, drilling. And setting up filters I'll show you a screen shot in a second, and there's. Also stackdriver airing, error. Structure. Error reporting which will, automatically. Spelunk, those log messages to surface errors to you and. Because. Cloud functions, is build on precisely. How, long your function runs and how, much memory you've allocated to it we of course capture. Usage metrics on how long as your functions are running and how much memory they're using. So. Here's a screenshot of stackdriver logging, and you can see there's a whole bunch of log messages, from our cloud function, so. You can do free text searching like I said or filtering you also can drop. Into one specific. Request. ID and see all the logs for, one specific invocation. Of the cloud function, and this is useful because if you have a bunch of cloud, functions identical, cloud functions running at the same time and you maybe have an error in one specific invocation. This lets you group all of those into one to further aid in debugging. And. In, stackdriver error reporting, so, this will coalesce, essentially. With, some intelligence errors. That are happening in your educator, application, so, you don't want to get an email or an alert every single, time the same error happens you want to gets a one error email, that says this, error has happened ten times today or some kind of roll up so, this has some intelligence, built in and again is all automatic out-of-the-box, it's, all turned on for you you just have to start using it. So. A couple of things to consider before, using cloud functions. One. Is, if, I, have an existing, application can. I just move it to cloud functions, and the, answer is usually, not and that's because cloud functions, are designed to be ephemeral remember. These are little functions that spring, into life they, run for a little bit and then they die off. Traditionally. When you write applications we assume that once the app starts it's just it's up and running until. We, shut the server down but. These are springing into life dying, off all time so we have to do things like manage, our state in a. Cloud service, that is designed, to manage state so something like a cloud sequel database or cloud firestore. Moreover. We, can't make assumptions that anything, we do in one. Invocation, of the function is still, happening the next time so for example if I write a file to disk the. Next time I function invokes, that file may or may not be there it just depends, on whether, it happens to use the, same, particular instance of your function so. You can do things like cheats with some optimizations, but you have to write it to assume that, it's stateless an ephemeral. Long-running. Compute tasks so if you have to do things more, than nine minutes there's a nine minute at maximum currently on cloud functions. Running. It directly, in college functions is probably not a great fit instead, what you want to do is run your long-running, work on something, like kubernetes, or Google compute engine which are our virtual machines and use. Your, cloud functions, to do the orchestration, so things like create. A send a message to pub/sub to kick off the process handle. The. Finalization. Pub/sub, message when it's done doing the processing and just use cloud functions for general orchestration. Of your workload if they're long-running and, then. Guaranteed, response times so, one. Of the advantages, of cloud functions, is that it only builds, when, you're, actually running. Your code but. The downside to that is because it's put everything to sleep that next, request that comes in may, take a bit to sort of spin up some resources and it's not long maybe like think of a second or two and for, most use cases that's, totally, fine but, if you need likes millisecond.
Type Response, that. Is not something we can really guarantee with cloud functions every once in a while you'll hit this what we call cold start. We. Have a whole set of cerberus, tools on Google cloud I'm not going to go into these now, because we're about out of time but, if you go to cloud google.com, slash server list you can check these out just. A, quick note about pricing, before you wrap up I alluded. To it earlier pricing, is essentially, based on a few factors including, how many times your functions are invoked how long they run for how, much remember you've given them and what your outbound networking, traffic is but. The important thing is you get two million, in vacations, every month, for, free with, our free tier so it's a great way to not only try out Cod functions and see, if it's a good fit but you can actually start building real applications. To million a month is a lot. Here. Are some resources if you want to get started today cloud. Google.com. Slash, functions, we actually just launched, the redesign of this website just, a few minutes ago so, be sure and check it out you'll see my smiling face in a video there we've. Got a bunch of new documentation, tutorials. And. Then, also join us on Stack Overflow and github to get started. If. You have questions in the future you can follow me on Twitter at bressant, MCG but, we actually have been taking live questions during, the webinar so, give. Us less than one. Minute and I, will be right back to, answer your questions, stay, tuned. You. All. Right so we have a few questions that we've taken during the webinar the. First one here is there, a serverless, option if I have already have an existing, application so. There are a few perches, you can do one, as. We kind of talked through the microservices, example, you, can identify parts, of your existing, application, that might make sense to extract, and put, into a cloud function. This. Requires some rework, and some rethinking of your application, but, it's a strategy that a lot of companies and a lot of projects are doing however. If you want to try and just lift, and shift an existing, application that you have we, have a product called Google App, Engine standard, and this is a server list platform, as-a-service so.
Phishing. And more, and thanks, again for staying tuned. You.