DEV Community

Cover image for Launch AWS EC2 Instances with Terraform Using Custom User Data Scripts
Durrell  Gemuh
Durrell Gemuh

Posted on

Launch AWS EC2 Instances with Terraform Using Custom User Data Scripts

Automating AWS EC2 instance launches with Terraform is powerful, but adding custom user data scripts makes it even more flexible. This post walks you through how to manage multiple initialization scripts in a GitHub repository and select which runs at launch by simply changing one line in your Terraform instance file.

Why Use User Data with Terraform?

User data scripts automate instance configuration—installing packages, starting services, and any other setup needed on first boot. Terraform automates resource provisioning, and combining the two lets you deploy ready-to-use servers consistently.

Repository Setup Overview

  • Terraform code stored in a GitHub repository.
  • Multiple custom user data scripts saved inside a scripts/ folder.
  • On launch, specify the script to run by changing a single line in the Terraform file (commonly on line 25 of the instance main Terraform configuration).
  • This keeps the infrastructure code clean and modular.

How to Use

1. Clone the Terraform repo

git clone https://github.com/durrello/aws-ec2-terraform
cd aws-ec2-terraform
Enter fullscreen mode Exit fullscreen mode

2. Edit main.tf to select your user data script

Open instance/main.tf and locate where the user data is set (around line 25). Edit this line to specify which script to run from the scripts/ folder.

resource "aws_instance" "main" {
  # other instance config ...

  user_data = file("./scripts/web_server.sh")

  # ...
}
Enter fullscreen mode Exit fullscreen mode

Replace web_server.sh with the script you want, like db_server.sh or your custom script.

3. Configure other variables

Make sure your variables (instance type, AWS region, security groups, etc.) are set appropriately either via terraform.tfvars or CLI options.

4. Run Terraform

Initialize and apply your Terraform configuration:

terraform init
terraform apply
Enter fullscreen mode Exit fullscreen mode

Terraform will launch the EC2 instance and execute the user data script during boot.

Benefits of This Approach

  • Keep multiple user data scripts for different initialization needs.
  • Change scripts easily without modifying the main Terraform logic.
  • Version control scripts alongside infrastructure code in GitHub.
  • Rapidly launch tailored instances for different roles.

Example Repository Structure

.
├── main.tf
├── scripts
│   ├── web_server.sh
│   ├── db_server.sh
│   └── custom_setup.sh
├── variables.tf
└── outputs.tf
Enter fullscreen mode Exit fullscreen mode

You can switch scripts by simply updating the script name in main.tf on the user_data line.

Conclusion

This flexible method of managing EC2 user data scripts with Terraform and GitHub streamlines launching customized instances. It suits environments where servers play different roles but share the same provisioning pipeline.

By combining Terraform's infrastructure-as-code with user data scripting, infrastructure automation becomes more modular, maintainable, and efficient.

Happy Terraforming!

Top comments (0)