
Here we will setup our Terraform development environment for Linux, specifically for Debian or Ubuntu based systems. And please note that the steps for installing Terraform might differ for other Linux distributions. If you're on a different Linux distribution other than Debian or Ubuntu, go check out the steps for your distribution at the link down below:
https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli#install-terraform
1. First, let's open the terminal by clicking on the terminal icon or pressing `CTRL+ALT+T`.
2. Let's make sure our system is up to date and all the necessary packages installed:
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
3. Let's now install Hashicorp GPG key to make sure we're installing it from the right repository:
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
4. Let's now make sure the GPG key has the correct signature before we install:
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
6. Next, let's add the HashiCorp repository to our system by running the following command:
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
3. Now, we need to update our package list by running the following command:
sudo apt-get update
4. Let's install Terraform now by running the following command:
sudo apt-get install terraform
5. Once Terraform is installed, let's install Visual Studio Code which is a popular code editor that we'll use for our Terraform development. There are multiple ways we can install Visual Studio Code and probably the easiest one is to download the package and install directly with that. So to do that, open up a browser window and go to:
https://code.visualstudio.com/download
Now to finish installing, let's run the install command with the .deb filename you have downloaded:
sudo apt install ./code...deb
6. Now, let's open Visual Studio Code and install the Terraform extension by HashiCorp. To do this, click on the Extensions icon in the left sidebar, search for "Terraform" in the search bar, and click on "Install".
7. Let's also verify our terraform installation. Open up our terminal and simply type `terraform -h`. There we go! Your environment is now ready to work with Terraform!
Here we will setup our Terraform development environment for MacOS.
1. Let's start by installing Visual Studio Code by typing the command: brew install --cask visual-studio-code in the terminal. This will install Visual Studio Code, which is a popular code editor that we'll use for our Terraform development.
2. Next, let's install Terraform. We'll use Homebrew again to do this. Simply type: brew install terraform in the terminal.
3. Once Terraform is installed, let's open Visual Studio Code and install the Terraform extension by HashiCorp. To do this, click on the Extensions icon in the left sidebar, search for "Terraform" in the search bar, and click on "Install". Since I've already installed it, it shows as "Installed" on my machine.
4. Finally, let's verify our terraform installation. Let's open up our terminal and simply type terraform -h.
5. To make our terraform commands typing easier, let's also install autocomplete capabilities for our terminal. Before we do that, let's create shell config file by typing touch ~/.bashrc if you're using bash or touch ~/.zshrc if you're using ZSH.
9. And finally, let's install terraform autocomplete by typing terraform -install-autocomplete . There we go! Our environment is ready to work with Terraform!
Here we will setup our Terraform development environment for Windows.
1. First, let's download the latest Terraform Windows package from the official HashiCorp website by navigating to the link down below: https://www.terraform.io/downloads.html. Choose the Windows version and click on the download link.
2. Once the download is complete, open the installer by double-clicking on the downloaded file.
3. Follow the prompts in the installer to complete the installation process. Make sure to select the option to add Terraform to your system's PATH during the installation process.
4. Next, let's install Visual Studio Code which is a popular code editor that we'll use for our Terraform development. To do this, navigate to the link down below: https://code.visualstudio.com/download and download the Windows version.
5. Once the download is complete, open the installer by double-clicking on the downloaded file.
6. Follow the prompts in the installer to complete the installation process.
7. Now, let's open Visual Studio Code and install the Terraform extension by HashiCorp. To do this, click on the Extensions icon in the left sidebar, search for "Terraform" in the search bar, and click on "Install".
8. Finally, let's verify our terraform installation. Let's open up our command line and simply type terraform -h. There we go! Our environment is ready to work with Terraform!
We're starting our journey into infrastructure as code with Terraform! To do this, we'll be following along with Amazon Web Services or AWS to create and manage our cloud infrastructure.
For those who wants to create an AWS Account, a free tier is available and can be created here: https://portal.aws.amazon.com/billing/signup
1. Let's start by logging into our AWS account.
2. I've already logged in and now let's navigate to the "IAM" service. Click on "Users" in the left-hand navigation menu, and then click on "Add user".
3. Enter a name for your user, such as "Terraform User", and select the "Programmatic access" checkbox. Then click on "Next".
4. On the next screen, select "Attach existing policies directly" and search for "AdministratorAccess". Check the box next to the policy to give your Terraform user full access to S3, and then click on the "Next: Tags" button.
5. You can add tags to your user if you'd like, but they're simply optional. Click on "Next".
6. Review the details of your user and the permissions it will have, and then click on the "Create user" button.
7. Your new user will now be created, and you will be presented with its access key ID and secret access key. Make sure to copy and securely store these credentials, as you won't be able to view the secret access key again.
8. Now, let's switch over to Terminal or command line and set the following environment variables for AWS using our newly created credentials, first with the access key ID by typing export AWS_ACCESS_KEY_ID equals your access key id:
export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
then hit enter. and same for secret access key by typing export AWS_SECRET_ACCESS_KEY equals your secret access key:
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
hit enter again and we're done with setting our AWS credentials.
There are many ways to set your AWS credentials. and this is just one approach to achieve it. To learn more about other ways of setting your AWS credentials, visit the link down below:
https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
Time has come to create our first piece of infrastructure with Terraform. We'll now create an S3 bucket on AWS using Terraform.
Follow along with me to create your first your resource using Terraform.
Source code available to download under Resources.
Terraform providers are plugins that allow you to interact with different cloud providers and services, such as AWS, Azure, and GCP. Providers define a set of resources that Terraform can manage, and provide an API for creating, updating, and deleting those resources.
In this lecture, we will be covering the basics of Terraform providers and how they are used.
Source code available to download under Resources.
In Terraform, a resource is a piece of infrastructure that we want to manage. It can be anything from an EC2 instance to an S3 bucket, and Terraform provides a large library of built-in resources for various cloud providers.
Each resource has a set of attributes that can be configured to customize its behavior. For example, an EC2 instance resource might have attributes for the instance type, the AMI image, and the security group.
In this lecture, let's look at some examples of AWS resources and their attributes.
Source code available to download under Resources.
Terraform is an open-source tool for building, changing, and versioning infrastructure. It allows you to write and manage your infrastructure as code, and provides a simple and consistent interface for interacting with different cloud providers such as AWS, Azure, GCP and much more. In this lesson, we will be covering the basics of Terraform CLI and commands.
Check out the complete list of Terraform commands here: https://developer.hashicorp.com/terraform/cli/commands
In Terraform, a data source is a way to retrieve information about an existing resource that was not created with Terraform. It allows you to reference information from another provider or from a resource in a different Terraform configuration.
Each data source has a set of attributes that can be used to filter and retrieve the information needed. For example, a data source for an AWS VPC might have attributes for the VPC ID and the associated tags.
In this lecture, let's look at some examples of AWS data sources and their attributes.
Source code available to download under Resources.
Terraform variables allow you to parameterise your configurations so that you can reuse them across different environments or scenarios. Variables can be used to pass values to your resources or to your modules.
In this lecture, let's take a look at some examples of how to define and use variables in Terraform.
Source code available to download under Resources.
Terraform has several built-in data types that can be used to define variables, resources, and other components of your infrastructure. These data types include:
Strings
Numbers
Booleans
Tuples
Maps or Objects
null
In this lecture, let's have a closer look at Terraform data types with examples.
Source code available to download under Resources.
Outputting is an important concept in Terraform, as it allows us to retrieve and display information from our infrastructure deployments.
In this lecture, we'll see how we can output infrastructure values and configuration with Terraform.
Source code available to download under Resources.
One of the most powerful features of Terraform is its ability to use loops and conditionals to create more dynamic and reusable configurations.
In this lecture, we'll explore how to use loops and conditionals in Terraform and look at some examples to understand their usage.
Source code available to download under Resources.
Terraform's null provider is a built-in provider that allows you to create custom resources and data sources without relying on any external APIs or infrastructure.
In this lecture, we'll explore how to use the null provider to create custom resources and data sources and look at some examples to understand their usage.
Source code available to download under Resources.
Terraform provides a number of utility functions that help to manipulate data and simplify resource configuration. These functions can be used within your Terraform configuration files to perform various tasks.
In this lecture, we'll explore some of the most used Terraform utility functions.
Source code available to download under Resources.
In this section, we will focus on Terraform state, how we can configure and work with it.
Terraform state is a representation of the resources that Terraform is managing. It keeps track of the resources' current state and maps it to the desired state described in the configuration file. Terraform uses state to create, modify, or destroy resources.
Terraform stores state in a state file, which is by default named terraform.tfstate. The state file is critical to the successful operation of Terraform. It is used to understand the current state of resources and track any changes that need to be made to bring the resources in line with the desired state.
Source code available to download under Resources.
Just like the local state, we can also use a remote state to manage our Terraform state with a remote system.
Terraform supports several remote state storage options, including:
Amazon S3 which is a popular remote state storage option because it provides reliable and scalable object storage.
Azure Blob Storage, a remote state storage option provided by Microsoft Azure.
Google Cloud Storage, a remote state storage option provided by Google Cloud.
And HashiCorp Consul: Consul is a distributed service mesh that can be used to store Terraform state as well
In this lecture, we'll explore remote state configuration and usage with AWS S3.
Source code available to download under Resources.
We're now familiar with how Terraform state work and if you remember, the state file is used to track changes to the infrastructure over time and to plan and apply changes.
However, when multiple people work on the same infrastructure codebase, there is a risk of concurrent changes to the state file, which can cause conflicts and result in data loss or corruption. To prevent this, Terraform provides a feature called state locking, which ensures that only one person can make changes to the state file at a time.
Terraform state locking can be implemented using a variety of backend services, including Amazon S3, Azure Blob Storage, and HashiCorp Consul. In this lecture, we will focus on using DynamoDB as the backend service.
Source code available to download under Resources.
In this lecture, we'll explore the commands we can use to manage our Terraform state.
In this lecture, we'll learn about the best practices to manage our Terraform state.
Welcome to AWS infrastructure with Terraform!
In this lecture, we will implement a reference VPC architecture with AWS. This implementation will help us gain hands-on experience with AWS as well as networking by implementing a reference infrastructure that you can also use in production. Once we implement everything, we'll go ahead and use terraform apply to provision the infrastructure on AWS.
In this lecture, we'll start implementing the AWS provider for our AWS Reference VPC Architecture.
We are already familiar with remote backends in AWS. Now it's time to actually implement and configure our own S3 remote backend for our AWS VPC Reference Architecture.
For our AWS VPC Reference Architecture implementation with Terraform, we'll need several variables to use throughout our implementation, let's implement them.
Amazon Virtual Private Cloud (VPC) is a service that lets you create your own virtual network within the AWS cloud. With Terraform, we can easily create and manage VPCs and subnets as part of our infrastructure-as-code workflow so let's implement our own VPC.
Subnets are essential parts of any VPC and you have to have subnets in order to launch resources in. Let's start by implementing our public subnets to be able to launch publicly available resources in them.
Subnets are essential parts of any VPC and you have to have subnets in order to launch resources in. Let's now implement our private subnets to be able to launch privately available resources in them.
Route tables are must-have for any network and especially for AWS VPC. In the lecture, let's create our public and private route tables in order to manage traffic across subnets and resources.
In order to take advantage of routing tables, we need to associate them to their subnets so let's do the proper associations.
NAT Gateway will allow us to access public internet within our private subnet resources. In order to launch a NAT Gateway, we need to assign it an IP so let's create an Elastic IP for it.
Let's now create our AWS NAT Gateway and add it to the Route Table to configure.
For internet access within our VPC overall, we need an Internet Gateway (IGW). Let's create that and configure with the Route Table.
Before we execute our Terraform code to launch the AWS VPC infrastructure, we need to assign values to variables we have created earlier. Let's add production.tfvars and use it to set values to the variables.
It will be nice to print out some of the infrastructure values to the terminal and our Terraform state once we execute it so that we might use them in our future infrastructure additions if we needed. Let's add outputs.tf to do exactly that.
It's time to launch our AWS VPC infrastructure! We've made all of our preparations and now let's review those changes and eventually use terraform apply to create all the resources on AWS.
Terraform modules are reusable components that can be used to manage infrastructure across multiple environments and projects. They allow you to encapsulate and abstract complex configurations, making it easier to share and maintain infrastructure code.
In this lecture, we're going to create our own terraform module that we can use to launch EC2 instances.
We have implemented our EC2 module in the previous lecture. In this lecture, let's use the module to actually create an EC2 instance with it.
By now, we've seen how powerful Terraform modules can be by implementing our own module and reusing it to create resources with it.
That's just the beginning of our journey! There's much more to Terraform modules with Terraform registry.
The Terraform Registry is the official repository for Terraform modules, maintained by HashiCorp.
In this lecture, let's explore Terraform registry further.
We now know how Terraform modules work and how we can implement them. How about if we could explore what others built in terms of Terraform modules for AWS and just reuse them instead of implementing our own, wouldn't it be cool?
That's exactly what we're going to do! In the lecture, we're going to reuse the official AWS VPC module from Terraform and see how we can take advantage of publicly available modules in Terraform Registry.
In this lecture, we will continue our modules journey further and explore the vast library of modules provided by Terraform Registry.
For our infrastructure provisioning, we usually have different environments right? A development and production for sure and we can have QA or more kind of environments to make sure we're working with the right kind of infrastructure during our software development.
To help us solve this problem for infrastructure provisioning, Terraform offers us Workspaces.
In this lecture, let's explore Terraform Workspaces and see how we can work with them.
Terraform is not just a CLI. HashiCorp offers Terraform as a service with Terraform Cloud. Let's explore that.
There's always more to learn about Terraform. In this lecture, I'll show you how to deepen your Terraform knowledge and know where to look for while doing so.
https://developer.hashicorp.com/terraform/docs
1. First up is Terraform documentation - The official documentation of Terraform is the best place to start learning about it. It covers all the essential concepts, modules, providers, and resources in detail. You can find out more about everything related to Terraform so Terraform documentation is essentially your best friend along your journey.
https://learn.hashicorp.com/
2. HashiCorp Learn - This platform from HashiCorp itself offers a variety of resources to help you learn Terraform. It covers everything from the basics to advanced topics, including how to use Terraform with other tools like AWS, Azure, and Kubernetes.
https://www.terraform.io/community
3. Terraform Community - The Terraform community is active and supportive. You can join the community forums, attend meetups, and participate in the discussions to learn more about Terraform and get help with any issues you may encounter.
https://github.com/search?q=terraform
4. GitHub - Terraform has a large and active open-source community on GitHub. You can find code samples, modules, and other resources to help you learn Terraform and get started with your own projects. You can get inspiration from other open source projects and build on top of them to implement your own solutions.
There are many more resources to learn Terraform but these are the most important ones to help you with everything related to Terraform. By the end of our course and after leveraging these resources, you can quickly become really proficient in using Terraform to manage your infrastructure as code.
There are hundreds of Terraform tools available to enhance your infrastructure as code experience with Terraform. Here I want to show you some of the best tools out there to make your Terraform development easier.
1. AIIAC: https://github.com/gofireflyio/aiac
2. terraform-switcher: https://github.com/warrensbox/terraform-switcher
3. tflint: https://github.com/terraform-linters/tflint
4. infracost: https://github.com/infracost/infracost
5. terragrunt: https://terragrunt.gruntwork.io/
6. More list of useful Terraform resources: https://github.com/shuaibiyy/awesome-terraform
Terraform certification can be handy to prove your Terraform skills and for those who are looking to get their Terraform knowledge certified by HashiCorp, I'd like to show you how you can get started.
Congratulations! you have completed the hands-on Terraform course! I hope you found it informative, engaging, and most importantly, practical. Throughout this course, you have learned the essential concepts of infrastructure as code, how to use Terraform to create and manage your infrastructure and more.
Now that you have completed this course, you are equipped with the necessary skills to use Terraform in real-world scenarios. You can now create and manage your infrastructure as code, apply changes in a safe and predictable manner, and collaborate with others using industry-standard tools and practices.
Remember, Terraform is an ever-evolving tool, and there will always be new features, modules, and best practices to learn. Keep exploring, keep experimenting, and keep learning from the Terraform community and other resources available to you. Don't forget to reach me out in Q&A to ask anything related to Terraform and I will be more than happy to help you!
I would like to thank you for taking the time to learn with me and wish you all the success in your Terraform projects. Keep on building great infrastructure with Terraform! Thank you!
Are you ready to take your infrastructure-as-code (IaC) skills to the next level? Start learning the most popular IaC language HCL and Terraform today!
Terraform is by far the most popular IaC tool as its popularity among developers have been proven with the latest StackOverflow 2022 Developer Survey. It comes right after Kubernetes in popularity and more popular than anything else on the market such as Ansible, CloudFormation, Chef and more!
Terraform's importance as an infrastructure-as-code tool is without a question and with this course, I'm here to teach you how to get started with Terraform and eventually master it with more advanced topics such as modules and AWS infrastructure implementation. Not only that, I will also guide you with next steps on your Terraform journey from documentation to essential tools to preparing for certification!
Let's have a brief overview of each of our sections and see what we will cover in this course.
In our first section, Introducing Terraform and Infrastructure as Code, we'll have a look at what manual provisioning means versus automated provisioning with Terraform. This will help you understand why there's a need for infrastructure-as-code and why Terraform is so successful and popular.
In Preparing the Environment section, I will show you how you can prepare your development environment to start working on Terraform code. There will be separate videos for Linux, MacOS and Windows so following along the video for your operating system.
In Getting Started with Terraform, we are beginning our Terraform journey by implementing our first resources with it on AWS. In this section, I will teach you all the basics of Terraform so that you have the foundations you need to start implementing your own infrastructure as code with Terraform.
In Managing Terraform State, we will learn how to configure and manage infrastructure state with Terraform. From local to remote backends with S3, we will explore different alternatives, as well as their pros and cons.
In AWS Infrastructure with Terraform section, we will take our Terraform knowledge to a next level by implementing a reference VPC architecture with AWS. I'll explain you every single line of code so you can follow along and implement easily as well.
In Reusability with Terraform Modules, we will discover the world of Terraform modules, modular pieces of Terraform code that we can reuse and implement infrastructure easily.
In Terraform Deployment and Collaboration, we will explore ways we can manage Terraform deployments such as Terraform Cloud, Workspaces and Terraform Registry.
In our last section, There's More to Terraform, I will show you the next steps and where to learn more about Terraform. Not only that but I will also show you tools to make your life easier, how you can prepare for Terraform certifications and more.
There's a great hands-on course ahead of you with many examples and resources to stay up-to-date. Whether you are an absolute beginner or an experienced Terraform user, I've designed this course in a way to help you understand every concept we cover and every single line of code we implement.
Looking forward to see you in the course and help you advance your infrastructure-as-code skills with Terraform!