Sunday, September 17, 2023

Github integration with Terraform cloud to driven workflow using VCS

Description: Here I have explained, How to integrated GitHub with Terraform cloud to driven the workflow using VCS.  When you integrate the GitHub with terraform cloud your terraform project tf files saved in GitHub repository.  Whenever you make any changes in GitHub files workflow run automatically.

GitHub Repository: 

The first step, we setup blank public repository in GitHub. To create the blank repository navigate GitHub and create blank repository with Readme file 




Clone the newly created repository in local folder using git clone command 

git clone https://github.com/harpal1990/testrepo-tfcloud.git



After clone the repository I am adding  terraform project files in same directory and upload it to repository. So I have uploaded main.tf and install_nginx.sh file as part of my terraform project



Now to push it to repository we need git username and password. So generate git token from the GitHub. To generate GitHub token navigate to user setting in GitHub --> Developer Settings --> Personal access tokens --> Tokens (classic) --> Generate new token 






Once you generated the GitHub token make sure to keep in safe place. If it loss need to regenerate with new name

Once you add the additional files and push to origin branch it asked for password/token as follow




Now push the additional files to GitHub





Workspace: Now after filling all the details create new workspace and select version control workflow





Select GiHub as subversion control system and click on next




Once you click on GitHub it will popup for account authorization




Add all the required repositories for terraform cloud in our example I am using testrepo-tfcloud



After adding the repository you can select the valid repository and click on create workspace 






After creating workspace add variables as follow

After uploaded all files uploaded create the require variables in Terraform cloud. To add variable in terraform cloud navigate to workspace setting --> variable --> create variable set




Add variable -- Create Variable set



First variable add  for region as follow



Now add access key and secret key for authorization to AWS as follow


After applying all the variable set run the task from the workspace and confirm apply 



In EC2 you can find the instance in the list



Destruction and Deletion: To remove the instance and workspace follow the below steps

  • Goto -> Workspace -> Settings -> Destruction and Deletion
  • click on Queue Destroy Plan to delete the resources on cloud
  • Goto -> Workspac -> Runs -> Click on Confirm & Apply
  • Add Comment: Approved for Deletion




No comments:

Post a Comment