Terraform Script to Ec2 instance and Copy-Install-Run Ansible

DevOps

MOTOSHARE 🚗🏍️
Turning Idle Vehicles into Shared Rides & Earnings

From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.

With Motoshare, every parked vehicle finds a purpose. Owners earn. Renters ride.
🚀 Everyone wins.

Start Your Journey with Motoshare
<strong>Step 1 - Create ec2-instance with key and group</strong>

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}
provider "aws" {
  region = "us-west-2"

  access_key = "my-access-key"
  secret_key = "my-secret-key"
}
resource "aws_instance" "hari-first-ec2" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  key_name = "harish_pwc"

    tags = {
    Name = "Harish"
  }
connection {
	
      type     = "ssh"
      user     = "ubuntu"
      private_key = file("harish_pwc.pem")
      host = self.public_ip
}

<strong>Step 2 - Copy Ansible playbook using file Provisioner</strong> 

 provisioner "file" {

    source      = "C:\Users\Dilip\Desktop\Training\Ansible Playbook.yaml"
    destination = "/tmp/"
  
} 

<strong>Step 3 - Install Ansible using remote Provisioner </strong>
 provisioner "remote-exec" {

    inline = [
                 "sudo apt update",
                  "sudo apt install ansible",
	          "sudo systemctl start ansible",
 
             ] 
}

<strong>Step 4 - Run Ansinle playbook command using remote Provisioner </strong>

provisioner "remote-exec" {

    inline = [

                  "ansible all -a "df -h" -u root",
             ] 
}

}
Code language: PHP (php)