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

Step 1 - Create ec2-instance with key and group

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
}

Step 2 - Copy Ansible playbook using file Provisioner 

 provisioner "file" {

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

Step 3 - Install Ansible using remote Provisioner 
 provisioner "remote-exec" {

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

Step 4 - Run Ansinle playbook command using remote Provisioner 

provisioner "remote-exec" {

    inline = [

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

}