I've just started learning Terraform (utilizing the AWS provider) and I have managed to replicate our infrastructure which includes quite a few amazon web services.

I can successfully deploy it and get a basic Apache website up and running on an EC2 instance with the following bash script (run from Terraform):

sudo apt update -y
sudo apt install apache2 -y
sudo systemctl start apache2
sudo bash -c 'echo frontend-website > /var/www/html/index.html'

Very basic, but I'm having issues in finding some documentation on the best practices for getting our project code onto the AWS EC2 instance once provisioned, and how everything should be setup for Gitlab CI/CD to keep it all automated.

Especially for a bigger project (e.g., separate frontend and backend instances) where you may have a dev, test, and prod environment for the frontend and backend.

  • 1
    Don't do this. Use Packer instead. Provisioners are a tool of last resort. Mar 20, 2021 at 8:27

2 Answers 2


Setup a Jenkins server. Please do code checkout using Jenkins. Jenkins supports parameterized build. you can pass selected parameter from Jenkins UI to linux bash.

Then use linux's cli command to fire terraform.Terraform supports several workspace. You can use same code for dev, test and prod.

terraform workspace new dev Use this command to create new workspace. After that , we can use this workspace name to your code. Below is the content of local.tf

locals {
  env = terraform.workspace

Like below use the same code for multiple env.

resource "aws_autoscaling_group" "asg" {
  name = "${local.env}-${local.project}-demo-asg"

Moreover to save your state file securely , you can use s3 as backend.

terraform {
  backend "s3" {
    bucket = "tfstate-demo"
    key = "keyfiles"
    region = "ap-southeast-1"
    dynamodb_table = "tfstate-demo"

To separate backend and frontend service, use best practice of AWS. Create a public ALB for frontend and then move traffic to backend services via private ALB.

You can use Ansible as a configuration manager to build your server then create a AMI of that using packer. Later on, use the same AMI in ASG.


The way we do this is to use EC2 User data to run a cloud-init script after the EC2 instance has been launched.

With Terraform this user data can be templated and parameterized for different environments using Terraform's templatefile and passing in variables via parameter files.

I would suggest you also read up on AWS Auto scaling groups and launch templates as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.