Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 19, 2022 10:40 am GMT

The Golden Packer

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

Image tweetImage 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

.ltag__user__id__637154 .follow-action-button { background-color: #141D2B !important; color: #9FEF00 !important; border-color: #141D2B !important; }
anuvindhs image

Original Link: https://dev.to/aws-builders/the-golden-packer-5k4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To