Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/siddhant-k-code/run-cloud-functions-locally

Run Cloud Functions Locally or in Gitpod
https://github.com/siddhant-k-code/run-cloud-functions-locally

cloud-functions gitpod local serverless

Last synced: 2 months ago
JSON representation

Run Cloud Functions Locally or in Gitpod

Awesome Lists containing this project

README

        

# Run Cloud Functions Locally πŸŒ©οΈπŸš€

Welcome to the exciting realm of cloud functions, where the magic of serverless computing comes alive on your local machine! This README will guide you through the enchanting process of setting up and running cloud functions locally, ensuring a pristine, isolated development environment with the aid of [**Gitpod**](https://www.gitpod.io/). πŸ§™β€β™‚οΈβœ¨

## Premise πŸ—οΈ

For this magical journey, Python will serve as our trusty runtime language. To avoid any mystical contamination in our local environment, I've set everything up in a dedicated Gitpod workspace. It's like conjuring your very own wizard's tower for code experimentation! 🏰

Bypass the implementation steps by leaping straight into this repository in Gitpod! Click the button below to teleport there (it's pre-configured for convenience πŸ˜‰):

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Siddhant-K-code/run-cloud-functions-locally)

## Implementation πŸ› οΈ

First, let's enchant our `requirements.txt` file with a sprinkle of dependencies:

```txt
functions-framework==3.*
```

Now for the spell-castingβ€”the code! Here's a simple Python script that summons a cloud function:

```python
import functions_framework

@functions_framework.http
def main(request):
return f'Hello, {request.json.get("name")}\n'

if __name__ == "__main__":
main()
```

### Spell Details 🧐

- `@functions_framework.http`: This powerful spell instructs our script to gear up for responding to HTTP requests as a Cloud Function.
- It's crucial to always send a response back. Ignoring requests is a no-no in both the arcane and coding worldsβ€”it prevents errors and keeps the magical and digital flows balanced.

## Execution πŸš€

It's time to animate our cloud function! Open your terminal (ensure it's not engaged with other magical commands) and recite the following spell:

```sh
functions-framework --target=main
```

Open another terminal window (think of it as opening a portal to another dimension) and invoke your newly minted function:

```sh
curl -X POST -H "Content-Type: application/json" -d '{"name":"Siddhant"}' http://localhost:8080
```

If the cosmic energies align and your incantation is precise, you will be greeted with:

```sh
Hello, Siddhant
```

Run cloud functions locally or in Gitpod

## Epilogue 🌟

Remember, the `functions-framework` is capable of more than just HTTP functions; it can also initiate event-driven functions. Perhaps that's an adventure we'll embark upon in the future!

## References πŸ“š

For more spells and scholarly knowledge, consult these ancient tomes (or just click here):

- [Google Cloud Functions Framework Python](https://github.com/GoogleCloudPlatform/functions-framework-python)

Happy coding, and best wishes on your serverless quest! <3