DevOps Classroom Series – 30/May/2020

Terraform Provisioning

  • Scenario: You are asked to create a VM in the cloud with the latest version of the application installed gameoflife

  • Steps:

    • Create a VM using terraform template
    • Now login into the vm and execute the commands
  • As of now we dont know how to login into resources and execute commands and that is what a Terraform Provisioner does.

  • For Docs Refer Here

  • Workflow of Provisioining in Terraform

    1. Create a resource
    2. Estabish a Connection
    3. Execute Provisioning: Preview
  • Steps: Installing game of life on AWS EC2

    • Create an EC2 instance and once ec2 is ready
    • Login and execute the following commands
    # Ubuntu VM is ready
    sudo apt-get update
    sudo apt-get install openjdk-8-jdk -y
    sudo apt-get install tomcat8 -y
    cd /tmp
    sudo cp /tmp/gameoflife.war /var/lib/tomcat8/webapps
    • Navigate to http://<publicip>:8080/gameoflife
  • Thinking in Terraform:

Solution for Gameoflife in AWS Ec2

  • Create a new directory gameoflife and create
    provider "aws" {
    resource "aws_instance" "gameoflifeprimary" {
    ami                         = var.ubuntuami
    instance_type               = "t2.micro"
    key_name                    = var.keyname
    security_groups             = var.securitygroups
    associate_public_ip_address = true
    provisioner "remote-exec" {
        inline                  = [
            "sudo apt-get update",
            "sleep 5",
            "sudo apt-get install openjdk-8-jdk -y",
            "sleep 5",
            "sudo apt-get install tomcat8 -y",
            "sleep 5",
            "cd /var/lib/tomcat8/webapps",
            "sudo wget",
            "sleep 5",
            "sudo service tomcat8 restart"
        connection {
            type                = "ssh"
            user                = "ubuntu"
            private_key         = file("./sample.pem")
            host                = aws_instance.gameoflifeprimary.public_ip


output "publicip" {
value = aws_instance.gameoflifeprimary.public_ip
  • Now apply terraform to create aws ec2 instance with game of life installed

Leave a Reply

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

About learningthoughtsadmin