Terraform copy/run scripts, and execute Commands
Run native OS commands and copy/run scripts
Website Visitors:With terraform you can also run any native commands related to the operating system, or copy files from your local machine and run them. In this tutorial, we’ll show you all the ways on how to run native OS commands, copy and run scripts from your local machine to your instance.
Running commands
After the instance is deployed, you may want to run some commands, for example, to install a software on the instance. This is possible through user_data
parameter in Terraform. Commands/script that you use in user_data parameter are like init scripts in AWS. They are executed at instance launch time. Let us show you some examples on how to use user_data
parameter.
Using commands
With user_data
parameter use all the commands in EOF block as shown below. After the instance is deployed, all the commands will be executed. In the below example, apache2 will be installed. If you open the public ip of your instance, it will show a web page which says “Created by Terraform”.
|
|
Using script file
In the same folder where you have terraform’s tf script file, create a file called “installapache.sh” and copy below commands into it.
|
|
Next, in your terraform’s tf script file, enter the code as shown below in user_data
parameter. This will run the script file on the instance after it is deployed.
|
|
Using template_file
In this type, you have to declare user data template first and then use it when you deploy your instance.
|
|
When deploying the instance refer to the data template_file as:
|
|
Make sure to create installapache.sh file in the same directory as you have your terraform files.
Copy file with Terraform
You can copy a script file from your local machine to the instance after it is deployed. You can achieve this with terraform provisioner
. Create a script file in the same folder you have all your terraform files and run provisioner as shown below:
|
|
This will copy apache.sh file from your local machine to your instance’s /home/ubuntu folder and rename it as installapache.sh.
Run script files
After copying a script file into your instance, you want to run it to fully automate the process. For running/executing a script file you should use 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.
|
|
File provisioner in this script will copy apache.sh file from your local machine to the instance, rename it as installapache.sh.
Run scripts remotely
After copying file to the instance, you want to run the script file. Using remote-exec
provisioner you will be able to run scripts. Below code will copy the script, add executable permission to the script and install apache.
|
|
Suggested Article
If you’d like to go through Terraform definitions, check it out here or browse other articles on Terraform here.
Conclusion
In this tutorial, we’ve explained how to run native OS commands and copy scripts using Terraform. We’ve also demonstrated examples for all the topics that are discussed. We hope you have learned something new in this article.
Please feel free to share your thoughts about this article in the comments section below.
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.