Blog Details

How to Build your Golden Image using packer for Terraform– 100 days of Cloud: Day 22

+1
0
+1
0
+1
0
+1
0
+1
0

DAY 22 – Building your Golden Image using packer for Terraform – Day Twenty two

Image tweet
Image cover

100 days of Cloud on GitHub – Read On iCTPro.co.nz – Read on Dev.to


How to Integrate packer with Terraform?

  • Packer uses HCL language
  • You Build image
  • Then refence the image

Lets download and install packer

Project work space environment

  • I am using VSCode and WSL2 (ubuntu) for this project for AWS environment.

Install Packer

Download link
As we are using WSL 2 linux so i am downloading via bash script

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install packer

Create a project folder , am going to name it as packer.and cd into it.

Checking packer

Image Packer

Building a Image

We are going to build a golden image for NGINX

  • Make a file inside the folder , name it as nginx.pkr.hcl
  • Copy Paste below code, Its a HCL code for create a packer image to run NGINX server on Ubuntu in ap-southeast-2 region.
variable "ami_id" {
    type = string
    default = "ami-0b7dcd6e6fd797935"
}

locals {
    app_name = "nginx"
}

source "amazon-ebs" "nginx" {
    ami_name = "my-nginx-server-${local.app_name}"
    instance_type = "t2.micro"
    region = "ap-southeast-2"
    source_ami = "${var.ami_id}"
    ssh_username = "ubuntu"
    tags = {
        Name = local.app_name
    }
}

build {
    sources = ["source.amazon-ebs.nginx"]
    provisioner "shell"  {
        inline = [
            "sudo apt install nginx -y",
            "sudo systemctl enable nginx",
            "sudo systemctl start nginx"

        ]
    }
}

once you complete the script , build the image using packer

packer build nginx.pkr.hcl 
Image packer build
Image build complete

Verify the build

  • Go to AWS console and select jump to EC2 dashboard.
  • Now under the images area select AMI.
  • You will be able to see the packer build AMIImage ami image

Deploy a new instance with Terraform

  • Lets create a new file in same folder , Name it as main.tf
  • Copy this code
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.58.0"
    }
  }
}

provider "aws" {
  profile = "default"
  region  = "ap-southeast-2"
}

data "aws_ami" "packer_image" {
    filter {
            name   = "name"
            values = ["my-nginx-server-nginx"]
        }
    owners = ["self"]
}

resource "aws_instance" "my_server" {
  ami           = data.aws_ami.packer_image.id
  instance_type = "t2.micro"
    tags = {
        Name = "Server-nginx-Packer"
    }
}

output "public_ip" {
  value = aws_instance.my_server.public_ip
}
  • Initiate terraform
terraform init
  • Plan the deployment
terraform plan

if there is no error

  • Deploy your infrastructure
terraform apply -auto-approve

Best Practice

  • Build your packer code
  • Publish to git , commit
  • build image
  • provision image
  • Reference your image
  • and provision infrastructure

🎉Congratulations🎉 you have successfully deployed an EC2 instance with image build on packer.


✅Connect with me on Twitter
🤝🏽Connect with me on Linkedin
🧑🏼‍🤝‍🧑🏻 Read more post on dev.to or iCTPro.co.nz
💻 Connect with me on GitHub