Using GitHub Actions to build packer AMI on AWS
Tags: packer, aws, github, awscommunitybuilders
Published: 2021-07-13 21:56:22

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wg453xocbjc67ocfub4.png)

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](https://github.com/marketplace?type=actions). The Action [build packer images](https://github.com/marketplace/actions/packer-build-on-aws) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iypu4qgyrsdm9zamv1ek.png)

**Step 03:** Copy and paste the script below in the`packer.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
```
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb09r1iay4hny6if9i19.png)

**Step 05:** Kindly see below a complete [example of the AMI template](https://github.com/ExitoLab/example-packer-jenkins). This template installs Jenkins and other software. Also, check the [complete workflow](https://github.com/ExitoLab/example-packer-jenkins/blob/master/.github/workflows/packer.yaml) 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.