Deploying AWS EC2 Instances with Terraform and GitHub Actions
Introduction:-
#AWS EC2 instances are widely used for running virtual machines in the cloud. #Terraform is a popular #infrastructure-as-code (IaC) tool that allows you to define and manage your infrastructure using code. GitHub Actions is a powerful automation tool that enables you to create workflows to automatically build, test, and deploy code changes in your #GitHub repositories. In this blog post, we will explore how to deploy #AWS EC2 instances using Terraform and automate the process using GitHub Actions.
Prerequisites: Before we begin, make sure you have the following prerequisites in place:
AWS account: You will need an AWS account with appropriate permissions to create EC2 instances.
GitHub account: You will need a GitHub account to create a GitHub repository and set up #GitHub Actions.
Terraform installed: You will need Terraform installed on your local machine or the environment where you plan to run the Terraform commands.
Step 1: Create a GitHub repository
Log in to your #GitHub account and navigate to the main page.
Click on the “New” button to create a new repository.
Enter a name for your repository and choose the desired settings (e.g., public or private).
Click on the “Create repository” button to create the repository.
Step 2: Clone the GitHub repository
Open a terminal on your local machine or the environment where you plan to run the Terraform commands.
Run the following command to clone the #GitHub repository to your local machine:
git clone <repository-url>
Make sure to replace <repository-url>
with the URL of the GitHub repository you created in step 1.
Step 3: Push the code to GitHub
Navigate to the folder where you have cloned the GitHub repository using the terminal.
Copy your #Terraform code to this folder.
Run the following commands to push the code to the GitHub repository:
git add .
git commit -m "Initial commit from mahiratechnology.com"
git push origin master
This will push the code to the GitHub repository that you created in step 1. Make sure the repository is set to private to ensure the security of your Terraform code and AWS credentials.
Step 4: Configure GitHub Actions workflow
Go to the GitHub repository where you pushed the code and navigate to the “Actions” tab.
Click on the “Set up a workflow yourself” or “New workflow” button.
Enter a name for the workflow, such as “Deploy EC2 with Terraform”.
In the editor, paste the following code for the workflow:
name: Deploy EC2 with Terraform
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Terraform
run: |
VERSION=$(curl --silent https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r .current_version)
DOWNLOAD_URL="https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_linux_amd64.zip"
curl -o terraform.zip ${DOWNLOAD_URL}
unzip terraform.zip
sudo mv terraform /usr/local/bin/
rm terraform.zip
- name: Configure AWS credentials
run: |
echo "$AWS_ACCESS_KEY_ID" > aws_access_key_id
echo "$AWS_SECRET_ACCESS_KEY" > aws_secret_access_key
aws configure set aws_access_key_id $(cat aws_access_key_id)
aws configure set aws_secret_access_key $(cat aws_secret_access_key)
aws configure set default.region ${{ secrets.AWS_REGION }}
rm aws_access_key_id aws_secret_access_key
- name: Run Terraform commands
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
terraform init
terraform validate
terraform plan -out=tfplan
terraform apply -auto-approve tfplan
This GitHub Actions workflow is triggered whenever a push event occurs on the “main” branch of your repository. It sets up Terraform, configures AWS credentials, and runs the necessary Terraform commands to deploy EC2 instances in AWS.
Note: Make sure to replace the placeholders like ${{
secrets.AWS
_REGION }}
, ${{
secrets.AWS
_ACCESS_KEY_ID }}
, and ${{
secrets.AWS
_SECRET_ACCESS_KEY }}
with the actual values for your AWS region and credentials, which you will configure in the subsequent steps.
Step 5: Configure AWS credentials
Go to the AWS Management Console and navigate to the “IAM” (Identity and Access Management) service.
Click on “Users” in the left-hand menu and then click on the “Add user” button.
Enter a username and select “Programmatic access” as the access type.
Click on the “Next: Permissions” button.
Attach the necessary permissions to the user, such as “AmazonEC2FullAccess” or a custom policy that grants EC2 related permissions.
Click on the “Next: Tags” button (optional) and then click on the “Next: Review” button.
Review the user details and click on the “Create user” button.
Note down the “Access Key” and “Secret Access Key” for the user, as you will need these to configure the #GitHub Actions workflow.
Step 6: Configure GitHub repository secrets
Go back to the GitHub repository where you created the workflow and navigate to the “Settings” tab.
In the left-hand menu, click on “Secrets”.
Click on the “New repository secret” button.
Enter a name for the secret, such as “AWS_ACCESS_KEY_ID”, and enter the corresponding value obtained from step 5.
Click on the “Add secret” button.
Repeat the process to add another secret for “AWS_SECRET_ACCESS_KEY” with its corresponding value obtained from step 5.
Step 7: Update the GitHub Actions workflow
Go back to the GitHub Actions workflow that you created in step 4 and click on the “Edit” button.
Update the “env” section with the correct AWS region, if necessary.
Update the “steps” section with the Terraform commands you want to run, such as “terraform init”, “terraform plan”, and “terraform apply”.
Make sure to set the working directory to the location where your Terraform code is stored in the repository.
Step 8: Trigger the GitHub Actions workflow
Commit and push any changes to your Terraform code in the GitHub repository.
Go to the “Actions” tab in the GitHub repository and you should see the workflow you created.
Click on the “Run workflow” button and select the appropriate inputs, such as the “tfpath” input for the Terraform file path, if you defined it in the workflow.
Click on the “Run workflow” button to trigger the workflow.
Step 9: Monitor the GitHub Actions workflow
Once the workflow is triggered, you can monitor its progress in the “Actions” tab.
The workflow will run the Terraform commands as defined in the workflow and deploy the EC2 instances in AWS.
If there are any errors or issues, you can check the logs and troubleshoot accordingly.
Once the workflow completes successfully, you should be able to see the deployed EC2 instances in your AWS account.
Conclusion:-
In this blog post, we explored how to deploy #AWS EC2 instances using Terraform and automate the process using #GitHub Actions. By following the steps outlined above, you can set up a seamless and automated workflow for deploying EC2 instances with #Terraform in your #AWS environment. This allows for efficient and repeatable infrastructure deployments, making it easier to manage your cloud resources. Happy automating!