In the previous article (1/2), we have discussed the theory of DevOps and Infrastructure as Code. It’s now time to dig into the practical concepts of these topics and see the results of actual results from a CICD pipeline that runs Terraform and deploys the infrastructure for a Kubernetes cluster that can host a 5G networking solution (2/2).
The entire talk can be watched on the platform of the Xchange conference. It’s available for all Replyers and Reply’s customers.
First, in this practical article, we wanted to share the code used for the different Terraform demos. The <main.tf> files of each demo are shown also in the screenshots below.
Of course, we can do all these manually by typing a couple of commands on our local machine (assuming AWS credentials are set up). The commands we need to do this locally are:
- terraform init— initialises the terraform providers used in the TF code.
- terraform validate (optional) — validates the structure of Terraform codebase.
- terraform plan — as the name suggests it will plan what type of infrastructure services will be deployed without doing so. Thus, it gives a good idea of what we will have when typing the following command.
- terraform apply— this is the final step where the message of deploying the infrastructure will be “transmitted” to AWS. Remember Terraform is declarative code, which means whatever is listed will be deployed on AWS. Normally, we will be asked “if we are sure” about applying this code. If we cannot be asked then the flag “-auto-approve” will be required.
- terraform destroy — finally, the infrastructure is not needed anymore we can destroy it. Similarly, with the previous command, we can use “-auto-approve”.
Terraform Automation
Automation couldn’t be missing from one of our articles since we have talked about DevOps, CI/CD and other automation concepts recently in previous blogs. Below we can see how a Gitlab CI (CI/CD) pipeline looks like in the context of provisioning the infrastructure. The three steps are responsible for automatically building (init & validate), deploy and, when needed, destroy the infrastructure.
As with any code the tools we need to trigger automatically this pipeline is our IDE (we used IntelliJ), git and the setup on Gitlab CI (gitlab-ci.yml is attached below). Once we make a change and push the code to our repository the pipeline will be triggered and the steps will be executed.