Terraform comparison and example code

Docker vs Ansible vs Terraform

  • Docker is particularly a container technology while Ansible are tools for configuration management.
  • Terraform, created by HashiCorp, provides a syntax to define infrastructure and services that can be hosted either on premises or in the cloud
  • ansible and terraform both are opensource.
  •  When it comes to Ansible, it’s very similar to Terraform in terms of what you can create with it. For example, you can also create the Azure Resource Group with Terraform.
  •  Terraform users define how to create, update, replace and delete resources with a programming language called HashiCorp configuration language (HCL). unlike Terraform, Ansible uses YAML
  • Ansible works with Python while Docker and Kubernetes work with Go
  •  Ansible and Terraform are complementary tools that are commonly used together.
  • Use Terraform to create resources and services. Then use Ansible to configure the resources Terraform created.

Terraform Sample code for creating a linux EC2 instance

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.49.0"
    }
    
  }
}


provider "aws" {
  region     = "us-west-2"
  access_key = "" #type access key
  secret_key = "" #type secret key
}


resource "aws_security_group" "allow_tls" {
  name        = "allow_tls"
  description = "Allow TLS inbound traffic"
  vpc_id      = aws_vpc.main.id


}


resource "aws_key_pair" "deployer" {
  key_name   = "deployer-key"
  public_key = "ssh-rsa email@example.com"
}