The application is deployed and now you'd like to shift to a VCS style workflow. The new application will be added to a GitHub repo, and then the workspace will be hooked in. The actual deployment shouldn't change at all,
- Move application code to a VCS repository
- Migrate to the VCS workflow
- Use the VCS workflow to deploy an update
- Show how the CLI plan still works
You will need a free GitHub.com account for this exercise. We recommend using a personal account. You can sign up or login in to an existing account at https://github.com/
Login to github and create a new repository by navigating to https://github.com/new.
Use the following settings for the code repository
- Name: "diamond_dogs"
- Description: Diamond Dogs Wicked Application
- Private repo
- Check "Initialize this repository with a README"
- Add
.gitignore
of type "Terraform"
Once created, clone the repository to your local machine.
git clone https://github.com/<YOUR_GIT_HUB_ACCOUNT>/diamond_dogs.git
cd diamond_dogs
In your repository copy the following configuration files to the diamond_dogs
directory: variables.tf
, main.tf
, outputs.tf
, terraform.auto.tfvars
, along with the files
subdirectory and its contents.
Commit the changes in GitHub.
git add .
git commit -m "terraform code update for diamond_dogs app"
git push
- Login to github in one browser tab.
- Login to Terraform Cloud in another browser tab.
- Within Terraform Cloud navigate to the settings page
- Click "VCS Providers" link:
For full instructions follow the Configuring GitHub Access section of the Terraform Cloud documentation to connect your GitHub account to your Terraform Organization.
Configuring GitHub Access - https://www.terraform.io/docs/cloud/vcs/github.html
Note: The process involves several back and forth changes and is documented well in the link.
- Navigate to https://app.terraform.io and click the diamond dogs workspace
- Select Settings >> Version Control
- Click the "Connect to version control"
- Select the Version Control workflow
- Click the VCS Connection in the "Source" section.
- Verify you can see repositories and select the
diamond_dogs
github repository. - Select Update VCS Settings
- Validate that a new Terraform run will occur on the workspace. Confirm and Apply the Terraform run.
In the diamond_dogs
github repository, create a development
branch from the main
branch. Also create a staging
branch from the main
branch.
Update your Terraform Cloud diamonddogs-aws-useast1-dev
workspace to point to your development
branch.
Create a new TFC workspace named diamonddogs-aws-useast1-prod
that is tied to the diamond_dogs
github repo by choosing a VCS Control workflow. Set the branch and enable speculative execution.
Repeat this step for the staging environment by creating a new TFC workspace named diamonddogs-aws-useast1-staging
that is tied to the diamond_dogs
github repo and specifying the staging
branch.
Each workspace is setup with it's respected variable for environment and watching that respected branch of our diamond_dogs project.
- In the
Settings
tab of your Terraform Cloud Organization go toVariable sets
. - Select the
Create Variable set
button - Give your variable set a name -
AWS Credentials
and description. - Apply the variable set to all diamond dogs workspaces.
- You can add environment variables for the AWS Credentials
- Add a Environment variable named
AWS_ACCESS_KEY_ID
with your AWS Access Key - Add a second Environment variable named
AWS_SECRET_ACCESS_KEY
with your AWS Secret Key. Be sure to mark this variable as sensitive.
Now you can use these AWS credentials on any new workspace you create in Terraform Cloud by adding the variable set to the workspace. While storing cloud credentials is a common use of variable sets, they are not limited to credentials. Variable sets can be utlized for any set of variables that you want to reuse accross workspaces within the organization.
Update the variables for each of the diamond dog Terraform Cloud workspaces for the environment
variable.
- Navigate to your Terraform Cloud workspaces in the UI.
- Once there, navigate to the
Variables
tab. - In the
Variables
tab, you can add variables related to the state file that was previously migrated. - To do this, first select the
+ Add variable
button - Let's add a Terraform variable named
environment
with a value ofdevelopment
Repeat these same steps for all diamond dogs workspaces specifying the correlating environment value for each. Validate that the application deploys with the respective settings by triggering a Manual apply >> Actions >> Start new plan
We will be using GitHub to promote a change to our app through Development into Stage and then into Production.
Let's look how a change promotion would look in this configuration we outlined. We are going to start in our "Development" environment and move, or promote, that change to our other environments. Make sure you have both GitHub and Terraform Cloud web pages up. We will start in GitHub.
Let's look how a change promotion would look in this configuration we outlined. We are going to start in our "Development" environment and move, or promote, that change to our other environments. Make sure you have both GitHub and Terraform Cloud web pages up. We will start in GitHub.
- Select the
diamond_dogs
project, if you are not already there. - From the branches list (pulldown menu that says main), choose
development
- Select the
Files
folder and thedeploy_app.sh
. - Click on the blue
Edit
button. We will just use the GitHub web editor for simplicity. - On line 14, add a message after the "Welcome" message.
The Diamond Dogs have struck again.
- Add a "Commit message" below that describes your change.
- When you're done click on
Commit Changes
- Navigate back out to Terraform Cloud. You should see that your diamond dogs development workspace is running.
- Approve the change by viewing the run.
- Refresh your Diamond Dogs Development web page to confirm that your updates are now displayed.
Now that we have our change in our development environment and we did our testing to confirm our application is functional, let's promote this change to our stage environment.
- Navigate back to GitHub. You should still be on the development branch viewing the
deploy_app.sh
file that we edited. Click on thePull requests
option. We will be merging our changes we made in the development branch to our staging branch. - Select the
Compare & pull request
selectChange branches
. Our source branch will default to "development". Change the target branch tostaging
and selectCreate pull request
- Update the Title to
Promote to Stage
and add a short description of your change. - For "Assignee" select
Assign to me
. We currently do not have users and groups setup in our environment but in a real world scenario we can put security controls around this approval process. - On the bottom of the page you can view what files and lines in those files are different between the development and stage branches. These are the changes that will be merged into our target branch.
- Select
Create pull request
. We now have an opened a pull request. In our lab, approvals are optional but we could require multiple approvers before any changes get applied. We could deny the request and put a comment with details regarding why we denied it. - Click on the
Show all checks
next to the green check-mark. - Open the
Details
link in a new tab. As a pull request reviewer, you can use this to review the Terraform plan that was ran by the GitHub pipeline within the diamond dogs staging workspace.
We peer reviewed the changes everything looks good. Now go back to the tab we left open with our merge request and select the green Merge pull request
button and Confirm merge
.
Notice that another pipeline was started under where the merge button was. Right click on this new pipeline and open it in a new tab. You can use the external pipeline to link out to Terraform Cloud to review the apply. We could have also been watching the Terraform Cloud workspace list to see our workspaces auto apply from our merge request inside the diamond dogs staging workspace.
You can open the stage URL of our Diamond Dogs app to confirm that our changes have been added.
The process to promote our change into our production environment would be identical and the environments are isolated so changes in the development branch have no impact to the production / main branch.