DEV Community

Ige Adetokunbo Temitayo for AWS Community Builders

Posted on • Edited on

6 2

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay