In this hands-on lab, we will take an existing Terraform configuration and convert it to run the code using Terraform Cloud. We will use the CLI-driven method to deploy our resources to AWS with Terraform Cloud doing the bulk of the work.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Prepare the Environment
Log in to the lab instance.
Clone the lab repo to your lab machine:
git clone https://github.com/ACloudGuru-Resources/content-introduction-terraform-cloud-lab.git
Create the
terraform-guru
organization.Select CLI-driven workflow.
Create the
terraform-cloud-guru
workspace.Create a variable set by going to your workspace settings and then Variable sets. Click Create variable set.
Use
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
environment variables.In the AWS Management Console, create an access key for the
cloud_user
and secret access key, and use those values for the variable set.In the user settings, create a token for the CLI to use with Terraform Cloud called
terraform-cloud
. You must copy the key, runterraform login
in your terminal, and follow the prompts.
- Migrate the Existing Terraform Project into Terraform Cloud
- Modify the
main.tf
file, and add the Terraform Cloud config. - Add the Terraform variables in Terraform Cloud for your workspace for the
ami
,instance_count
,instance_type
, andsubnet
Note: You will need to look this up from the console or CLI. All other variables will be in the
variables.tf
file. - Modify the variables so they use the Terraform Cloud variables.
- Modify the
- Deploy the EC2 Instances with Terraform Cloud
- Initialize your working directory.
- Validate your code.
- Apply your configuration, and view it in your terminal and Terraform Cloud Runs section.
- Check the AWS Management Console to verify the instances were created.
- Create a Terraform Module from Existing Code
- Create a GitHub repo called
terraform-as-ec2-psacg
. - Add the Terraform Cloud application, and configure it.
- Add the module configuration to your repo called
main.tf
. - Add a tag to the repo of
1.0.0
. - Add the module to Terraform Cloud Registry.
- Create a GitHub repo called
- Modify the Terraform Configuration to use the Module and Redeploy
- Edit the main Terraform configuration to use the module.
- Initialize the Terraform working directory.
- Validate the syntax.
- Apply your new configuration.
- Confirm the changes.