Terraform Provisioner
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform output.
https://cloudaffaire.com/terraform-output/
In this blog post, we will discuss terraform provisioner.
What is Terraform Provisioner?
Provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction. Provisioners can be used to bootstrap a resource, cleanup before destroy, run configuration management, etc.
Provisioners types:
- Provisioner Connections: Many provisioners require access to the remote resource. For example, a provisioner may need to use SSH or WinRM to connect to the resource. Any connection information provided in a resource will apply to all the provisioners, but it can be scoped to a single provisioner as well.
- Provisioners Without a Resource: If you need to run provisioners that aren’t directly associated with a specific resource, you can associate them with a null_resource.
- Chef Provisioner: The chef provisioner installs, configures and runs the Chef Client on a remote resource. The chef provisioner supports both ssh and winrm type connections.
- File Provisioner: The file provisioner is used to copy files or directories from the machine executing Terraform to the newly created resource. The file provisioner supports both ssh and winrm type connections.
- Habitat Provisioner: The habitat provisioner installs the Habitat supervisor and loads configured services. This provisioner only supports Linux targets using the ssh connection type at this time.
- local-exec Provisioner: The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on the machine running Terraform, not on the resource.
- remote-exec Provisioner: The remote-exec provisioner invokes a script on a remote resource after it is created. This can be used to run a configuration management tool, bootstrap into a cluster, etc.
- Salt Masterless Provisioner: The salt-masterless Terraform provisioner provisions machines built by Terraform using Salt states, without connecting to a Salt master. The salt-masterless provisioner supports ssh connections.
Provisioner Syntax:
1 2 3 4 5 6 7 8 |
## Syntax resource " { provisioner " connection " } connection " } |
Provisioner demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
##---------------------------- ## Terraform: Provisioner ## ##---------------------------- ## Create a directory and get inside it mkdir terraform && cd terraform ## Create main.tf vi main.tf --------------- #create a ec2 instance with httpd provider "aws" { access_key = " secret_key = " region = "ap-south-1" } #create the EC2 instance resource "aws_instance" "my_web_instance" { ami = "ami-0937dcc711d38ef3f" instance_type = "t2.micro" key_name = " tags = { Name = "my_web_instance" } provisioner "remote-exec" { #execute the command in remote ec2 instance inline = [ "sudo mkdir -p /var/www/html/", #command to be executed in remote ec2 instance "sudo yum install -y httpd", "sudo service httpd start", "sudo usermod -a -G apache ec2-user", "sudo chown -R ec2-user:apache /var/www", ] } provisioner "local-exec" { #execute the command in local ec2 instance command = "echo 'Hello from terraform' > /home/ec2-user/terraform/index.html" } provisioner "file" { #copy file from local ec2 instance to remote ec2 instance source = "index.html" destination = "/var/www/html/index.html" } connection { #ssh connection details for remote ec2 instance type = "ssh" user = "ec2-user" password = "" #copy #chmod 400 private_key = "${file("/home/ec2-user/your_private_key.pem")}" } } output "we_server_address" { value = "${aws_instance.my_web_instance.public_dns}" } ---------------- :wq ## Format code terraform fmt ## Initialize terraform terraform init ## Create the resource terraform apply #Copy the we_server_address from output and check in your favourite browser ## List outputs terraform output ## Destroy the resources terraform destroy ## Cleanup cd .. && rm -rf terraform |
Hope you have enjoyed this article. In the next blog post, we will discuss terraform override.
To get more details on terraform, please refer below terraform documentation
https://www.terraform.io/docs/index.html