# Cloud functions
---
## What are "Cloud functions"?
In terms of most cloud service providers, cloud functions is a serverless platform for building event-based microservices.
It's based around a concept called Function as a Service (FaaS).
----
## How serverless architecture works:
- Developers don't have to manage and worry about the servers on which their code will run.
- Once a specific trigger is invoked, the cloud provider will either execute a function on a already running server, or will a start a new server instance for this purpose.
----

----
## Serverless vs Container Architecture:
1. Both allow developers to deploy application code by abstracing away the host enviroment.
2. Containers require maintainance such as managing their system settings and dependencies.
----
## Serverless vs Container Architecture:
3. Serverless applications scale automatically, while container architectures require orchestration, by a platform like Kuberenetes.
4. Serverless functions are more suited to really small event driven microservices, than bigger applications.
---
## Categories of cloud functions:
In terms of GCP, "Cloud Functions" can be divided into 2 kinds of functions:
- Event driven Functions (Background functions and CloudEvent functions)
- HTTP Functions
---
## Event driven functions:
Background functions are invoked in response to an event, such as a message on a [Pub/Sub](https://cloud.google.com/pubsub/docs/overview) topic, a change in a Cloud Storage bucket, or a Firebase event.
Cloud functions are essentialy the same, with one big difference, which is that they use a [CloudEvents](https://cloudevents.io/) event format.
---
## HTTP Functions:
These kinds of functions are invoked by use of standard HTTP requests using POST, PUT, GET or DELETE, etc. HTTP methods.
They can be used in a lot of scenarios, by merging multiple API's into a bigger service.
---
## Hello world
```python=
from flask import escape
def hello_http(request):
request_json = request.get_json(silent=True)
request_args = request.args
if request_json and 'name' in request_json:
name = request_json['name']
elif request_args and 'name' in request_args:
name = request_args['name']
else:
name = 'World'
return 'Hello {}!'.format(escape(name))
```
---
## Exemplary use cases of Cloud Functions.
1. Automatic detection and blurring of offensive or violent pictures uploaded to Cloud Storage.

----
2. Creating a messenger chatbot, utilisng GCP API's.

----
3. Creating Slack messages when a commit is pushed to GitHub.

---
## Benefits of cloud functions:
1. Use of cloud functions is billed per-invocation basis (in case of GCP, it's the time that function has run).
2. Multiple instances of functions are created automatically, within the set limits.
3. No need to manage any server architecture what so ever.
---
## Limits of cloud functions:
1. Each "function" needs to have one function defined as it's one and only entry point.
2. Each cloud function has a designated "timeout". This the maximum time in which the function can be executed, after that, it is terminated.
3. Developers are limited to a smaller selection of programming languages, that the cloud provider is providing.
---
## Other cloud providers solutions:


----
| Service: | Languages available by default |
| -------- | ----------------------------------------------- |
| AWS Lambda | C# Go Java Node.js PowerShell Python Ruby |
| Azure Functions | C# F# Java Node.js PowerShell Python TypeScript |
| GCP Cloud Functions | Go Java Node.js Python Ruby PHP |
----
| Service: | Custom runtimes |
| -------------------- | ----------------------------------------------------- |
| AWS Lambda | using custom deployment packages or AWS Lambda Layers |
| Azure Functions | using Azure Functions custom handlers |
| GCP Cloud Functions | using custom Docker images |
----
| Service: | Maximum Timeout |
| -------------------- | -------------------------------------- |
| AWS Lambda | 15 minutes |
| Azure Functions | 5 minutes standard, 30 minutes premium |
| GCP Cloud Functions | 9 minutes |
---
## Useful courses utilisng Cloud Functions:
- [Creating an app in Google Assistant with GCF](https://www.qwiklabs.com/focuses/3634?catalog_rank=%7B%22rank%22%3A2%2C%22num_filters%22%3A0%2C%22has_search%22%3Atrue%7D&parent=catalog&search_id=14636154)
- [Whole course on developing Google Assitant apps](https://www.qwiklabs.com/quests/61?catalog_rank=%7B%22rank%22%3A2%2C%22num_filters%22%3A0%2C%22has_search%22%3Atrue%7D&search_id=14636084)
- [Cloud Functions - Qwik Start](https://www.qwiklabs.com/focuses/1763?catalog_rank=%7B%22rank%22%3A1%2C%22num_filters%22%3A0%2C%22has_search%22%3Atrue%7D&parent=catalog&search_id=14636118)
- [Using Cloud Logging with IoT Core Devices ](https://www.qwiklabs.com/focuses/2768?parent=catalog)
{"metaMigratedAt":"2023-06-16T14:57:40.962Z","metaMigratedFrom":"YAML","title":"Cloud Functions and Serverless Architecture","breaks":true,"slideOptions":"{\"theme\":\"sky\",\"keyboard\":true,\"transition\":\"slide\"}","contributors":"[{\"id\":\"b089228c-907a-4224-a4f7-bcb8703a7e29\",\"add\":7363,\"del\":1818}]"}