Skip to main content

Command Palette

Search for a command to run...

Creating an AWS EC2 Instance Using GitHub Actions

Updated
3 min read
Creating an AWS EC2 Instance Using GitHub Actions
H
Aspiring software engineer with a strong interest in AWS, cloud computing, and web technologies. Focused on learning new skills, building practical projects, and growing as a technology professional.

Introduction

Infrastructure as Code (IaC) helps automate cloud resource creation. Instead of manually creating AWS resources through the AWS Console, we can use GitHub Actions to provision infrastructure automatically. In this tutorial, we will create an AWS EC2 instance using GitHub Actions and AWS CLI.

Prerequisites

Before starting, ensure you have:

  • An AWS account: AWS

  • A GitHub account: GitHub

  • An IAM user with EC2 permissions

  • A GitHub repository

Step 1: Create an IAM User

Log in to AWS and open the IAM service. Create a new IAM user with programmatic access. Attach the AmazonEC2FullAccess policy or a custom policy with the required EC2 permissions. After creating the user, save the Access Key ID and Secret Access Key because they will be needed in GitHub.

Step 2: Configure GitHub Secrets

Open your GitHub repository and navigate to:

Settings → Secrets and Variables → Actions

Create the following secrets:

  • AWS_ACCESS_KEY_ID – Your AWS Access Key

  • AWS_SECRET_ACCESS_KEY – Your AWS Secret Key

  • AWS_REGION – AWS Region (for example, ap-south-1)

These secrets allow GitHub Actions to securely authenticate with AWS.

Step 3: Create the GitHub Actions Workflow

Inside your repository, create the file:

.github/workflows/create-ec2.yml

Add the following workflow:

name: Create EC2 Instance

on:
  workflow_dispatch:

jobs:
  create-ec2:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Create EC2 Instance
        run: |
          aws ec2 run-instances \
          --image-id ami-0f58b397bc5c1f2e8 \
          --instance-type t2.micro \
          --count 1

This workflow authenticates with AWS and launches a new EC2 instance when executed.

Step 4: Commit and Push the Workflow

Run the following commands:

git add .
git commit -m "Add EC2 creation workflow"
git push origin main

After pushing the code, GitHub will store the workflow in your repository.

Step 5: Run the Workflow

Open your repository in GitHub.

Navigate to:

Actions → Create EC2 Instance → Run Workflow

Click Run Workflow.

GitHub Actions will:

  • Authenticate with AWS

  • Execute the AWS CLI command

  • Create a new EC2 instance

Step 6: Verify the EC2 Instance

Open the EC2 dashboard:

Amazon EC2 Console

You should see the newly created instance listed under Instances.

Security Best Practices

  • Store credentials only in GitHub Secrets.

  • Use the least-privilege IAM permissions possible.

  • Rotate AWS access keys regularly.

  • Restrict security groups to trusted IP addresses.

  • Avoid hardcoding credentials in code repositories.

Conclusion

GitHub Actions provides a simple way to automate AWS infrastructure provisioning. By integrating GitHub Actions with AWS CLI, you can create EC2 instances automatically and build powerful CI/CD workflows. For larger projects, tools such as Terraform and AWS CloudFormation are commonly used to manage infrastructure at scale.

Tags: #AWS #EC2 #GitHubActions #DevOps #CloudComputing #Automation #InfrastructureAsCode #CICD #Linux #AWSCloud