Audience

The information contained in this page is intended for:

Scope

The information contained in this page offers a high level overview of an automation framework that allows the repeatable and reliable deployment of applications and the infrastructure to support them, to GCP. It serves only as a guide and is not a definitive solution or standard. Each project will have its own unique requirements, but the framework presented here can be used a starting point or as an inspiration for a real-life implementation.


Assumptions

The example project attached to this page makes certain assumptions that contribute to the opinionated nature and code structure of the framework. However, as mentioned above, this is only intended to be used as a starting point so adjustments can always be made. The assumptions made are as follows:

If any of the above assumptions are not true,. the framework can still be applied/used with modifications to address the gaps.


Pre-requisites

In order to implement the automation framework the following pre-requisites must be met:


Solution Overview

The Automation Framework is based on the parameterisation of all important input values for the build & deployment processes, in the form of environment variables. This approach allows for reliable repeatable builds & deployments across multiple environments, since the required input values are standardised and multiple manually maintained input variable files for each environment are avoided.

It is assumed that all builds & deployments will be executed in a Linux Operating System environment and as such, all relevant automation scripts are written for execution in a bash compatible execution environment. In addition to a global environment variables file, each module in the project has its own local environment variables file along with a build/deployment executable script. The name of the build script in each software component module is “run_deploy.sh” while the infra module has two (2) scripts to match the Terraform plan & apply actions, named “terraform_plan.sh” and “terraform_apply.sh” respectively. The software component build scripts as well as the terraform deployment scripts, accept a single parameter indicating the target environment. The valid options are:


The global environment variables file defines some globally applicable environment variables such as the GCP Project ID where all resources should be deployed, the environment that the deployment should target, the GCP region where resources should be deployed etc. The local environment variables file defines all values specific to that module, such as name of component, name of Cloud Secret Manager secrets it uses, name of container to build etc. The names of the global and local environment variable files are respectively:


The first action that both the software component build scripts and the infra deployment scripts will take, is load the global environment variables file, followed by the local environment variables file. This order is important because many of the local environment variable values may include the GCP Project ID or target environment. In some cases, configuration files will not support direct environment variable substitution. In these cases, template files are created to include the environment variable references where necessary, and before the build/deployment is executed, the actual configuration file is re-generated using the template and environment variable values loaded into memory at that time. These configuration files that are dynamically generated at run-time, are excluded from the source code repository, i.e. are not stored with the code, via exclusion directives in the project’s .gitignore file.


Project Repository Structure

The diagram below illustrates the structure of the sample project's repository.



Bootstrapping

The “bootstrap” process deploys infrastructure that is either necessary for the execution of the regular build and deployment steps, or infrastructure that should persist as-is between builds. For example, this includes the Cloud Storage bucket where the remote Terraform state is stored to ensure consistent infrastructure builds, as well as any static external IP addresses that are attached to external HTTPS Load Balancers etc. The bootstrap process can be executed by executing the script infra/bootstrap/bootstrap.sh. It expects only one (1) parameter, the name of the target environment as detailed above. The bootstrap script performs the following tasks:

  1. Load global and local environment variables
  2. Enable all required GCP APIs
  3. Create all required Cloud Storage buckets
  4. Create all other required persistent resources as needed


For all of the above tasks, the script will first check if the resource already exists and will attempt to create it only if it does not already exist. The script is intended to be executed once, before any components of the project are built or deployed. However, if additional resources that need to persist or should be independent of regular builds should be deployed such as a new Cloud Secret Manager Secret, then the updated script could be re-executed. Because of the way the script is structured, previously deployed resources by the script will not be affected.


Build & Deployment process

The IaC configuration files that define the GCP infrastructure that supports the service component(s), reference attributes of the software components such as container image names, component versions etc. For this reason, component builds must take place before the infrastructure deployment. The order of the software component builds does not matter, as long as all of them are completed before the infrastructure deployment. These can be triggered in the sample application by executing the following scripts:

Service software component build

./services/my-service/run_deploy.sh <target env>


Infrastructure deployment

./infra/terraform_plan.sh <target env>
./infra/terraform_apply.sh <target env>


Before executing a software component build of an infrastructure deployment, the person using the framework must authenticate themselves with GCP. The following two (2) commands will open a browser window where the user must login with their GCP-connected Google account credentials and accept the authorisation prompt to allow GCP access to their account. Once these steps have been successfully completed, the person will be able to execute the build and deployment.

gcloud auth login
gcloud auth application-default login


Resources

The below attachment includes all the relevant framework files for the simple deployment of a Cloud Run instance to GCP.

my-project.zip