Terraweek Day 1 Challenge

Table of contents

Hello everyone, I am excited to announce that I have taken up the TWS TerraWeek Challenge! Over the next 7 days, I will be participating in the Terraform Challenge Program.

It’s my Day 1 of #Terraweek challenge

Table of content:

  • Introduction to Terraform :

    • What is Terraform?

    • Benefits of Terraform.

  • Why Terraform?

    • Why do we need Terraform and how does it simplify infrastructure provisioning?
  • Installation :

    • How can you install Terraform and set up the environment for AWS, Azure, or GCP?
  • Widely used terminologies in Terraform :

    • The important terminologies of Terraform with the example at least (5 crucial terminologies).

    • What is Terraform and how can it help you manage infrastructure as code?

      • Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It enables you to create, manage, and version infrastructure resources across various cloud providers and other infrastructure platforms.

      • It allow you to manage infrastructure with configuration files rather than through a graphical user interface.

      • It lets you define resources and infrastructure in human-readable, declarative configuration files, and manages your infrastructure's lifecycle.

    • Benefits of Terraform :

      • Terraform can manage infrastructure on multiple cloud platforms.

      • The human-readable configuration language helps you write infrastructure code quickly.

      • Terraform's state allows you to track resource changes throughout your deployments.

      • You can commit your configurations to version control.****

      • By defining infrastructure as code, you can ensure that your infrastructure is consistent across different environments, reducing the risk of errors and inconsistencies.

      • IaC enables you to automate the provisioning and deployment of infrastructure, significantly reducing the time it takes to set up new environments or make changes to existing ones.

      • With IaC, developers and operations teams can work together more effectively, as they can share and collaborate on infrastructure code.

    • Why do we need Terraform and how does it simplify infrastructure provisioning?

      • Multi-Cloud and hybrid-cloud support : Organisation often se multi cloud providers or combination of cloud and on-premises infrastructure. Cloud providers like AWS, azure, GCP are supported by Terraform. Terraform allows you to manage infrastructure across multiple providers using a single tool.

      • IaaC : Terraform enables you to define infrastructure resources using code. This approach brings the benefits of software development practices, such as version control, code review, and collaboration, to infrastructure management. It uses a declarative language (HCL) which allows you to describe the desired state of your infrastructure, rather than specifying the steps to achieve it.

      • Reusability : Terraform promotes use of modules, which are reusable. This encourages the creation of reusable components, making it easier to manage and scale your infrastructure.

      • Change management : When updates are needed and requirements are changed, Terraform helps manage these changes in a controlled manner. By running the terraform plan command, you can preview the changes that will be applied and review them before making any modifications.

    • How can you install Terraform and set up the environment for AWS, Azure, or GCP?

Steps to install Terraform and create an (EC2) instance in AWS using Terraform.

  1. Create a new project directory and inside that directory create an another directory where installation of terraform and aws-cli is done.

    1. mkdir terraform-project

    2. cd terraform-project

    3. mkdir terraform-aws-cli

    4. wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg 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 sudo apt update && sudo apt install terraform

    5. sudo apt install awscli

  2. After installing the aws-cli and terraform, configure the aws values and connect aws and terraform.

    1. aws configure

    2. export AWS_ACCESS_KEY_ID=<access key>

    3. export AWS_SECRET_ACCESS_KEY=<secret key>

  3. Create a main.tf file where you will have your terraform configuration.

  4. Define a provider and resource: In main.tf, define a provider and a resource using HCL. For example, to create a simple AWS EC2 instance, you might use the following code:

  • Initialise terraform using terraform init. It is used to initialise a Terraform project in a directory and will download the necessary provider plugins and set up the backend for storing your state file.

  • Run terraform apply to create your infrastructure. Terraform will prompt you to confirm that you want to proceed. Type yes and press Enter to continue.

  • The terraform plan command is used in Terraform to generate an execution plan for your infrastructure.

Your Ec2 instance is created using Terraform.

Similarly one can create various services using Terraform on multiple cloud providers.

Important Terminologies

  1. Provider: A provider is a plugin that Terraform uses to interact with various infrastructure platforms or services.

  2. Resource: A resource is a tangible piece of infrastructure that Terraform manages. It represents a specific component or service provided by a provider. Examples of resources include virtual machines, networks, storage buckets, databases, and more.

  3. A module is a reusable unit of Terraform configuration that encapsulates a set of resources and their configurations. Modules allow you to organize and abstract your infrastructure code

  4. State: Terraform keeps track of the state of your infrastructure in a state file. The state file records the desired state of the resources defined in your configuration and also tracks any changes that have been made.

  5. Plan: A plan is the output of the terraform plan command. It shows the actions that Terraform will take to bring the current state of the infrastructure to the desired state specified in the configuration files

  6. Apply: The terraform apply command is used to apply the changes specified in the configuration to the infrastructure. It reads the desired state from the configuration files and updates the infrastructure to match that state. It creates, modifies, or destroys resources as necessary.

  7. Variable: Variables allow you to parameterize your Terraform configurations, making them more flexible and reusable

  8. Output: Outputs in Terraform allow you to export values from your infrastructure configuration.

Conclusion :

In this blog, we have explored the basics of Infrastructure as Code (IaC) and why Terraform is a popular choice for managing infrastructure. We also created an Ec2 instance on AWS cloud provider using Terraform.