Skip to content

Learning Infrastructure Build Automation with HashiCorp Packer and automating with GitHub Actions

License

Notifications You must be signed in to change notification settings

NishkarshRaj/HashiCorp-Packer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HashiCorp Packer - Infrastructure Build Tool

Image

Why use Packer?

Configuration Management and Infrastructure-as-a-Service is trending because of the surge of remote deployments of services and their access over network to the consumers via Cloud or containers techniques. The biggest problem to infrastructure or pre-baked images is that they are written by development team and thus operations team are not too flexible to make changes to them due to high learning curve. Furthermore, it is difficult to automate infrastructure using pre-baked images.

  • Create identical machine images for multiple platforms from a single source configuration. Packer automates the creation of any type of machine image.
  • Multi-provider portability: Packer creates identical images which can be deployed on public cloud (AWS,GCP,Azure etc), private cloud (OpenStack etc.) or local desktop machines (Virtual Box, VWWare)
  • Fast: Entire infrastructure is configured and provisioned in seconds

What are the alternatives to Packer?

Configuration Management tools like Chef and Puppet are the alternatives but Packer is becoming more popular because:

  1. Easy to learn and use
  2. Automate IaaS
  3. Compatible with other tools and frameworks like Chef and Puppet

Key Features of Packer

  • Fast infrastructure deployment - provision infrastructure and configures its dependencies within seconds.

  • Multi-Provider Portability - identical images for all platforms

  • Improved Stability - configure dependency on image creation rather than configuration of infrastructure

  • Greater Testability - Easy to smoke test one machine and that guarentees it will work everywhere due to high portability.

Some Use-cases of Packer

  • Continuous Delivery - Lightweight, Portable, CMD driven. Can be used to create portable image to be configured by Ansible, Chef.

  • Dev-Prod Parity - Tries to reduce the differences between the various environments in software development lifecycle.

  • Appliance-Demo creation - Very suitable for alpha and beta testing as user environment can be replicated very easily.

Installation of Packer in Linux machines

  • Install Golang on your machine
$ apt install golang-go
  • Check version of golang for verifying successful installation
$ go version
  • Clone packer in your GOPATH
$ mkdir -p $(go env GOPATH)/src/github.com/hashicorp && cd $_
$ git clone https://github.com/hashicorp/packer.git
$ cd packer
  • Extract and install binaries using Make utility
$ make dev
  • Verify successful Packer installation
$ packer --version

packer

Direct installation of Packer for Debian based systems

$ apt-get upgrade && apt-get update
$ apt install packer
$ packer --version

Launching an AWS EC2 instance using Packer

  • Create a json script - example.json
{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": ""
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "us-east-1",
      "source_ami_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
          "root-device-type": "ebs"
        },
        "owners": ["099720109477"],
        "most_recent": true
      },
      "instance_type": "t2.micro",
      "ssh_username": "ubuntu",
      "ami_name": "packer-example {{timestamp}}"
    }
  ]
}
  • Validate the Packer Script
$ packer validate example.json

aws

aws

aws

aws

aws

aws

  • Create IAM User and generate tokens to be passed as variables to the script file
$ export AWS_ACCESS_KEY_ID=AKIAYNZB56UH67JQO5XJ 
$ export AWS_SECRET_ACCESS_KEY=fwQqvr7Fad60aSG/ekKjeNi5nYEC5RXUNEsmUYJl

export

  • Build the Packer script
$ packer build example.json

build

build

build

build

Automation HashiCorp Packer using GitHub Actions

  • Create a Dockefile in action-a/ folder to specify Packer installation

Image

  • Write the main configuration file of GitHub Actions that executes the above created Dockerfile

Image

  • Check for the current installation of Packer in GitHub Actions UI

Image

Image

  • Pass the AWS IAM Credentials as encrypted secrets to GitHub workflow

Image

Image

  • Modify the Dockerfile to build the packer for AWS example.json file
RUN ADD example.json /example.json
RUN packer build example.json

Note: You might get a build error stating there is no default VPC is the user you have authenticated has no direct VPC attached

Image

  • Go to AWS - VPC services and create a default VPC.

Image

  • Re-run the GitHub Actions workflow

Image

  • The workflow ran successfully and gives the AMI of the provisioned AWS EC2 instance.

Image

Image

  • Visit AWS - EC2 services to see the snapshot in execution

Image

About

Learning Infrastructure Build Automation with HashiCorp Packer and automating with GitHub Actions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages