Followers

Terraform Provisioner

 

Terraform Provisioners

Provisioners are used to performing certain custom actions and tasks either on the local machine or on the remote machine.

File Provisioner

Example1(Amazon EC2) - Upload the file to an EC2 instance

provider "aws" {

region="us-east-1"
}
resource "aws_instance" "ec2_example" {

    ami = "ami-05fa00d4c63e32376"
instance_type = "t2.micro" key_name= "aws_key" vpc_security_group_ids = [aws_security_group.main.id] provisioner "file" { source = "/home/ubuntu/1.txt" destination = "/home/ec2-user/test-file.txt" } connection { type = "ssh" host = self.public_ip user = "ubuntu" private_key = file("/home/ubuntu/aws_key") timeout = "4m" } } resource "aws_security_group" "main" { egress = [ { cidr_blocks = [ "0.0.0.0/0", ] description = "" from_port = 0 ipv6_cidr_blocks = [] prefix_list_ids = [] protocol = "-1" security_groups = [] self = false to_port = 0 } ] ingress = [ { cidr_blocks = [ "0.0.0.0/0", ] description = "" from_port = 22 ipv6_cidr_blocks = [] prefix_list_ids = [] protocol = "tcp" security_groups = [] self = false to_port = 22 } ] } resource "aws_key_pair" "deployer" { key_name = "aws_key" public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDYQIjBKUkXyvWRrrDvLyc5hqs+8CFnCoS8LOR847TJ9NwMVPT5KsKg17k9a/edvemMCZtB15fZS2YsK/EsawacGSlaruhr5mCGQ9p8GVzkGCkjTx8gRGKU23D7KCi6lIBPMO47FbYmb22OemEinwox4vpw41V6AeRDdxnVGiw6xOKruAcN05XhUsTYw3GYoRACWn78fGsZL8YrWyK0Xze0tZlOX/48x9wD/co1znw/JkfUEjhfeCvYtnWYESy521dG9388S/8vhOXfSY4eD57PGvJj5eaN1BfizfzkQn6IviOx/lEtBGL1bIrn3nBaJROzW8nUMgUrd0APimQTERGF root@terraform" }

Example1(Windows) - Upload the file to an EC2 instance

provider "aws" {
  region = "us-east-1"
}
resource "aws_instance" "ec2_example" {

    ami = "ami-05fa00d4c63e32376"
    instance_type = "t2.micro"
    key_name= "aws_keyraman"
    vpc_security_group_ids = [aws_security_group.main.id]
    tags = {
      "Name" = "FileProvisoning"
    }

  provisioner "file" {
    source      = "C:\\ABB\\1.yaml"
    destination = "/home/ec2-user/test-file.txt"
  }
  connection {
      type        = "ssh"
      host        = self.public_ip
      user        = "ec2-user"
      private_key = file("C:\\ABB\\aws_key")
      timeout     = "4m"
   }
}

resource "aws_security_group" "main" {
  egress = [
    {
      cidr_blocks      = [ "0.0.0.0/0", ]
      description      = ""
      from_port        = 0
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "-1"
      security_groups  = []
      self             = false
      to_port          = 0
    }
  ]
 ingress                = [
   {
     cidr_blocks      = [ "0.0.0.0/0", ]
     description      = ""
     from_port        = 22
     ipv6_cidr_blocks = []
     prefix_list_ids  = []
     protocol         = "tcp"
     security_groups  = []
     self             = false
     to_port          = 22
  }
  ]
}

resource "aws_key_pair" "deployer" {
  key_name   = "aws_keyraman"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6V5MtNDz26m5qVyvUt6EIdCzXsMWyLvGLVIPhAYKLS+s4Z84wSPO8kJz9jYwjtk2gXtKQDKd0PSeiUwue7YBzXQiCd1xsskcBZvgirrwql0RTXGFKMe99Kr7H/dIrmqcedyKxYupBFIoAt4ugCtGmhsBo+gW15C0+nI+f4PWh0vubJRweEu8rjN+EnEoKbJKPp0N6N5rFwFpsq0P7mtL5c9sdh0G95eo7N1pSVAjI49BfTGzYu36w2wa1iJ6KV9VIZDFD6yanvZRjeWG9wp3Q9NeNw/AMIt7yXgzh2cf4RAqyoSSujorgzmu0i7Ki8bNGDR+U37aerNvcp5+6+rGCeF46v5N950Xa8toFfZH83Wcj4bHHrH5nyiFo18E6kV2+f51EYSU3vdDpRqu34pXlTLmcgrLc+t7jT3uzZq90D9N5DMGAeFLE3cEuYggKdAV06cEo9/dpALEqWOCGYSHw4uMPRsjcEKve/U6mztl5b749RBJyVkc6YyjigYusl5c= raman@Raman-Sharma"
}

local-exec provisioner

This provisioner is used when you want to perform some tasks onto your local machine where you have installed the terraform.

So local-exec provisioner is never used to perform any kind task on the remote machine. It will always be used to perform local operations onto your local machine.

Example(Ubuntu) - Consider the following example where we are trying to create a file hello-world.txt on the local machine

provider "aws" {
profile="rprofile"
region="ap-south-1"
}
resource "aws_instance" "ec2_example" {

    ami = "ami-04bde106886a53080"
    instance_type = "t2.micro"
    key_name= "devops"

  provisioner "local-exec" {
    command ="touch hello-world.txt"
  }
}


Example (Windows)

provider "aws" {
  region = "us-east-1"
}
resource "aws_instance" "ec2_example" {

    ami = "ami-05fa00d4c63e32376"
    instance_type = "t2.micro"
    key_name= "newkey"
    vpc_security_group_ids = [aws_security_group.main.id]
    tags = {
      "Name" = "Local command Exec"
    }

 provisioner "local-exec" {
    command ="echo 'Test' >> hello-world.txt"
  }
 
}

resource "aws_security_group" "main" {
  egress = [
    {
      cidr_blocks      = [ "0.0.0.0/0", ]
      description      = ""
      from_port        = 0
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "-1"
      security_groups  = []
      self             = false
      to_port          = 0
    }
  ]
 ingress                = [
   {
     cidr_blocks      = [ "0.0.0.0/0", ]
     description      = ""
     from_port        = 22
     ipv6_cidr_blocks = []
     prefix_list_ids  = []
     protocol         = "tcp"
     security_groups  = []
     self             = false
     to_port          = 22
  }
  ]
}


remote-exec provisioner

As the name suggests remote-exec it is always going to work on the remote machine. With the help of the remote-exec you can specify the commands of shell scripts that want to execute on the remote machine.

provider "aws" {
  region = "us-east-1"
}
resource "aws_instance" "ec2_example" {

    ami = "ami-05fa00d4c63e32376"
    instance_type = "t2.micro"
    key_name= "aws_keyraman"
    vpc_security_group_ids = [aws_security_group.main.id]
    tags = {
      "Name" = "Remote Command Execution"
    }

 provisioner "remote-exec" {
    inline = [
      "touch /home/ec2-user/hello.txt",
      "echo helloworld remote provisioner >> /home/ec2-user/hello.txt",
    ]
  }
  connection {
      type        = "ssh"
      host        = self.public_ip
      user        = "ec2-user"
      private_key = file("C:\\ABB\\aws_key")
      timeout     = "4m"
   }
}

resource "aws_security_group" "main" {
  egress = [
    {
      cidr_blocks      = [ "0.0.0.0/0", ]
      description      = ""
      from_port        = 0
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "-1"
      security_groups  = []
      self             = false
      to_port          = 0
    }
  ]
 ingress                = [
   {
     cidr_blocks      = [ "0.0.0.0/0", ]
     description      = ""
     from_port        = 22
     ipv6_cidr_blocks = []
     prefix_list_ids  = []
     protocol         = "tcp"
     security_groups  = []
     self             = false
     to_port          = 22
  }
  ]
}

resource "aws_key_pair" "deployer" {
  key_name   = "aws_keyraman"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6V5MtNDz26m5qVyvUt6EIdCzXsMWyLvGLVIPhAYKLS+s4Z84wSPO8kJz9jYwjtk2gXtKQDKd0PSeiUwue7YBzXQiCd1xsskcBZvgirrwql0RTXGFKMe99Kr7H/dIrmqcedyKxYupBFIoAt4ugCtGmhsBo+gW15C0+nI+f4PWh0vubJRweEu8rjN+EnEoKbJKPp0N6N5rFwFpsq0P7mtL5c9sdh0G95eo7N1pSVAjI49BfTGzYu36w2wa1iJ6KV9VIZDFD6yanvZRjeWG9wp3Q9NeNw/AMIt7yXgzh2cf4RAqyoSSujorgzmu0i7Ki8bNGDR+U37aerNvcp5+6+rGCeF46v5N950Xa8toFfZH83Wcj4bHHrH5nyiFo18E6kV2+f51EYSU3vdDpRqu34pXlTLmcgrLc+t7jT3uzZq90D9N5DMGAeFLE3cEuYggKdAV06cEo9/dpALEqWOCGYSHw4uMPRsjcEKve/U6mztl5b749RBJyVkc6YyjigYusl5c= raman@Raman-Sharma"
}

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
static_page
DevOpsWorld: Terraform Provisioner
Terraform Provisioner
DevOpsWorld
https://www.devopsworld.co.in/p/terraform-provisioner.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/p/terraform-provisioner.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content