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

DevOps

YOUR COSMETIC CARE STARTS HERE

Find the Best Cosmetic Hospitals

Trusted • Curated • Easy

Looking for the right place for a cosmetic procedure? Explore top cosmetic hospitals in one place and choose with confidence.

“Small steps lead to big changes — today is a perfect day to begin.”

Explore Cosmetic Hospitals Compare hospitals, services & options quickly.

✓ Shortlist providers • ✓ Review options • ✓ Take the next step with confidence

<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)