Ansible-Terraform Workspace. Part 3
development environment with Ansible, Terraform, and lots of other applications in one docker image.
This is Part 3 of the publications about Ansible-Terraform workspace — a docker image, that contains a toolkit to work with Ansible and Terraform, including several browser-based applications to schedule jobs, visualize infrastructures, terraform plan visualization, dashboard of Ansible playbooks, and more.
Part 3 describes Terraform toolkit of Ansible-Terraform workspace, with examples and code snippets.
Terraform is an open-source infrastructure as a code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.
Ansible-Terraform workspace contains a small example Terraform project that creates a server on the Scaleway cloud
cd /home/examples/terraform-scaleway/ && terraform init
Set Scaleway credentials as environment variable in your workspace. Add the following lines to /home/abc/.zshrc:
export SCW_DEFAULT_PROJECT_ID=<YOUR_PROJECT_ID>
export SCW_ACCESS_KEY=<YOUR_ACCESS_KEY>
export SCW_SECRET_KEY=<YOUR_SECRET_KEY>
Restart terminal, and execute
cd /home/examples/terraform-scaleway/ && terraform plan
Create infrastructure with
cd /home/examples/terraform-scaleway/ && terraform apply
Terraform report
A small tool that produces several outputs from a terraform project and visualizes terraform plan as an interactive HTML page.
Terraform report can be generated from the small example terraform project, included in the Workspace
cd /home/examples/terraform-scaleway/ && terraform-report
Terraform-report outputs artefacts to the folder /home/static-server/terraform-reports/
. This folder is served by the Static File Server that you can use to view the artifacts, that include interactive HTML pages
Example with AWS
If you want to try Terraform report with your own AWS account, open workspace and configure AWS profile — create file with AWS credentials
mkdir -p ~/.aws
nano ~/.aws/credentials
The file ~/.aws/credentials
would look like this
[terraform]
aws_access_key_id = <YOUR_AWS_KEY>
aws_secret_access_key = <YOUR_AWS_SECRET>
Clone your terraform project to the workspace, or if you dont have anny, you can use this terraform example repository:
git clone https://github.com/pvarentsov/terraform-aws-free-tier /home/project/aws-example
Open file /home/project/aws-example/src/free-tier/main.tf
and comment out the part that configures S3 backend
terraform {
backend "s3" {}
}
Initialize a working Terraform directory
cd /home/project/aws-example/src/free-tier && terraform init
Paste public ssh key (for the sake of example you can type anything)
nano ./provision/access/free-tier-ec2-key.pub
Now you can generate terraform report
terraform-report
Use Static File Server to review the report
Terraform Rover
Rover — is an awesome Terraform visualizer with browser-based UI. Rover helps to better understand Terraform state and planned changes. To see how Rover works, you can use a basic tterraform example in folder /home/examples/terraform-scaleway/. Initialize Terraform project first
cd /home/examples/terraform-scaleway/ && terraform init
and start Rover to visualize terraform state
rover --workingDir /home/examples/terraform-scaleway/
If you have followed hands-on the tutorial from the previous section (terraform report from the terraform-aws-free-tier repo), you can vizualize it with Rover:
rover --workingDir /home/project/aws-example/src/free-tier
Blast Radius
Blast Radius is a tool for reasoning about Terraform dependency graphs with interactive visualizations. You can try Blast Radius — launch workspace and visualize an example Terraform project.
cd /home/examples/terraform-scaleway && terraform init
blast-radius --serve --port 8030
open localhost:8030 in browser
NOTE: Blast Radius is a great project, but there is lack of updates to the project recently, and it might not work with some Terraform providers.
Pre-commit git hooks to take care of Terraform configurations. Workspace has all dependencies innstalled.
- Visualize terraform state
inframap generate terraform.tfstate | dot -Tpng > graph.png