Infrastructure as Code Lab: From Terraform to Serverless

By: Yasas Harshana | Date: 2025-07-07

Infrastructure as Code Lab: From Terraform to Serverless

In this tutorial, we'll walk through a complete Infrastructure as Code (IaC) lab setup using a suite of popular DevOps tools. You'll learn how tools like Terraform, Ansible, Docker, Helm, and the Serverless Framework work together to provision, configure, deploy, and scale applications in the cloud.

Note: This is a high-level walkthrough, ideal for beginners exploring how modern infrastructure automation fits together.


🚀 What You'll Build

  • AWS cloud infrastructure with Terraform
  • Kubernetes cluster with Ansible (via Kubespray)
  • Containerized app using Docker
  • Application deployed with Helm
  • Serverless function via the Serverless Framework

🧰 Prerequisites

  • Mac/Linux system or Windows with WSL2
  • AWS account with billing enabled
  • SSH key pair (locally generated)

🔨 Step 1: Provision Infrastructure with Terraform

Terraform helps us define and spin up cloud resources using code.

Highlights: - Uses Kubespray's Terraform config (contrib/terraform/aws) - Launches VPC, bastion, master & worker nodes - Scalable by editing terraform.tfvars

```hcl

terraform.tfvars

aws_region = "us-west-2" cluster_name = "k8s-lab" bastion_size = "t2.micro" number_of_workers = 4 ```

Commands: bash terraform validate # check for syntax errors terraform plan # preview changes terraform apply # create/update infrastructure


⚙️ Step 2: Configure Kubernetes with Ansible

Ansible installs and configures Kubernetes components.

  • Uses cluster.yml and scale.yml playbooks from Kubespray
  • Leverages the inventory file generated by Terraform

Run Playbooks: bash ansible-playbook -i inventory/hosts.yaml cluster.yml # full install ansible-playbook -i inventory/hosts.yaml scale.yml # add new nodes


🐳 Step 3: Containerize the App with Docker

Containerize a sample Go-based Word Cloud Generator.

Dockerfile: Dockerfile FROM alpine:3.18 COPY word-cloud-generator /opt/ EXPOSE 8888 ENTRYPOINT ["/opt/word-cloud-generator"]

Build & Run: bash make docker-build make docker-run # binds to localhost:8888

Push to Docker Hub: bash make docker-push


📦 Step 4: Deploy with Helm

Use Helm to deploy your containerized app to Kubernetes.

Steps: - Create a custom Helm chart: bash helm create wordcloud - Edit values.yaml to define your app image and settings: yaml replicaCount: 3 image: repository: your-dockerhub-username/wordcloud tag: latest-amd64 service: type: LoadBalancer port: 8888

Deploy App: bash helm install wordcloud ./wordcloud -f values.yaml

Verify: bash kubectl get pods kubectl get svc


⚡ Step 5: Go Serverless

Deploy a Node.js API using the Serverless Framework.

Install & Create App: bash npm install -g serverless serverless create --template aws-nodejs --path infra-as-code-demo cd infra-as-code-demo

Deploy: bash serverless deploy

Edit handler.js:

js module.exports.hello = async () => { return { statusCode: 200, body: JSON.stringify({ message: "Hi LinkedIn Learning!" }) }; };

Redeploy: bash serverless deploy


✅ Final Thoughts

By combining these tools: - You gain full automation over your infrastructure lifecycle - You reduce manual errors and increase repeatability - You leverage scalable, reusable patterns

🔁 Always test in dev before deploying to production. The IaC ecosystem thrives on community—reuse responsibly and contribute back!