Logo

dev-resources.site

for different kinds of informations.

Terraform Cloud Project Bootcamp with Andrew Brown - Creating a command alias

Published at
11/22/2023
Categories
terraform
aws
terraformcloud
hashicorp
Author
msaghu
Author
6 person written this
msaghu
open
Terraform Cloud Project Bootcamp with Andrew Brown - Creating a command alias

Hi guys , in this piece we will learn how to create an alias for the terraform command i.e instead of using terraform init, we use tf init.

This article is part of my Terraform journey with Terraform Bootcamp by Andrew Brown and Andrew Bayko, together with Chris Williams(I am also using his resources that he published here and the beloved Shala Warner. I am also using some other resources from Aaron Brooks so that I can better explain new terms. And a special shout out to Gwen Leigh for such a helpful outline that I used as a guide to create this series so that the documentation is easy to read!

As I learn more about Terraform, feel free to follow Andrew Brown on Youtube and see their free (and paid) content . Now let's jump in.

Table of Contents

  • [Shortening commands ; creating tf instead of Terraform](#shortening-commands-ccreating-tf-instead-of-ter

Shortening commands ; creating tf instead of Terraform

  • Set an alias for terraform to be tf in terraform, this can be replicated across other projects as you may like.

  • Open ~/.bash_profile for the terminal where we can set bash configurations/bash commands.

  • Add in a line
    alias tf="terraform"

  • We need to reload the file, therefore we will run source ~/.bash_profile

  • To make sure that it is set, type tf in the terminal to test it.

  • To persist it, we need to create a bash script for it;
    Create a new file set_tf_alias and paste in;

#!/usr/bin/env bash

#Checks if the alias already exists in the .bash_profile
grep -q 'alias tf="terraform"' ~/.bash_profile

# $? is a special variable in bash that holds the exit status of the last
if [ $? -ne 0 ]; then
    #if the alias does not exist, append it
    echo 'alias tf="terraform"' >> ~/.bash_profile
    echo "Alias added successfully."
else
    # inform the user if the alias already exists
    echo "Alias already exists in .bash_profile"
fi 

#Optional: source the .bash_profile to make the alias variable immediately
source ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode
  • Change its permissions in the terminal;
    chmod u+x ./bin/set_tf_alias

  • Execute it in the terminal(it should say that its already created`

  • Add it to gitpod.yml and add it for both terraform and aws.
    `
    tasks:

    • name: terraform before: | source ./bin/set_tf_alias source ./bin/install_terraform_cli source ./bin/generate_tfrc_credentials
    • name: aws-cli env: AWS_CLI_AUTO_PROMPT: on-partial before: | source ./bin/set_tf_alias source ./bin/install_aws_cli

vscode:
extensions:
- amazonwebservices.aws-toolkit-vscode
- hashicorp.terraform
`

  • Restart the workspace to make sure that it loads as expected.
hashicorp Article's
30 articles in total
Favicon
What is HashiCorp Vault? Features, Benefits, and Know How Does it Work
Favicon
Nomad 101: The Simpler, Smarter Way to Orchestrate Applications
Favicon
Vault Secret Engines: A Guide to HashiCorp Vault's Path to Secure Secrets Management
Favicon
HashiCorp Vault: Unlocking the Essentials of Secrets Management
Favicon
S3 Cross region replication with Terraform stacks
Favicon
Two Years in the Vault: 4 Best Practices 🔒
Favicon
Terraform Authoring and Operations Professional certification
Favicon
Managing Terraform State for AWS workloads with v1.10.0-beta1
Favicon
[Top-2024]! HashiCorp Terraform-Associate-003 Practice Exam To Prepare Yourself
Favicon
Vault CLI in Containers
Favicon
Introduction to Vault
Favicon
What is HashiCorp Vault?
Favicon
Mastering Terraform testing, a layered approach to testing complex infrastructure
Favicon
Hashicorp Boundary with Azure SQL Server
Favicon
Centralized TLS Certificate Management with HashiCorp Vault PKI and Cert Manager
Favicon
Run Vagrant VMs in a M1/M2/M3 Apple Sillicon Chip
Favicon
Terraform Cloud Agents - Podman
Favicon
The Reluctant Software Developer: Contributing to HashiCorp Vault
Favicon
Terraform is making my day impossible
Favicon
Deploy a HashiCorp Vault in Minutes using Spheron Compute
Favicon
Mastering Terraform: Best Practices for Scalable, Secure, and Reliable Infrastructure as Code
Favicon
Test permutations with Terraform and GitHub Actions
Favicon
Terraform Cloud Project Bootcamp with Andrew Brown - Creating a command alias
Favicon
Testing Framework in Terraform 1.6: A deep-dive
Favicon
Terraform Cloud Project Bootcamp with Andrew Brown - Configuring Terraform & AWS CLI on Gitpod
Favicon
Terraform Cloud Project Bootcamp with Andrew Brown - Week 0, 1 & 2 Journal
Favicon
Infrastructure as Code - HashiCorp Terraform
Favicon
What is HashiCorp Terraform?
Favicon
HashiCorp Developer AI
Favicon
A Comprehensive Guide to Testing in Terraform: Keep your tests, validations, checks, and policies in order

Featured ones: