Building Pipelines with Terraform Cloud

Cloud Technology

Having a robust and effective CI/CD pipeline is the key to shorter sprints and effective iterations of cloud-native applications.

Having a robust and effective CI/CD pipeline is the key to shorter sprints and effective iterations of cloud-native applications. In order to push updates regularly and successfully, you have to incorporate a number of things into the pipeline, including testing and security.

Terraform is used to build, maintain, and update cloud infrastructure. It runs from your desktop and communicates directly with cloud service providers like AWS.

While Terraform offers all the features you need to do infrastructure and policy as code, it is far from challenge-free. The most prominent challenge of them all is when collaborative efforts are needed in the process because manual changes are still required.

From Terraform Local to Terraform Cloud

The introduction of Terraform Cloud was meant to solve the challenges mentioned earlier, particularly when provisioning needs to be done by a team rather than a single specialist. A lot of developers use Terraform to manage infrastructure, so migrating to Terraform Cloud is not going to be that difficult.

In fact, HashiCorp makes migrating to Terraform Cloud easy for local Terraform users. You start by creating a Terraform Cloud user account and then gathering state data from your local Terraform installation. You can use the Terraform login command to connect the two.

Your local Terraform needs to be stopped before you continue with the rest of the migration. Make sure all runs are completed and that CI jobs don’t run once the migration is started. Get everyone in the team onboard with migrating to Terraform Cloud too.

You can then prepare the working directory by retrieving the terraform.tfstate you collected earlier and running terraform init. You need to make some changes to the configuration, particularly the backend block of the config file.

With the config file updated, run terraform init to start the migration. You will be prompted to copy the state to a remote backend (which is Terraform Cloud). Answer YES and the process will run on its own. All you need to do next is configure Terraform Cloud workspace to your specific requirements.

That’s it! At this point, you can fully utilize Terraform Cloud and its features.

Features and Benefits of Terraform Cloud

That brings us to our next point: the true benefits of migrating to Terraform Cloud. With Terraform running in the cloud, the biggest benefit you will appreciate immediately is the fact that you can now have multiple team members working on Terraform.

In fact, Terraform Cloud puts emphasis on this team-oriented remote workflow. Team members can share responsibilities and awareness on how cloud resources are provisioned and how policies are enforced. Version control and remote state management are among the features you can use out of the box.

This team-oriented workflow is further supported by remote Terraform execution, which is a native feature of Terraform Cloud. Remote operations or executions run on their own virtual machines; the VMs are designed as disposable to limit overhead.

And you can test and check runs against Sentinel policies with absolute consistency. 

New resource provisioning runs can be much smoother now that errors and potential mistakes can be avoided in a consistent way. Since everyone in the team can also monitor remote operations, there is no single point of failure to worry about.

Speaking of version control, the integration of Version Control System (VCS) with Terraform Cloud is on another level. VCS repositories can have specific branches and subdirectories, and they can be linked to a particular Terraform configuration. Changes made to the VCS repository are reflected in the Terraform runs.

VCS also allows Terraform Cloud to run speculative plans with changes made to the repository, and then post results as pull request checks. Everyone in the team can then review the pull request checks and make the recommended changes if needed. These features are limited to supported VCS only but making a switch to one is recommended considering the benefits you can get in return.

On top of everything, Terraform Cloud is still an easy tool to use with its CLI. Terraform Cloud can run in the cloud and locally just as consistently, allowing it to offer the best of both worlds. The Enterprise version of Terraform Cloud even supports large, multi-team management.

What’s interesting is how Terraform Cloud’s CLI can use the current working directory⁠—in a local setup⁠—with the workspace variables stored in the cloud. Checks can be performed before policies or codes are pushed to the remote repository. This is huge for team efficiencies since developers can be more effective on their own while working seamlessly as a team.

Workspaces for Infrastructure Management

The key to Terraform Cloud’s robustness is how it uses workspaces. The local version of Terraform uses a persistent working directory, which holds configuration and state data as well as variables. It is possible to use different working directories to work on different infrastructure groups, but the process is still tedious in some ways.

That is not the case with Terraform Cloud, which uses workspaces to perform the same task. Workspaces can have everything needed to manage the respective infrastructures and Terraform will use the workspace in context with every run it executes.

This includes storing state data, and that is a huge plus. No more mixing state data with the working directory it originated from. Workspaces always provide context and allow Terraform Cloud to match state data with the configuration file. To make it even better, workspaces can share remote operations, variables, and other information using terraform_remote_state.

Even when you are working locally most of the time, migrating to Terraform Cloud is still highly beneficial. The integration of VCS repositories and the use of workspaces are rewarding for teams of any sizes. One additional feature that makes migrating highly recommended is the Private Module Registry, which allows for reusable modules to be standardized and managed in the same meticulous way. A team-based permissions system completes the set and makes Terraform Cloud the perfect tool for building CI/CD pipelines.

Caylent provides a critical DevOps-as-a-Service function to high growth companies looking for expert support with Kubernetes, cloud security, cloud infrastructure, and CI/CD pipelines. Our managed and consulting services are a more cost-effective option than hiring in-house, and we scale as your team and company grow. Check out some of the use cases, learn how we work with clients, and read more about our DevOps-as-a-Service offering.

Cloud Technology
Mauricio Ashimine

Mauricio Ashimine

View Mauricio's articles

Learn more about the services mentioned

Caylent Services

Infrastructure & DevOps Modernization

Quickly establish an AWS presence that meets technical security framework guidance by establishing automated guardrails that ensure your environments remain compliant.

Accelerate your cloud native journey

Leveraging our deep experience and patterns

Get in touch

Related Blog Posts

re:Invent 2023 AI/ML Session Summaries

Get up to speed on all the GenAI, AI, and ML focused 300 and 400 level sessions from re:Invent 2023!

Cloud Technology
Artificial Intelligence & MLOps

re:Invent 2023 Storage Session Summaries

Get up to speed on all the storage focused 300 and 400 level sessions from re:Invent 2023!

Cloud Technology

re:Invent 2023 Serverless Session Summaries

Get up to speed on all the serverless focused 300 and 400 level sessions from re:Invent 2023!

Cloud Technology