Learning how to execute a Bash script from Terraform

For those learning AWS/AWS CLI, Terraform is a tool for building infrastructure with various technologies including AWS, Azure, GCP, and vSphere.

Here is a very simple document on how to use Terraform to build an AWS EC2 Linux instance and then execute a bash script from Terraform against the newly created instance. The bash script will be executed from Terraform as a provisioner.

Executing a bash script from Terraform can be used to configure the newly created server or for any other purpose.

The example below will show how to execute a script named setup-lnxcfg-user; this bash script will prepare the newly created Linux instance for Ansible playbook execution from an Ansible controller server. The script creates a new login id named lnxcfg, sets up ssh-keys, and sudo access on the new instance. This instance will be an Ansible client of an Ansible controller server; using Ansible is beyond the scope of this document, however, learning how to execute a bash script from Terraform will be very useful.

The setup-lnxcfg-user bash script is as follows:

setup-lnxcfg-user:

#!/bin/bash
# setup-lnxcfg-user
# create lnxcfg user for Ansible automation
# and configuration management.

The terraform configuration and variable files are below.

Within the main.tf Terraform file, the nested connection block will connect to the newly created Linux instance through the ssh protocol using the AWS login user of ec2-user and the AWS pem key associated with the Linux instance. The nested provisioner “file" block will copy the setup-lnxcfg-user bash script to the /tmp directory of the new instance. The nested provisioner “remote-exec” block will add execute permission on the /tmp/setup-lnxcfg-user bash script and then execute the script with sudo permissions from the AWS login user, ec2-user. The bash script’s primary purpose is to set up the new server as an Ansible client of an Ansible controller server. After the Terraform configuration is complete an Ansible controller server will be able to use the lnxcfg user to login, sudo up, and execute Ansible playbooks against the new server.

main.tf:

# create a Linux instance in AWS
# execute bash script to set up Ansible client user

variables.tf:

# variables.tf

Once these files are created you can use the terraform commands to syntax check and prepare the deployment and then deploy the AWS instance.

Within the directory where these files are located issue the command:

terraform init

The init argument will initialize the environment.

Then issue:

terraform plan -out main.plan

The plan argument will syntax check the files and prepare the deployment.

Deploy the instance:

terraform apply main.plan

To view data about the instance execute:

terraform show

To destroy the instance execute:

terraform destroy

Executing a bash script against a newly created Linux server from within Terraform can be very useful.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store