Terraform - Create EC2 resource using GitHub Actions
Below is a complete enterprise-level solution with detailed steps to create an EC2 instance in the Ohio region (default subnet) using Terraform. It incorporates best practices such as remote state management, separate provider configuration, and reusable modules.
We can clone the GitHub Repo to have the code.
Initially, we will create the Terraform files and test them, later will improve by automating using GitHub Actions workflow.
Steps to automate the deployment using GitHub actions are provided in the same article.
1. Prerequisites
Install Terraform: Download and install Terraform from Terraform's official website.
Install AWS CLI: Install and configure it using
aws configure
to set up your credentials.IAM User with Permissions: Ensure the IAM user has sufficient permissions (EC2, VPC, S3, DynamoDB, IAM).
Create an S3 Bucket and DynamoDB Table for Remote State:
S3 Bucket:
terraform-state-enterprise
DynamoDB Table:
terraform-lock-table
with a primary keyLockID
of typeString
.
2. Folder Structure
Organize your Terraform files in a project structure like in GitHub Repo or we can clone the #repo
note the .github folder which has GitHub action yml file and module folder.
3. Backend Configuration
Create the backend.tf
file in the root directory for remote state management
Steps to Create the Backend
Create an S3 bucket (
terraform-state-enterprise
) in the Ohio region (us-east-2
).Create a DynamoDB table (
terraform-lock-table
) with the primary keyLockID
.
4. Provider Configuration
configure providers.tf
5. Create Reusable EC2 Module
In modules/ec2
, create the following files:
The below link has the code
6. Main Terraform Configuration
In the root folder, create these files:
7. Deploy the Infrastructure
Step-by-Step Commands
Navigate to the Project Directory:
cd terraform-ec2
Initialize Terraform:
This downloads the AWS provider and configures the backend:
terraform init
Validate the Configuration:
Ensure there are no syntax errors:
terraform validate
Generate an Execution Plan:
Review the planned infrastructure changes:
terraform plan
Apply the Configuration:
Deploy the EC2 instance:
terraform apply
Type yes to confirm
8. Verify the Deployment
AWS Management Console:
Navigate to the EC2 dashboard in the Ohio region.
Confirm the instance is running with the specified tags and settings.
9. Cleanup
To destroy the resources and avoid incurring charges:
terraform destroy
10. Enterprise-Level Practices
Remote State Management:
- Used S3 for state storage and DynamoDB for locking.
Modularity:
- Created a reusable EC2 module.
Version Control:
- Commit this code to a Git repository for collaboration.
Environment Tagging:
- Added tags like
Environment
to distinguish resources.
- Added tags like
Dynamic Variables:
- Used variables for region, instance type, and tags for flexibility.
This approach ensures a robust, scalable, and enterprise-ready solution for managing AWS infrastructure with Terraform.
1. Enterprise-Level GitHub Actions Workflow for Terraform
Automate the deployment of the EC2 infrastructure with the following considerations:
CI/CD Workflow: Automate Terraform commands (plan, apply, destroy).
Secure Secrets Management: Store AWS credentials securely.
State Locking: Ensure backend state locking is intact.
Dynamic Environments: Use branch-based environments for staging and production.
2. Prerequisites
GitHub Repository:
- The Terraform codebase is already stored in GitHub.
IAM Role for GitHub Actions:
- Create an IAM user or role with permissions for S3, DynamoDB, and EC2.
GitHub Secrets:
Store sensitive information as GitHub secrets:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION
3. Folder Structure
4. GitHub Actions Workflow
Create a file .github/workflows/terraform.yml
, this file should be in root folder.
5. Testing the Workflow
Step 1: Commit and Push Changes
What it means:
Once you’ve configured your Terraform code and GitHub Actions workflow (
terraform.yml
), you commit these changes to your repository and push them to themain
branch.This action triggers the GitHub Actions workflow as it’s configured to run on pushes to
main
.
How to do it:
Step 2: Verify Output
once What it means:
After the workflow runs, you check the logs in the GitHub Actions tab of your repository to verify successful execution.
Confirm that the EC2 instance is created in AWS by logging into the AWS Management Console and checking the EC2 dashboard.
How to do it:
Go to your GitHub repository.
Click on Actions > Select the latest workflow run.
Review logs for steps like
Terraform Init
,Plan
, andApply
.
-
once we click on actions
-
we can view the workflows, we can click on it and view the logs for the workflow.
-
The workflow which we created we can run it manually and select the destroy to delete the created instance.
select the required options to Run Workflow