The Golden Packer

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

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
T…


This content originally appeared on DEV Community and was authored by Anuvindh Sankaravilasam

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


This content originally appeared on DEV Community and was authored by Anuvindh Sankaravilasam


Print Share Comment Cite Upload Translate Updates
APA

Anuvindh Sankaravilasam | Sciencx (2022-03-19T10:40:16+00:00) The Golden Packer. Retrieved from https://www.scien.cx/2022/03/19/the-golden-packer/

MLA
" » The Golden Packer." Anuvindh Sankaravilasam | Sciencx - Saturday March 19, 2022, https://www.scien.cx/2022/03/19/the-golden-packer/
HARVARD
Anuvindh Sankaravilasam | Sciencx Saturday March 19, 2022 » The Golden Packer., viewed ,<https://www.scien.cx/2022/03/19/the-golden-packer/>
VANCOUVER
Anuvindh Sankaravilasam | Sciencx - » The Golden Packer. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/19/the-golden-packer/
CHICAGO
" » The Golden Packer." Anuvindh Sankaravilasam | Sciencx - Accessed . https://www.scien.cx/2022/03/19/the-golden-packer/
IEEE
" » The Golden Packer." Anuvindh Sankaravilasam | Sciencx [Online]. Available: https://www.scien.cx/2022/03/19/the-golden-packer/. [Accessed: ]
rf:citation
» The Golden Packer | Anuvindh Sankaravilasam | Sciencx | https://www.scien.cx/2022/03/19/the-golden-packer/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.