Terraform how is it used?
Asked Answered
L

4

5

I have been experimenting with Terraform over the last while and it seems to be a very powerful tool for setting up / tearing down cloud infrastructure quickly.

I am trying to work out though what are the actual use cases for this in the real world beyond the initial setup of your VMs etc? Once the initial setup has completed do you use something like Chef to manage the infrastructure? It just seems that Terraform would not be used too often for smaller companies who are not constantly setting up and tearing down clusters or data centres.

Can someone set me in the right direction, i have checked Hashicorps docs but it is still not abundantly clear to me what the real world scenarios would be to use this once your infrastructure has been set up.

Legging answered 7/12, 2017 at 16:52 Comment(3)
That's broadly accurate - Terraform is basically responsible for provisioning (cloud) infrastructure. If your infrastructure requirements don't change, then your Terraform code doesn't need to change.Beller
I use it to manage my entire AWS infrastructure: Security groups, VPCs, subnets, long-running instances, launch configurations, autoscaling groups, s3 buckets, lambda functions, IAM users, groups & policies, aws inspector, amazon config, load balancers, ECS clusters, ECS registries - etc, etc. I could add our DNS configuration in, I could connect it to Google Cloud resourcesJanitajanith
The whole idea of Terraform could be resumed in Infrastructure as Code, so basically terraform allows you to create infrastructure (not configure it) in a testable, reliable, reproducible and automatic way. After applying terraform you still probably want to run a Configuration Management tool like Puppet, Chef or Salt.Trunks
C
4

You can use terraform to setup resources, modify, delete them. You just need to write the terraform file/code/template which is used to manage resources. This Terraform code help us to manage resources

e.g. You can use Terraform Template/File/Code to create EC2 instance resource on AWS Cloud, modify it and delete when not needed. Here, AWS is called as a provider.

Terraform support a lot of providers.

A basic template would like as follows.

This template will create a VPC resource on AWS provider.

variables.tf

variable "access_key" {
     description = "Access key to AWS console"
}
variable "secret_key" {
     description = "Secret key to AWS console"
}
variable "region" {
     description = "Region of AWS VPC"
}

terraform.tfvars

region = "eu-west-3"
access_key = "YOUR-ACCESS-KEY"
secret_key = "YOUR-SECRET-KEY"

main.tf

provider "aws" {
      region     = "${var.region}"
      access_key = "${var.access_key}"
      secret_key = "${var.secret_key}"
}
resource "aws_vpc" "vpc" {
     cidr_block = "10.0.0.0/16"
}
resource "aws_internet_gateway" "gateway" {
     vpc_id = "${aws_vpc.vpc.id}"
}
resource "aws_route" "route" {
     route_table_id         = "${aws_vpc.vpc.main_route_table_id}"
     destination_cidr_block = "0.0.0.0/0"
     gateway_id             = "${aws_internet_gateway.gateway.id}"
}
data "aws_availability_zones" "available" {}
resource "aws_subnet" "main" {
    count                   = "${length(data.aws_availability_zones.available.names)}"
    vpc_id                  = "${aws_vpc.vpc.id}"
    cidr_block              = "10.0.${count.index}.0/24"
    map_public_ip_on_launch = true
    availability_zone       = "${element(data.aws_availability_zones.available.names, count.index)}"
}

Commands:

terraform init
terraform plan
terraform apply

I would recommend to go through Terraform's Official Site. They have good documentations available on their site.

Crass answered 14/4, 2020 at 10:6 Comment(0)
C
3

Terraform can be used to setup your infrastructure the way you want. It can be used to setup cluster of nodes in a smaller amount of time than doing it manually which is time consuming. Once you write down terraform templates you can run it to setup the nodes instantly. The greatest advantage of using Terraform is that it supports various cloud providers like AWS, Microsoft Azure, Google Cloud, Openstack etc and it is open source.

If you have a dynamic environment where you would need to scale up and down often definitely terraform will help you to ease the work. For example setting up a Mesos or a DC/OS cluster. If you want to increase the number of nodes in your mesos cluster you just need to update the number of instances require in your terraform template file. Once you have setup your cluster you can utilize any configuration management tool like chef to apply the configurations.

I would recommend reading the blog series which explains well why Terraform is needed.

Crosier answered 16/12, 2017 at 16:0 Comment(0)
H
1

Terraform is the IAAS(Infrastructure as the service) tool. It is used to automate the infra provision on the various cloud providers (more than 75). It has become the industry standard for IAAC.

https://www.terraform.io/docs/providers/index.html

Why Terraform ?

  • Free of cost
  • The manual tasks are error-prone and inconsistent
  • Multiple platform support
  • Easy to write (JSON based)
  • Easy integration with configuration management tool (Ansible).

Sample script to launch AWS - EC2 instance

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

resource "aws_instance" "myec2" {
  ami = "ami-0e34e7b9ca0ace12d"
  instance_type= "t2.micro"
}
Haymow answered 18/6, 2020 at 0:18 Comment(0)
B
0
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckChecked">
  <label class="form-check-label" for="flexSwitchCheckChecked">Email Sent</label>
</div>


document.addEventListener('DOMContentLoaded', () => {
  // Function to update the checkbox based on Emailsent value
  function updateCheckbox(emailSent) {
    const emailSentCheckbox = document.getElementById('flexSwitchCheckChecked');
    emailSentCheckbox.checked = emailSent; // Set the checkbox state
  }

  // Simulate fetching data
  const emailSentStatus = true; // Replace with actual data fetching logic
  updateCheckbox(emailSentStatus);
});


// Inside your component class
onEditClick(): void {
  this.fetchData(); // Fetch and update the form
}

document.getElementById('editButton').addEventListener('click', () => {
  const emailSentStatus = true; // Replace with actual data fetching logic
  updateCheckbox(emailSentStatus);
});
Badr answered 30/7 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.