I’m new to both Terraform and cloud services, so this question might be stupidly simple for the more experienced folks.
Terraform is all about deploying infrastructure. After configuring the backend (either storing locally, in an S3 bucket/DynamoDB, on Terraform Cloud etc.), every change to the terraform code followed by terraform apply
will idempotently change infrastructure. This makes sense to me.
Where everything falls apart however, is when I want to run a service on one of the deployed virtual machines. Let’s take a hello world NodeJS + Express web server as an example.
In my world view, this is what I expected to be able to do:
- Define infrastructure and
terraform apply
# see https://github.com/revosw/portfolioassignment/blob/main/main.tf
# for entire terraform file
resource "aws_instance" "web" {
# Which virtual machine image this VM should be based on
ami = "ami-0bd9c26722573e69b"
# How much hardware resources does this instance need
instance_type = "t3.micro"
# What commands should be executed when the VM boots
user_data = file("install.sh")
# The ssh key pair to apply to this virtual machine
key_name = "terraform-abcd"
# Required for network setup
security_groups = ["terraform-sg-abcd"]
}
- In the file
install.sh
set up my virtual machine
#! /bin/bash
adduser webmaster
# Install Volta - node and npm manager
curl https://get.volta.sh | bash
. ~/.profile
volta install node@17
# Clone repository
git clone https://github.com/revosw/terraform-project
cd portfolioassignment
# Install dependencies and start server
npm install
npm start
- Define github action to redeploy affected terraform instances whenever the Express web server application code changes
Point 3 is where it falls apart. The problem is that terraform will only redeploy when infrastructure changes. This is an application code change. Thus, terraform is gonna shrug and say “everything looks unchanged to me”.
So my question is: what tool am I missing to automatically deploy new application code to the cloud using some github action?