DevOps for Beginners: Automating Infrastructure Provisioning with Terraform and AWS
In the world of DevOps, infrastructure as code (IaC) is a fundamental concept. It allows you to manage and provision your infrastructure using code, enabling automation and scalability. In this guide, we’ll walk through a beginner-friendly DevOps project that leverages Terraform to provision cloud infrastructure on AWS.
By the end of this tutorial, you’ll know how to:
Set up Terraform
Use Terraform to provision an EC2 instance on AWS
Automate infrastructure deployment using CI/CD with GitHub Actions
Let’s get started!
Prerequisites:
Before diving into the project, ensure you have the following:
An AWS account (sign up for the [AWS Free Tier] (aws.amazon.com/free/))
Basic knowledge of AWS EC2
Terraform installed on your local machine
A text editor (e.g., VSCode)
Step 1: Set Up Terraform
Objective: Install Terraform and set up a basic configuration file.
Install Terraform:
Follow the official installation guide to install Terraform on your machine.
Create a New Project Directory:
Create a directory for your project and navigate into it:
mkdir terraform-aws-project
cd terraform-aws-project
Configure Your AWS Credentials:
Install the AWS CLI if you haven’t already and configure your credentials:
aws configure
You’ll need your AWS access key, secret key, and region (e.g.,
us-east-1
).Write Your First Terraform Configuration File:
Create a file called
main.tf
and add the following code to provision an EC2 instance:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with a valid AMI ID for your region
instance_type = "t2.micro"
}
- This file defines an EC2 instance with the specified AMI and instance type.
Step 2: Initialize and Apply Terraform
Objective: Use Terraform to provision the EC2 instance on AWS.
Initialize Terraform:
Run the following command to initialize Terraform. This will download the necessary provider plugins: terraform init
Apply the Terraform Configuration:
To provision the infrastructure, run: terraform apply
Terraform will show you an execution plan and ask for confirmation. Type
yes
to proceed.After a few moments, Terraform will provision the EC2 instance on AWS.
Verify the EC2 Instance:
Log in to the AWS Management Console and navigate to the EC2 dashboard. You should see the instance running.
Step 3: Automate Infrastructure Deployment with GitHub Actions
Objective: Use GitHub Actions to automate the deployment of your infrastructure using Terraform.
Create a GitHub Repository:
Create a new repository on GitHub and clone it to your local machine.
Add your Terraform configuration files (
main.tf
) to the repository.Commit and push your code to GitHub:
git add .
git commit -m "Initial Terraform configuration"
git push origin main
Set Up GitHub Actions:
In your repository, create a
.github/workflows
directory.Inside that directory, create a file named
terraform.yml
with the following content:
name: Terraform
on:
push:
branches:
- main
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
- name: Initialize Terraform
run: terraform init
- name: Apply Terraform
run: terraform apply -auto-approve
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
This GitHub Actions workflow will trigger on every push to the
main
branch, initialize Terraform, and apply the configuration.Store AWS Credentials in GitHub Secrets:
Go to your repository settings on GitHub, and under “Secrets and variables”, add two new secrets:
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
. These should be the credentials for your AWS account.Test the Workflow:
Push your changes to the
main
branch. GitHub Actions should automatically run the workflow, provisioning the infrastructure on AWS.You can monitor the progress of the workflow in the “Actions” tab of your repository.
Step 4: Destroy Infrastructure
Objective: Clean up the AWS resources by destroying the infrastructure when no longer needed.
Destroy the Resources:
In your local environment, run the following command to destroy the resources provisioned by Terraform: terraform destroy
This will remove the EC2 instance and any associated resources.
Automate the Teardown:
You can extend your GitHub Actions workflow to include a
destroy
job that runs when you need to tear down the infrastructure.
Conclusion
Congratulations! You’ve completed your first DevOps project using Terraform and AWS. You’ve learned how to set up and manage infrastructure as code, automate deployments with GitHub Actions, and efficiently provision and destroy cloud resources.
This project introduces you to the core principles of DevOps: automation, version control, and continuous delivery. As you grow in your DevOps journey, you’ll explore more complex infrastructures, integrate other tools like Kubernetes, and dive deeper into monitoring and security.
Feel free to share your experience and ask questions in the comments. Happy coding!