This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

The Golden Packer – DEV Community




DAY 22 – Constructing your Golden Picture utilizing packer for Terraform – Day Twenty two

Image tweetImage cover

100 days of Cloud on GitHubRead On iCTPro.co.nz – Learn on style-tricks.com


How you can Combine packer with Terraform?

  • Packer makes use of HCL language
  • You Construct picture
  • Then refence the picture



Lets obtain and set up packer



Challenge work house setting

  • I’m utilizing VSCode and WSL2 (ubuntu) for this mission for AWS setting.



Set up Packer

Obtain link
As we’re utilizing WSL 2 linux so i’m downloading through 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) principal"
sudo apt-get replace && sudo apt-get set up packer
Enter fullscreen mode

Exit fullscreen mode

Create a mission folder , am going to call it as packer.and cd into it.



Checking packer

Image Packer



Constructing a Picture

We’re going to construct a golden picture for NGINX

  • Make a file contained in the folder , identify it as nginx.pkr.hcl
  • Copy Paste under code, Its a HCL code for create a packer picture to run NGINX server on Ubuntu in ap-southeast-2 area.
variable "ami_id" {
    sort = string
    default = "ami-0b7dcd6e6fd797935"
}

locals {
    app_name = "nginx"
}

supply "amazon-ebs" "nginx" {
    ami_name = "my-nginx-server-${native.app_name}"
    instance_type = "t2.micro"
    area = "ap-southeast-2"
    source_ami = "${var.ami_id}"
    ssh_username = "ubuntu"
    tags = {
        Title = native.app_name
    }
}

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

        ]
    }
}
Enter fullscreen mode

Exit fullscreen mode

when you full the script , construct the picture utilizing packer

packer construct nginx.pkr.hcl 
Enter fullscreen mode

Exit fullscreen mode

Image packer build
Image build complete



Confirm the construct

  • Go to AWS console and choose bounce to EC2 dashboard.
  • Now beneath the photos space choose AMI.
  • It is possible for you to to see the packer construct AMI
    Image ami image



Deploy a brand new occasion with Terraform

  • Lets create a brand new file in similar folder , Title it as principal.tf
  • Copy this code
terraform {
  required_providers {
    aws = {
      supply  = "hashicorp/aws"
      model = "3.58.0"
    }
  }
}

supplier "aws" {
  profile = "default"
  area  = "ap-southeast-2"
}

knowledge "aws_ami" "packer_image" {
    filter {
            identify   = "identify"
            values = ["my-nginx-server-nginx"]
        }
    house owners = ["self"]
}

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

output "public_ip" {
  worth = aws_instance.my_server.public_ip
}
Enter fullscreen mode

Exit fullscreen mode

terraform init
Enter fullscreen mode

Exit fullscreen mode

terraform plan
Enter fullscreen mode

Exit fullscreen mode

if there isn’t a error

  • Deploy your infrastructure
terraform apply -auto-approve
Enter fullscreen mode

Exit fullscreen mode



Finest Apply

  • Construct your packer code
  • Publish to git , commit
  • construct picture
  • provision picture
  • Reference your picture
  • and provision infrastructure

🎉Congratulations🎉 you have got efficiently deployed an EC2 occasion with picture construct on packer.


✅Join with me on Twitter
🤝🏽Join with me on Linkedin
🧑🏼‍🤝‍🧑🏻 Learn extra put up on style-tricks.com or iCTPro.co.nz
💻 Join with me on GitHub

anuvindhs image



The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?