At Cycloid, we developed Inframap: a command line interface (CLI) used to generate graphs from Terraform HCL or TFState. Behind this CLI, there is a library: why not use it to provide a service? Like an endpoint where you send your TFState and it returns a graph? Let’s try!
One of Google Cloud Platform's most famous serverless features is Google Cloud Functions but it has some cons: you depend on the supported runtimes, their versions, and you can’t serve “complex” applications with specific system dependencies.
Let’s try Google Cloud Run, an alternative allowing you to provide serverless features through a Docker container.
In terms of requirements, you only need a Google Cloud Platform account and a Docker setup somewhere - don’t forget to configure your Google Container Registry access.
$ gcloud auth configure-docker
Now, let’s write some code: we want a simple HTTP server, handling POST requests on the root. Content of the request (TFState) will be provided to InfraMap’s lib in order to generate a graph from it.
The code here is not really interesting but you can check it out on GitHub.
Same for the Dockerfile, nothing fancy: a multistage build with a cache strategy for the dependencies but now we start realizing what's so cool about Google Cloud Run: you deploy a Docker image so you're in control of the environment.
Now, let’s build and push our Docker image to our Google Container Registry.