Skip to main content

Command Palette

Search for a command to run...

Using Terraform to create an Azure Resource Group on Linux

Published
5 min read
Using Terraform to create an Azure Resource Group on Linux

What is a Terraform?

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define and manage infrastructure using a high-level configuration language called HashiCorp Configuration Language (HCL). Terraform allows you to automate the provisioning and management of resources across various cloud providers and on-premises environments

Features of Terraform:

  • Declarative Configuration: You describe the desired state of your infrastructure and Terraform figures out how to achieve that state.

  • Multi-Cloud Support: Manage resources across multiple cloud providers like Azure, AWS, Google Cloud, and more.

  • Version Control: Configuration files can be versioned, reused, and shared, making collaboration easier.

  • Automation: Integrate with CI/CD pipelines to automate infrastructure changes.

  • State Management: Keeps track of the real infrastructure state in a state file, ensuring consistency.

How Terraform Works:

Terraform allows you to;

  1. Write: Define resources in configuration files.

  2. Plan: Terraform generates an execution plan showing what it will do.

  3. Apply: Execute the plan to create, update, or destroy resources

What is Nginx?

Nginx is an open-source web server that also functions as a reverse proxy, load balancer, mail proxy, and HTTP cache. It was created by Igor Sysoev and first released in 2004.

Key Features of Nginx:

  • High Performance: Designed to handle a large number of simultaneous connections with low memory usage.

  • Reverse Proxy: Can act as an intermediary for requests from clients seeking resources from other servers.

  • Load Balancing: Distributes incoming network traffic across multiple servers to ensure no single server becomes overwhelmed.

  • HTTP Cache: Stores copies of responses to reduce the load on backend servers and improve response times.

  • TLS/SSL Support: Provides secure communication over a computer network.

Common Use Cases:

  • Web Serving: Efficiently serves static content like HTML, CSS, and images.

  • Proxying: Acts as a gateway for forwarding client requests to other servers.

  • Load Balancing: Balances traffic across multiple servers to optimize resource use and prevent overload.

  • Caching: Improves performance by caching responses from backend servers.

Nginx is widely used by high-traffic websites and services.

What is Microsoft Azure?

Microsoft Azure is a comprehensive cloud computing platform developed by Microsoft. It offers a wide range of services and products designed to help you build, deploy, and manage applications through Microsoft’s global network of data centers.

Features of Microsoft Azure:

  • Infrastructure as a Service (IaaS): Provides virtualized computing resources over the internet, allowing you to run virtual machines and other resources.

  • Platform as a Service (PaaS): Offers a platform allowing customers to develop, run, and manage applications without dealing with the underlying infrastructure.

  • Software as a Service (SaaS): Delivers software applications over the internet, on a subscription basis.

  • Hybrid Cloud: Integrates on-premises infrastructure with cloud services, providing flexibility and scalability.

  • Security and Compliance: Includes a wide array of security tools and compliance certifications to protect your data and applications.

  • AI and Machine Learning: Provides tools and services to build, train, and deploy machine learning models.

  • Internet of Things (IoT): Supports IoT solutions with services like Azure IoT Hub and Azure IoT Central.

Popular Use Cases:

  • Web and Mobile Apps: Develop and host web and mobile applications.

  • Data Storage and Analytics: Store and analyze large volumes of data.

  • Disaster Recovery: Implement robust disaster recovery solutions.

  • DevOps: Integrate with CI/CD pipelines to automate application deployment and management.

Azure supports a variety of programming languages, frameworks, and tools, making it a versatile choice for developers and enterprises alike.

Step by step guide creating an Azure Resource Group using Terraform as the infrastructure tool code, Nginx as the web server on Linux.

  1. Install Azure click on your Linux using the code below.

    “curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash”

  2. Ensure to use az login to be in your subscription

  3. Create a directory in which to test the sample Terraform code and make it the current directory using the code below.

    “mkdir myterrachris”

    make it the current directory using the code below.

    “CD myterrachris”

  4. Create a file named providers.tf and insert the following code as shown below.

    “vim providers.tf”

  1. Create a file named main.tf and insert the following code:

    “vim main.tf”

  2. Create a file named variables.tf and insert the following code:

    “vim variables.tf”

  3. Create a file named outputs.tf and insert the following code:

    “vim outputs.tf”

  4. Initialize Terraform using the code below.

    “terraform init -upgrade”

  5. Create a Terraform execution plan using the code below

    “terraform plan -out main.tfplan”

  6. Apply a Terraform execution plan using the code below

    terraform apply main.tfplan

  7. Verify the results using the following codes below

    “resource_group_name=$(terraform output -raw resource_group_name)”

    “az group show --name $resource_group_name”

  8. To confirm the resource group has been created, log in to your azure portal

  9. To clean up resources, use the following code below

    “terraform plan -destroy -out main.destroy.tfplan”

    • The terraform plan command creates an execution plan but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.

    • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.

  1. To apply the cleanup execution plan, run the following code below

    “terraform apply main.destroy.tfplan”

    Thank you for subscribing