DevOps Classroom Series – 21/Jan/2020

Terraform Provisioning

  • Provisioning in Terraform is majorly used to do Configuration Management
  • Terraform supports many provisioners, some of them are
    • remote-exec
    • local-exec
    • Chef
  • For Ansible based CM use remote-exec to install ansible and execute ansible-playbook with localhost in inventory
  • To use provisioners, you should also use connection
  • Provisioners are written in terraform resources
resource '<resource-type>' '<resource-name>' {


    connection {
        ...
    }

    provisioner 'p-type' {

    }
}
  • Lets create a ec2 machine and install git & apache
  • First create ec2 machine

resource "aws_instance" "apache" {
    ami = "ami-04590e7389a6e577c"
    instance_type = "t2.micro"
    key_name = "packer"
    security_groups = ["sshonly"]
}
  • Now lets add the connection section for terraform to connect to ec2 machine

resource "aws_instance" "apache" {
    ami = "ami-04590e7389a6e577c"
    instance_type = "t2.micro"
    key_name = "packer"
    security_groups = ["sshonly"]

    connection {
        type = "ssh"
        user = "ec2-user"
        host = "${aws_instance.apache.public_ip}"
        private_key = "${file("./packer.pem")}"
    }

  
}      

  • Now lets provision using remote-exec to install git & apache

resource "aws_instance" "apache" {
    ami = "ami-04590e7389a6e577c"
    instance_type = "t2.micro"
    key_name = "packer"
    security_groups = ["sshonly"]

    connection {
        type = "ssh"
        user = "ec2-user"
        host = "${aws_instance.apache.public_ip}"
        private_key = "${file("./packer.pem")}"
    }

    provisioner "remote-exec" {
        inline = ["sudo yum install git -y", "sudo yum install httpd -y"]
    }

  
}

Backends

Exercise

  • Write a terraform template to create two vms (ec2 or azure vms) and install tomcat in one vm and mongo db (any database) in other vm

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin