Skip to main content

Command Palette

Search for a command to run...

Creating an AWS EC2 Instance Using GitHub Actions (Infrastructure as Code)

Updated
4 min read
Creating an AWS EC2 Instance Using GitHub Actions (Infrastructure as Code)

Introduction

Cloud computing has transformed the way applications are deployed and managed. Instead of manually creating resources through the AWS Management Console, we can automate infrastructure provisioning using code. This approach is known as Infrastructure as Code (IaC).

In this article, we will learn how to create an AWS EC2 instance automatically using GitHub Actions. By integrating GitHub with AWS, we can provision cloud resources directly from our repository, making deployments faster, more reliable, and repeatable.

What is GitHub Actions?

GitHub Actions is a Continuous Integration and Continuous Deployment (CI/CD) service provided by GitHub. It allows developers to automate workflows such as:

  • Building applications

  • Running tests

  • Deploying applications

  • Managing cloud infrastructure

Using GitHub Actions, we can execute AWS commands whenever code is pushed to a repository.

Prerequisites

Before starting, ensure you have:

  • An AWS Account

  • A GitHub Account

  • AWS Access Key ID

  • AWS Secret Access Key

  • A GitHub Repository

  • Basic knowledge of AWS EC2 and GitHub

Step 1: Create an IAM User in AWS

For security reasons, avoid using the root account.

  1. Login to AWS Console.

  2. Navigate to IAM.

  3. Create a new user.

  4. Attach the following permissions:

    • AmazonEC2FullAccess
  5. Create an Access Key.

  6. Save:

    • Access Key ID

    • Secret Access Key

These credentials will be used by GitHub Actions.

Step 2: Create a GitHub Repository

Create a new repository on GitHub.

Example repository structure:

aws-ec2-automation/
│
├── .github/
│   └── workflows/
│       └── ec2-create.yml
│
└── README.md

Step 3: Configure GitHub Secrets

In your GitHub repository:

  1. Go to Settings.

  2. Select Secrets and Variables.

  3. Click Actions.

  4. Add the following secrets:

Secret Name Value
AWS_ACCESS_KEY_ID Your Access Key
AWS_SECRET_ACCESS_KEY Your Secret Key

These secrets are securely stored and accessed during workflow execution.

Step 4: Create GitHub Actions Workflow

Create the file:

.github/workflows/ec2-create.yml

Add the following code:

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: ap-south-1

    - name: Create EC2 Instance
      run: |
        aws ec2 run-instances \
        --image-id ami-0f58b397bc5c1f2e8 \
        --count 1 \
        --instance-type t2.micro \
        --key-name my-keypair \
        --security-group-ids sg-xxxxxxxx \
        --subnet-id subnet-xxxxxxxx

Understanding the Workflow

workflow_dispatch

on:
  workflow_dispatch:

This allows us to manually trigger the workflow from GitHub.

Configure AWS Credentials

uses: aws-actions/configure-aws-credentials@v4

This action authenticates GitHub with AWS using the stored secrets.

Create EC2 Instance

aws ec2 run-instances

This AWS CLI command launches a new EC2 instance.

Important parameters:

Parameter Description
--image-id Amazon Machine Image (AMI)
--instance-type EC2 size
--count Number of instances
--key-name SSH Key Pair
--security-group-ids Firewall rules
--subnet-id Network subnet

Step 5: Commit and Push Code

Run the following commands:

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

Step 6: Execute the Workflow

  1. Open GitHub Repository.

  2. Click Actions.

  3. Select Create EC2 Instance workflow.

  4. Click Run Workflow.

  5. Wait for execution to complete.

GitHub Actions will now connect to AWS and launch the EC2 instance automatically.

Step 7: Verify in AWS

  1. Open AWS Console.

  2. Navigate to EC2 Dashboard.

  3. Click Instances.

  4. Verify that your EC2 instance has been created successfully.

You should see a running instance with the specified configuration.

Benefits of Using GitHub Actions for AWS Automation

Automation

No need to manually create resources.

Consistency

Every deployment follows the same process.

Faster Provisioning

Infrastructure can be created within minutes.

Version Control

Infrastructure code is stored in GitHub and can be tracked easily.

Collaboration

Teams can review and manage infrastructure changes together.

Best Practices

  • Never store AWS credentials directly in code.

  • Use GitHub Secrets for sensitive information.

  • Follow the principle of least privilege when assigning IAM permissions.

  • Monitor workflow execution logs.

  • Use Infrastructure as Code tools such as Terraform for larger environments.

Conclusion

Automating AWS EC2 instance creation using GitHub Actions is a powerful way to implement Infrastructure as Code. By integrating GitHub with AWS, developers can provision cloud resources efficiently, reduce manual effort, and improve deployment consistency.

As you continue your cloud journey, you can extend this workflow to create VPCs, Security Groups, Load Balancers, RDS databases, and complete application environments automatically.

Happy Learning and Happy Automating!