Terraform and Docker

Terraform and Docker

·

10 min read

Why we need terraform:

Terraform is a popular Infrastructure as Code (IaC) tool that enables users to define and provision infrastructure resources in a declarative configuration language. There are several reasons why Terraform is widely used and considered essential in modern infrastructure management:

  1. Infrastructure as Code (IaC): Terraform allows infrastructure to be defined as code, which means you can manage your infrastructure in a version-controlled and repeatable manner. This helps in treating infrastructure as software, enabling practices such as code review, collaboration, and automation.

  2. Declarative Syntax: Terraform uses a declarative configuration language to describe the desired state of your infrastructure. This allows users to specify what they want their infrastructure to look like rather than writing scripts to achieve the desired state. Terraform then figures out the necessary actions to bring the actual infrastructure to the desired state.

  3. Multi-Cloud Support: Terraform supports multiple cloud providers such as AWS, Azure, Google Cloud Platform, as well as other infrastructure providers like VMware, Docker, and Kubernetes. This enables users to manage resources across different cloud platforms using a consistent workflow and configuration syntax.

  4. Resource Graph: Terraform builds a dependency graph of all resources defined in the configuration. This allows it to determine the order in which resources should be provisioned or destroyed, ensuring that dependencies are managed correctly.

  5. Plan and Preview Changes: Before making any changes to the infrastructure, Terraform can generate an execution plan that shows what it will do. This helps users understand the impact of their changes before applying them, reducing the risk of unintended consequences.

  6. State Management: Terraform maintains a state file that keeps track of the current state of your infrastructure. This state file allows Terraform to understand what resources are currently deployed and what changes need to be made to reach the desired state.

  7. Modularity and Reusability: Terraform configurations can be organized into modules, which are reusable units of infrastructure configuration. Modules can abstract away complex infrastructure components, promote consistency across projects, and simplify the management of infrastructure at scale.

  8. Community and Ecosystem: Terraform has a large and active community that contributes modules, plugins, and best practices. This ecosystem provides access to a wide range of pre-built configurations and integrations, saving time and effort in managing infrastructure.

Overall, Terraform provides a powerful and flexible toolset for managing infrastructure, enabling organizations to automate their infrastructure provisioning, deployment, and management processes efficiently and reliably.

Why we need Docker:

Docker is a containerization platform that allows you to package applications and their dependencies into standardized units called containers. Here are several reasons why Docker is widely used and considered essential in modern software development and deployment:

  1. Consistency across Environments: Docker containers encapsulate the application along with its dependencies, libraries, and configuration settings. This ensures consistency between development, testing, staging, and production environments, reducing the risk of "it works on my machine" issues.

  2. Isolation: Containers provide a level of isolation for applications, allowing them to run independently of the underlying host system. This isolation improves security by limiting the impact of potential vulnerabilities and conflicts between applications.

  3. Portability: Docker containers are lightweight and portable, making it easy to deploy applications across different environments and infrastructure platforms, including on-premises servers, public clouds, and hybrid cloud environments.

  4. Resource Efficiency: Docker containers share the host system's kernel and use resources more efficiently compared to traditional virtual machines. This enables higher resource utilization and allows for running multiple containers on the same host without sacrificing performance.

  5. Fast Deployment: Docker containers can be started and stopped quickly, enabling rapid deployment and scaling of applications. This agility is particularly beneficial for microservices architectures and continuous integration/continuous deployment (CI/CD) pipelines.

  6. Version Control and Rollback: Docker images are version-controlled, allowing developers to track changes to the application and roll back to previous versions if needed. This facilitates reproducible builds and simplifies troubleshooting in case of issues.

  7. Dependency Management: Docker simplifies dependency management by packaging all dependencies, libraries, and runtime environments into a single container image. This eliminates compatibility issues and streamlines the deployment process.

  8. Microservices Architecture: Docker is well-suited for microservices architectures, where applications are decomposed into smaller, loosely coupled services. Each service can be packaged as a separate container, enabling independent development, scaling, and deployment of services.

  9. Ecosystem and Community: Docker has a thriving ecosystem with a rich set of tools, frameworks, and services that extend its functionality. This includes orchestration tools like Kubernetes, monitoring solutions, logging frameworks, and more.

Overall, Docker revolutionizes the way applications are developed, deployed, and managed by providing a consistent, efficient, and portable platform for building and running containerized applications. It has become an essential tool in modern software development workflows, empowering teams to deliver applications faster, with greater consistency and reliability.

Basic Terraform commands:

Terraform commandsPurpose of this commands
terraform initThe command initializes a Terraform configuration, downloading necessary plugins and modules. It prepares the environment for managing infrastructure, ensuring dependencies are met for subsequent operations like planning and applying changes to infrastructure defined in Terraform configuration files.
terraform init -upgradeThe command upgrades Terraform plugins to the latest compatible versions, ensuring compatibility with the current Terraform configuration. It fetches and installs updated dependencies, enhancing security, performance, and compatibility with new features, thus improving the infrastructure management process.
terraform planThe command generates an execution plan outlining proposed infrastructure changes based on the current configuration. It previews additions, modifications, or deletions of resources, providing insight into the effects of applying changes. This helps verify configurations and avoid unintended alterations before execution.
terraform applyThe command executes planned infrastructure changes as defined in Terraform configuration files. It provisions or modifies resources based on the execution plan generated by terraform plan. This applies changes to the actual infrastructure, ensuring alignment with the desired state defined in the configuration.
terraform validateThe command checks the syntax and configuration of Terraform files, ensuring they conform to the expected format and standards. It verifies the correctness of resource configurations, variable references, and module usage, helping to identify errors or issues early in the development process.
terraform fmtThe command automatically formats Terraform configuration files to adhere to a consistent style and layout. It standardizes indentation, spacing, and formatting conventions, improving readability and maintainability of code. This ensures consistency across configurations and facilitates collaborative development workflows.
terraform destroyThe command dismantles infrastructure resources defined in Terraform configuration files, reverting the environment to its initial state or removing all provisioned resources. It facilitates the cleanup and teardown of infrastructure, helping to reduce costs and ensure clean environments between deployments or testing cycles.

What is Resource Block and Provider Block:

  1. Resource Block: A resource block defines a single infrastructure object, such as an AWS EC2 instance or an Azure Virtual Network. It specifies the resource type, its attributes, and any dependencies. Resource blocks are used to declare the desired state of infrastructure.

Example:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
  1. Provider Block: A provider block configures the respective cloud or infrastructure provider that Terraform will interact with to manage resources. It defines authentication details, region settings, and other necessary configurations. Multiple provider blocks can be used in a Terraform configuration to manage resources across different cloud providers.

Example:

provider "aws" {
  region = "us-west-2"
}

In summary, resource blocks define individual infrastructure objects, while provider blocks configure the cloud or infrastructure provider to interact with during resource provisioning and management.

Create a Terraform files and provide the docker provider block and Install the nginx:

Step by step we will do.

Create an EC2 machine and install the terraform and docker packages:

  1. Log in to the AWS Management Console: Go to https://console.aws.amazon.com/ and sign in to your AWS account.

  2. Navigate to EC2 Dashboard: Click on the "Services" dropdown menu at the top-left corner and select "EC2" under the "Compute" section.

  3. Launch Instance: In the EC2 Dashboard, click on the "Launch Instance" button.

  4. Choose an Amazon Machine Image (AMI):

    • In the "Step 1: Choose an Amazon Machine Image (AMI)" section, select "Ubuntu" or any other desired Ubuntu AMI.
  5. Choose an Instance Type:

    • In the "Step 2: Choose an Instance Type" section, select "t2.medium" as the instance type.
  6. Configure Instance Details:

    • Leave the default settings or adjust them according to your requirements.

    • In the "Advanced Details" section, paste the following script in the "User data" field. This script will be executed on instance startup and will install Terraform and Docker:

#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update -y
sudo apt-get install -y docker-ce
sudo apt-get install -y unzip
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update -y
sudo apt-get install -y terraform
  1. Add Storage:

    • 8Gb
  2. Add Tags:

    • (Optional) Add any tags you want to associate with the instance.
  3. Configure Security Group:

    • Create or select an existing security group that allows SSH access (port 22) and any other necessary ports for your application.

    • Ensure that the security group allows inbound traffic on port 22 from your IP address or a specific IP range for SSH access.

  4. Review and Launch:

    • Review the configuration details and click on the "Launch" button.
  5. Select an Existing Key Pair or Create a New Key Pair:

    • Choose an existing key pair or create a new one. This key pair will be used to connect to the EC2 instance via SSH.
  6. Launch Instance:

    • Click on the "Launch Instances" button to launch the EC2 instance.

Once the instance is launched, you can SSH into the instance using the private key associated with the key pair (terraform-medium.pem) and verify that Terraform and Docker are installed by running the following commands:

terraform --version
docker --version

You should see the version information for Terraform and Docker if they are installed successfully.

we need to create a directory and in this directory we need to create a another directory and another directory we need to create terraform file.

Create a Provider block Terraform script:

terraform.tf

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "3.0.2"
    }
    aws = {
      source = "hashicorp/aws"  # Changed source to official provider
      version = "4.66.0"
    }
  }
}

provider "aws" {
  region = "ap-south-1"
}

provider "docker" {
  # You can specify configuration for the Docker provider here if needed
}

Make sure your ec2 instance is running same region "ap-south-1".

Create a resource Block for an nginx docker image

resource "docker_image" "nginx" {

           name = "nginx:latest"
           keep_locally = false

}

resource "docker_container" "nginx" {
      image = docker_image.nginx.name
      name = "Docker-Nginx"
      ports {
                  internal = 80
                  external = 80
   }
}

main.tf:

Now need to run the command terraform init for Terraform configuration, downloading necessary plugins and modules. It prepares the environment for managing infrastructure, ensuring dependencies are met for subsequent operations like planning and applying changes to infrastructure defined in Terraform configuration files.

Now need to check code is validate or not type terraform validate command

Now need to run terraform plan command for generates an execution plan outlining proposed infrastructure changes based on the current configuration. It previews additions, modifications, or deletions of resources, providing insight into the effects of applying changes. This helps verify configurations and avoid unintended alterations before execution.

Now you need to run the command terraform apply for executes planned infrastructure changes as defined in Terraform configuration files. It provisions or modifies resources based on the execution plan generated by terraform plan. This applies changes to the actual infrastructure, ensuring alignment with the desired state defined in the configuration.

And now go the the ec2 instance and copy to public ip and paste to another browser