Terraform Script example

Write a Terraform script which create a ubuntu ec2-instance and copy a ansible playbook, install ansible and run it.


provider "aws" {
  region     = "us-west-2"
  access_key = " "
  
  secret_key = " "
}
resource "aws_instance" "second-ec2" {
  ami           = "ami-03d5c68bab01f3496" # us-west-2
  instance_type = "t2.micro"
  key_name 		= "bhargavi"
  tags = {
    Name = "RajeshKumar"
  }
  
  connection {
      type     = "ssh"
      user     = "ubuntu"
      private_key = file("bhargavi.pem")
      #host = aws_instance.web.public_ip
      host = self.public_ip
  }

  provisioner "local-exec" {
    command = "touch devopsschool-local"
  }
  
  provisioner "remote-exec" {
    inline = [
	  "sudo apt-get update",
      "sudo amazon-linux-extras install ansible2 -y",
      "sudo yum install git -y",
      "ansible-playbook example.yml 

      
    ]
  }
  
  provisioner "file" {
    source      = "example.yml"
    destination = "/tmp/"
  } 
}