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
    wget https://war-jar-files.s3-us-west-2.amazonaws.com/gameoflife.war
    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.tf
    provider "aws" {
    
    }
    
    • main.tf
    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 https://github.com/GitPracticeRepo/game-of-life/blob/master/gameoflife.war",
            "sleep 5",
            "sudo service tomcat8 restart"
        ]
    
        connection {
            type                = "ssh"
            user                = "ubuntu"
            private_key         = file("./sample.pem")
            host                = aws_instance.gameoflifeprimary.public_ip
        }
        
    }
    
    

}

```
* outputs.tf
```
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.

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube