Sunday, September 17, 2023

What is Terraform Cloud and How to run local cli work flow using Terraform cloud

Description: Here I have explained, What is Terraform Cloud and How we can run the workflow with terraform cloud

What is Terraform Cloud?

Terraform Cloud builds on these features by managing Terraform runs in a consistent and reliable environment instead of on your local machine. It securely stores state and secret data and can connect to version control systems so that you can develop your infrastructure using a workflow similar to application development.

The Terraform Cloud UI provides a detailed view of the resources managed by a Terraform project and gives enhanced visibility into each Terraform operation.

Workflows: Terraform cloud supports three types of workflows for Terraform runs

  • The CLI-driven workflow, which uses Terraform standard CLI tools to execute runs in Terraform Cloud
  • The UI/Version Control System(VCS)-driven workflow, in which changes pushed to version control repositories trigger runs in the associated workspace
  • The API-driven workflow, allows you to create tooling to interact with the Terraform Cloud API programmatically


Signup with Terraform Cloud: To signup with terraform cloud use this URL


First Workflow: The CLI-driven workflow, which uses Terraform standard CLI tools to execute runs in Terraform Cloud

To run the workflow in terraform first need to generate API token. To generate API token login with terraform cloud and navigate to user setting and create token


Once you click on Create an API token it will popup for token information as follow. Fill all the details and create token 


Once you click on Generate Token it will create the token keep token on safe place never shared with someone. Also it will not displayed again so if you forgot to copied you need to create new one.

Workspace: After creating the API token now create workspace in terraform cloud to run the projhect from the local cli

To create the workspace navigate to project & workspace and new -->  workspace


Once you click on workspace it will pop-up for select the workflow, in this example I am selecting the cli-driven workflow



After click on CLI workflow filled the required details like name of worflow and select the default-project. 

Note: In this example I used default-project you can create new project and use same



Once you click on create it will shows the code which we need to add in our existing terraform tf file 





After generating the API token now need to login using token in local machine from where you want to run the terraform project



install_nginx.sh file for nginx installation in user_data



  In terminal run the command terraform login to login with the terraform cloud





Once you put input it will ask for the token so paste the token which we have created earlier. You will get user details if token is valid as follow







Now run the terraform commands to run the project with the current directory

$ terraform init



$ terraform plan



When your the terraform plan it also show progress in terraform cloud as well



Once the plan completed you can get the result in terraform cloud as well



After planning now I am going to run the terraform apply




Once you pass Yes, it will run and apply the work flow 









So once  the runs completed it shows as follow


You can see the result like approved using UI or API


So as per our terraform project one EC2 instance created with the nginx role 



Also we can get result by browse the url 




Finally I am going to destroy the infrastructure using terraform destroy 





Confirm and apply to destroy the instance




Once the instance destroyed you will get the output as follow







No comments:

Post a Comment