DEV Community

Ige Adetokunbo Temitayo for AWS Community Builders

Posted on • Updated on

Using GitHub Actions to build packer AMI on AWS

GitHub Actions is a very cool tool for automating CI/CD pipeline workflows or any routine task. Once the code resides in Github, automating tasks from using Github actions is achievable.

Alt Text

Github Actions allows Engineers to create a very simple workflow to automate code compilation and deployment. Github Actions is very easy to use and it makes deployment to production very easy and interesting.

Actions are defined in YAML files, which allows pipeline workflow to be triggered using any GitHub events like on creation of Pull Requests, on code commits, and much more

I recently developed, deployed my first github Action and published the action to GitHub Actions Marketplace. The Action build packer images on AWS. I wanted an action to build simple packer images and i decided to pick up the challenge.

Let's get started with the GitHub Action

I will be describing how to use GitHub Action to build packer images on AWS.

Step 01: Navigate to the repository where you wish to implement the GitHub Action.

Step 02: Create a file packer-build.yml in the parent directory. The file will be created in .github/workflows/packer-build.yaml. See screenshot below.

How to create GitHub Actions

Step 03: Copy and paste the script below in thepacker.yaml file.

name: Run packer build on a template file

on:
  push:
    branches:
        - 'master'
jobs:
  packer_build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Packer build
        uses: ExitoLab/packer_build_action_aws@v0.2.10
        with:
          templateFile: 'ami.json'
          workingDir: 'ami'
          varFile: 'variables.json'
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-east-1
Enter fullscreen mode Exit fullscreen mode

Code Explanation: The actions work with the GitHub Event trigger which is push to master branch. The input parameters are the; working directory and template file. The workingDir is defined as the directory where the packer template and var file reside. The templateFile contains the packer template file for building packer AMI. The access_key and secret_key are used for authenticating to AWS will be stored in GitHub secrets.

Step 04: Add your secrets AWS_ACCESS_KEY and AWS_SECRET_KEY in the Github secrets. Under your repository name, click Settings. In the left sidebar, click Secrets. See image below

Adding secrets in GitHub for GitHub Actions

Step 05: Kindly see below a complete example of the AMI template. This template installs Jenkins and other software. Also, check the complete workflow for building the AMI.

In conclusion, I hope to build more GitHub Actions for executing routine tasks and I totally enjoyed working on my first GitHub Action. It was a very exciting experience.

Top comments (0)