How to turn on AWS EC2 using Github Actions

Photo by Jakob Owens on Unsplash

How to turn on AWS EC2 using Github Actions

Instructions in writing a CI/CD GitHub Actions workflow to turn on a Amazon Web Services EC2 Virtual Machine

Below we are going to write a Github Action that turns on an AWS EC2 server

Create a GitHub account

  • Create a new repository

  • Setup file for workflow

    • Folder named .github
    • Folder in .github called workflows
    • Create a file called turnOnAWSEC2.yml
    • See Link for more info

Add code to workflow (turnOnAWSEC2.yml) file

  • Please see comments starting with '#' explaining what each bit of the file does
  • This code is set up only to run when you click action in the repository
  • Github workflows servers that are used to run the workflows have AWS CLI commands built-in, so all you need to do is run the AWS CLI command See this link for more information
name: startMagent2DevEC2

# Controls when the action will run. 
# This code is set up only to run when you click action in the repository, If you require to run on a push or cron edit below
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  # push:
  #  branches: [ main ]
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:

    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Below this are each of the steps that are run
    steps:

     # Name of step
      - name: Start AWS EC2
        # Run AWS Command on the GitHub Hosted runner which starts the instance using AWS authentication stored in GitHub Secrets (see below how to add)
        run: |
          aws ec2 start-instances --instance-ids ${{secrets.AWS_EC2_INSTANCE_ID }}
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}

Add GitHub Repository Secrets

  • In the above workflow, we have many secrets to add eg. ${{ secrets.AWS_ACCESS_KEY_ID }}

    • Secrets are secret values stored so that GitHub workflow can use them without exposing them in your workflow file
  • Create AWS Keys

    • Log into your AWS portal that has high privileges to create accounts
    • Create a new IAM user as its AWS best practice to keep your logins separate from your root account See this link
    • Logout and login as your new IAM user
    • Click your cloud name
    • Click 'My Security Credentials' image.png
    • Click create
    • Here you will have 2 keys that you will need to add to your repository. Keep this page open or copy these to add to below
  • To add these AWS keys to your GitHub Repository

    • Click settings in your repository
    • Then secrets
    • Add repository secret named AWS_ACCESS_KEY_ID and add the key from the AWS portal you generated above
    • Create another secret named AWS_SECRET_ACCESS_KEY and add relevant keys from your keys created in your AWS Account
  • Create a secret for your instance id ie. ${{secrets.AWS_EC2_INSTANCE_ID }}

    • Go to your AWS Console
    • Select Services > EC2
    • Instance ID is the identifier of what EC2 VM as below screenshot
    • Copy this ID and add it to your GitHub Secrets
    • Click settings in your repository
    • Then secrets
    • Add repository secret named AWS_EC2_INSTANCE_ID and add instance id

image.png

  • Create a secret for your AWS region where your AWS EC2 is running id ie. ${{ secrets.AWS_REGION }}
  • Go to your AWS Console
    • Select Services > EC2
    • Region is in the right-hand corner as below screenshot
    • Make sure you match your region with code in this link
    • Add to your GitHub Secrets
    • Click settings in your repository
    • Then secrets
    • Add repository secret named AWS_REGION and add AWS region

image.png

Commit and push code if you haven't already

Run your GitHub action

  • In your repo
  • Click Actions
  • Select Action to run
  • Click run the workflow

If successful you should have a complete success workflow and your EC2 should be switched on in AWS.

Don't forget to turn off your EC2 if you don't want it to run.

Shameless Plugs

Credits

Feel free to comment if you get stuck or have questions or feedback✌️

Happy Coding 🙂

Az 👨🏾‍💻

Did you find this article valuable?

Support Asrin Dayananda by becoming a sponsor. Any amount is appreciated!