{"id":347,"date":"2023-01-05T01:43:00","date_gmt":"2023-01-05T01:43:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=347"},"modified":"2023-01-05T01:43:00","modified_gmt":"2023-01-05T01:43:00","slug":"packer-tutorial-for-beginners","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=347","title":{"rendered":"How to Connect to Cloudways Server [SSH &#038; SFTP]"},"content":{"rendered":"<p>Packer is an <strong>open-source VM image creation tool <\/strong>from <a href=\"https:\/\/www.hashicorp.com\/?ref=devopscube.com\" rel=\"noreferrer noopener\">Hashicorp<\/a>. It helps you automate the process of Virtual machine image creation on the cloud and on-prem virtualized environments.<\/p>\n<p>To put it simply,  whatever manual steps you perform to create a Virtual machine image can be <strong>automated through a simple Packer config template<\/strong>. You declare the state of the VM image you need, and Packer builds it for you.<\/p>\n<h2 id=\"packer-use-cases\">Packer Use Cases<\/h2>\n<p>The following are the main use cases for Packer.<\/p>\n<ol>\n<li><strong>Golden Image Creation:<\/strong> With packer, you can template the configurations required for a golden VM image that can be used across organizations.<\/li>\n<li><strong>Monthly VM Patching: <\/strong>You can integrate Packer into your monthly VM image patching pipeline.<\/li>\n<li><strong>Immutable Infrastructure:<\/strong> If you want to create an <a href=\"https:\/\/devopscube.com\/immutable-infrastructure\/\" rel=\"noreferrer noopener\">immutable infrastructure <\/a>using VM images as a deployable artifact, you can use Packer in your CI\/CD pipeline.<\/li>\n<\/ol>\n<p>Basically, all the above use cases are part of the <strong>CI pipelines or IaaC code pipelines<\/strong> where every Packer template is version controlled. Even the <strong>packer template development or update<\/strong> should go through all the CI tests before getting deployed in a project environment.<\/p>\n<h2 id=\"packer-tutorial-for-beginners\">Packer Tutorial For Beginners<\/h2>\n<p>In this Packer beginner tutorial, you will learn the following.<\/p>\n<ol>\n<li>The basic building blocks of Packer.<\/li>\n<li>Packer Workflow<\/li>\n<li>Installing Packer<\/li>\n<li>Understanding Packer HCL template.<\/li>\n<li>Creating an AWS AMI using Packer<\/li>\n<\/ol>\n<p>Let&#8217;s get started with the Packer setup.<\/p>\n<h2 id=\"install-packer\">Install Packer<\/h2>\n<p>You can have Packer installed on your local workstation or on a cloud instance. In the actual project, the Packer installation would be part of the <a href=\"https:\/\/devopscube.com\/jenkins-2-tutorials-getting-started-guide\/\" rel=\"noreferrer noopener\">Jenkins<\/a> agent or any tooling which is part of the CI\/CD process.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b> If you are setting up Packer on your workstation for debugging or testing purposes, you need the AWS access and secret keys for Packer to interact with AWS services. Make sure you have the AWS access keys set on the file <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">~\/.aws\/credentials<\/code>. It is not mandatory to have a credentials file. You can pass the AWS credentials using the packer variable declaration as well.<\/div>\n<\/div>\n<p><strong>Step 1:<\/strong> Download the latest Packer executable from the Packer downloads page. <a href=\"https:\/\/www.packer.io\/downloads.html?ref=devopscube.com\" rel=\"noreferrer noopener\">https:\/\/www.packer.io\/downloads.html<\/a>. It is available for Windows, Linux, and other Unix platforms.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-314.png\" class=\"kg-image\" alt=\"packer downloads page\" loading=\"lazy\" width=\"593\" height=\"341\"><\/figure>\n<p>For Linux, you can install Packer from the respective package managers or you can get the download link from the download button and download it using <code>wget<\/code>.<\/p>\n<p>For example, In Ubuntu<\/p>\n<pre><code>wget -O- https:\/\/apt.releases.hashicorp.com\/gpg | gpg --dearmor | sudo tee \/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg\necho \"deb [signed-by=\/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\" | sudo tee \/etc\/apt\/sources.list.d\/hashicorp.list\nsudo apt update &amp;&amp; sudo apt install packer<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Verify packer installation by executing the packer command.<\/p>\n<pre><code>packer version<\/code><\/pre>\n<p>You should see the output as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/packer-output-min.png\" class=\"kg-image\" alt=\"Verifying Packer installation\" loading=\"lazy\" width=\"608\" height=\"340\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/packer-output-min.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/packer-output-min.png 608w\"><\/figure>\n<h2 id=\"building-vm-image-aws-ami-using-packer\">Building VM Image (AWS AMI) Using Packer<\/h2>\n<p>Building a VM image using Packer is a simple process. The workflow is somewhat similar to <a href=\"https:\/\/devopscube.com\/vagrant-tutorial-beginners\/\" rel=\"noreferrer noopener\">building VMs using Vagrant<\/a><\/p>\n<p>Take a look at the following high-level Packer workflow diagram.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/packer-workflow-min.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"720\" height=\"464\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/packer-workflow-min.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/packer-workflow-min.png 720w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Let&#8217;s break down the process.<\/p>\n<ol>\n<li>Declare all the required VM configurations in an HCL (Hashicorp configuration language) or a JSON file. Let&#8217;s call it the <strong>Packer template<\/strong>.<\/li>\n<li>To build the VM image, execute Packer with the Packer template..<\/li>\n<li>Packer authenticates the remote cloud provider and launches a server. If you execute Packer from a cloud environment, it leverages the cloud service account for authentication.<\/li>\n<li>Packer takes a remote connection to the server (SSH or Winrm).<\/li>\n<li>Then it configures the server based on the provisioner you specified in the Packer template (Shell script, Ansible, Chef, etc).<\/li>\n<li>Registers the AMI<\/li>\n<li>Deletes the running instance.<\/li>\n<\/ol>\n<h2 id=\"packer-template-reference\">Packer Template Reference<\/h2>\n<p>All the code used in this article is hosted on a <a href=\"https:\/\/github.com\/techiescamp\/packer-templates?ref=devopscube.com\" rel=\"noreferrer noopener\">Packer Template Github repository.<\/a> You can clone or fork it to keep it as a reference. Or you can use the template directly from the <code>packer-aws-shell<\/code> folder<\/p>\n<pre><code>git clone https:\/\/github.com\/techiescamp\/packer-templates<\/code><\/pre>\n<p>Please feel free to contribute to the repo with more templates.<\/p>\n<h2 id=\"packer-hcl-template-explained\">Packer HCL Template Explained<\/h2>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b> From version <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">v1.7<\/code> Packer uses HCL format templates. JSON support is also there. But it&#8217;s recommended to change to HCL format as the newly developed features will not be added to the JSON-based template. If your exiting project or organization users JSON templates, you can convert them to HCL templates. See <a href=\"https:\/\/www.packer.io\/docs\/commands\/hcl2_upgrade?ref=devopscube.com\" rel=\"noreferrer noopener\">Upgrading JSON to HCL<\/a> for more information.<\/div>\n<\/div>\n<p>Let&#8217;s have a look at the HCL Packer template in detail.<\/p>\n<p>Here is the high-level packer template structure.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/packer-template-file-min.png\" class=\"kg-image\" alt=\"Packer HCL template structure\" loading=\"lazy\" width=\"720\" height=\"1063\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/packer-template-file-min.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/packer-template-file-min.png 720w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>An HCL template has a few blocks. Let&#8217;s <strong>understand each block with sample configuration<\/strong> before we create the AMI with Packer<\/p>\n<h3 id=\"variables\">Variables<\/h3>\n<p>Using the variable block, you can declare the default Packer parameters as variables. The declared variables can be overridden from the command line during the packer build process.<\/p>\n<p>You can make use of packer variables for dynamic configuration when used with CI tools like Jenkins.<\/p>\n<p>Let\u2019s understand different scenarios for variable usage.<\/p>\n<h4 id=\"using-variable-within-template\">Using Variable Within Template<\/h4>\n<p>The <code>variables<\/code> block holds all the default variables within a template. An example is shown below.<\/p>\n<pre><code>variable \"ami_id\" {\n  type    = string\n  default = \"ami-6f68cf0f\"\n}<\/code><\/pre>\n<p>The declared variables can be accessed in other parts of the template using <code>\"${var.app_name}\"<\/code> syntax. An example is shown below.<\/p>\n<pre><code>source_ami    = \"${var.ami_id}\"<\/code><\/pre>\n<h4 id=\"using-environment-variables-in-templates\">Using Environment Variables in Templates<\/h4>\n<p>Packer lets you use the system environment variables. First, you need to declare the environment variables in the variable section to use them in the other parts of the template.<\/p>\n<p>Let&#8217;s say, you want to use the <code>SCRIPT_PATH<\/code> environment variable that holds the path to a shell script that has to be used in the shell provisioner. You can declare that variable as shown below.<\/p>\n<pre><code>variable \"script_path\" {\n  default = env(\"SCRIPT_PATH\")\n}<\/code><\/pre>\n<p>Then, you can use the <code>script_path<\/code> variable in the provisioned block like shown below.<\/p>\n<pre><code>provisioner \"shell\" {\n    script = \"${var.script_path}\/demo-script.sh\"\n  }<\/code><\/pre>\n<h4 id=\"using-command-line-variables\">Using Command Line Variables<\/h4>\n<p>All the variables declared in the variable block can be overridden during the Packer run time using <code>-var<\/code> a flag followed by the variable value.<\/p>\n<p>For example,<\/p>\n<pre><code>packer build -var 'app_name=httpd' ami.pkr.hcl<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b>&nbsp;In the above example,&nbsp;<code spellcheck=\"false\" style=\"white-space: pre-wrap;\">ami.pkr.hcl<\/code>&nbsp;is the packer template file.<\/div>\n<\/div>\n<h4 id=\"using-an-hcl-file\">Using an HCL file<\/h4>\n<p>You can also use an HCL file to eternalize all the variables in the template. Let&#8217;s say we have a variable file named  <code>vars.packer.hcl<\/code>, you can add the variables as shown below.<\/p>\n<pre><code>app_name = \"httpd\"\nami_id   = \"ami-6f68cf0f\"<\/code><\/pre>\n<p>And during the packer build, you can pass this file as shown below.<\/p>\n<pre><code>packer build -var-file=\"vars.packer.hcl\"<\/code><\/pre>\n<h4 id=\"using-a-json-file\">Using a JSON File<\/h4>\n<p>You can also list all the variables in a JSON file and pass it during the packer build as shown below.<\/p>\n<pre><code>packer build -var-file=variables.json ami.pkr.hcl<\/code><\/pre>\n<h3 id=\"locals\">locals<\/h3>\n<p>Locals are also variables, but they cannot be overridden. It is scoped within the Packer template.<\/p>\n<pre><code>locals {\n    app_name = \"httpd\"\n}<\/code><\/pre>\n<p>You can interpolate a variable with a local as shown below.<\/p>\n<pre><code>locals {\n  ami_name = \"${var.app_name}-prodcution\"\n}<\/code><\/pre>\n<p>If you want to pass any <strong>sensitive information in a variable<\/strong>, you can convert it to a local value and then mark it as sensitive. This way, the value won&#8217;t be printed anywhere in the Packer build logs.<\/p>\n<pre><code>local \"secret_key\" {\n  key = \"${var.secret_key}\"\n  sensitive  = true\n}<\/code><\/pre>\n<h3 id=\"source\">Source<\/h3>\n<p>Packer works on the concept of the builder plugin. For every cloud provider, there are specific builder plugins that interact with the Cloud provider API to create the images.<\/p>\n<p>In this block, we mention the builder plugin details. For example, if you want to build AWS AMI, you can specify <code>amazon-ebs<\/code> as  a builder plugin. You have to provide all the mandatory parameters associated with the <code>amazon-ebs<\/code> builder plugin.<\/p>\n<p>Also, In the following code block, you can see that we are calling <code>${var.ami_id}<\/code> declared in the variable block.<\/p>\n<p>Check the <a href=\"https:\/\/www.packer.io\/docs\/builders?ref=devopscube.com\" rel=\"noreferrer noopener\">official builder plugin documentation <\/a>to know about the list of supported builders.<\/p>\n<pre><code>source \"amazon-ebs\" \"httpd\" {\n  ami_name      = \"PACKER-DEMO-${local.app_name}\"\n  instance_type = \"t2.micro\"\n  region        = \"eu-west-1\"\n  source_ami    = \"${var.ami_id}\"\n  ssh_username  = \"ec2-user\"\n  tags = {\n    Env  = \"DEMO\"\n    Name = \"PACKER-DEMO-${var.app_name}\"\n  }\n}<\/code><\/pre>\n<h3 id=\"build\">build<\/h3>\n<p>In this block, we declare all the provisioners and post-processor.<\/p>\n<pre><code>build {\n  sources = [\"source.amazon-ebs.httpd\"]\n\n  provisioner \"shell\" {\n    script = \"script.sh\"\n  }\n}\n<\/code><\/pre>\n<h3 id=\"provisioner\">Provisioner<\/h3>\n<p>The provisioner block is part of the <code>build<\/code> block. Here you can mention how you want to configure the VM image. You can <strong>run shell scripts, Ansible playbooks, Check cookbooks<\/strong>, etc. See the <a href=\"https:\/\/www.packer.io\/docs\/provisioners?ref=devopscube.com\" rel=\"noreferrer noopener\">provisioners page<\/a> for all the supported provisioners.<\/p>\n<p>The script or <code>ansible-playbook <\/code>should be present in the folder where you have the Packer template.<\/p>\n<pre><code>provisioner \"shell\" {\n    script = \"script.sh\"\n  }<\/code><\/pre>\n<p>Also, there is a file provisioner using which you can copy files from the packer machine to the AMI.<\/p>\n<p>For example,<\/p>\n<pre><code>provisioner \"file\"{\n  source = \"\/build\/artifact\/code.jar\"\n  destination = \"\/app\/code.jar\"\n}<\/code><\/pre>\n<h3 id=\"post-processor\">Post-Processor<\/h3>\n<p>Post-Processor is not a mandatory block. In this block, we can specify what to do after creating the Image. For example, you can execute a local shell script to create a file with all the image details.<\/p>\n<pre><code>post-processor \"shell-local\" {\n    inline = [\"echo foo\"]\n  }<\/code><\/pre>\n<p>Also, you can use the manifest post-processor to get the AMI details in the <strong><code>manifest.json<\/code><\/strong> file.<\/p>\n<pre><code>  post-processor \"manifest\" {\n    output     = \"manifest.json\"\n    strip_path = true\n  }<\/code><\/pre>\n<h2 id=\"creating-aws-ami-using-packer-hcl-template\">Creating AWS AMI Using Packer HCL Template<\/h2>\n<p>Now let&#8217;s see how to create an AMI using the Packer HCL template.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b> I have given a basic example with minimal configurations just for your understanding. When it comes to Packer implementation in the real-time project, more customizations may be needed in terms of variables, naming schemes, builder plugging, etc.<\/div>\n<\/div>\n<p>Here is what the template does.<\/p>\n<ol>\n<li>Packer <strong>deploys an ec2 RHEL instance<\/strong> based on AMI id <code>ami-01e78c5619c5e68b4 <\/code>in the <code>eu-west-1<\/code> region.<\/li>\n<li><strong>Executes the shell script<\/strong> on the RHEL instance from the <code>script.sh<\/code> script provided in the template directory<\/li>\n<li><strong>Registers the AMI<\/strong> with the name given in the template.<\/li>\n<li>Deletes the running machine.<\/li>\n<\/ol>\n<p>Let&#8217;s get our hands dirty with a practical example.<\/p>\n<p><strong>Step 1:<\/strong> Create a folder named &#8220;<code>packer-vm<\/code>&#8220;.<\/p>\n<pre><code>mkdir packer-vm<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Inside the <code>packer-vm<\/code> folder, create a file named &#8220;<code>vm.pkr.hcl<\/code>&#8221; and copy the following HCL template.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\">If we put together all the blocks I explained in the above section, we get the following HCL Template.<\/div>\n<\/div>\n<pre><code>\nvariable \"ami_id\" {\n  type    = string\n  default = \"ami-01e78c5619c5e68b4\"\n}\n\nvariable \"app_name\" {\n  type    = string\n  default = \"httpd\"\n}\n\nlocals {\n    app_name = \"httpd\"\n}\n\nsource \"amazon-ebs\" \"httpd\" {\n  ami_name      = \"PACKER-DEMO-${local.app_name}\"\n  instance_type = \"t2.micro\"\n  region        = \"us-west-2\"\n  source_ami    = \"${var.ami_id}\"\n  ssh_username  = \"ec2-user\"\n  tags = {\n    Env  = \"DEMO\"\n    Name = \"PACKER-DEMO-${var.app_name}\"\n  }\n}\n\nbuild {\n  sources = [\"source.amazon-ebs.httpd\"]\n\n  provisioner \"shell\" {\n    script = \"script\/script.sh\"\n  }\n\n  post-processor \"shell-local\" {\n    inline = [\"echo foo\"]\n  }\n}<\/code><\/pre>\n<p><strong>Step 3: <\/strong>Create a folder named <code>script<\/code> inside the <code>packer-vm<\/code> folder and cd into the script directory.<\/p>\n<pre><code>mkdir script &amp;&amp; cd script<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Create a file name <code>script.sh<\/code> and copy the following shell script to it.  It just updates the yum repo and installs <code>httpd<\/code>. Here, you can add all the shell scripts that you want to get executed during the AMI creation.<\/p>\n<pre><code>#!\/bin\/bash\nsudo yum -y update\nsudo yum install -y httpd<\/code><\/pre>\n<p><strong>Step 4: <\/strong>[<strong>Very Important<\/strong>] Now that we have all the required files, let&#8217;s understand the required packer authentication.<\/p>\n<p>If you are running Packer on your workstation for debugging or testing purposes, you <strong>need the AWS access and secret keys for Packer to interact with AWS services<\/strong>. Make sure you have the AWS access keys set on the file <code>~\/.aws\/credentials<\/code>.<\/p>\n<p>Alternatively, you can pass AWS keys in the source block, as shown below. It is <strong>not a recommended practice<\/strong>. Accidently you might commit this to a source code repo.<\/p>\n<pre><code>source \"amazon-ebs\" \"example\" {\n  access_key = \"ASDFASDFASDFJQWEKONNOASDF\"\n  secret_key = \"SDFQ324876134HJKLASDBJKASDJFHBAJSDFJKHASKDG\"\n}<\/code><\/pre>\n<p>If you are running Packer from an <strong>AWS ec2 instance<\/strong>, make sure you add an<strong> IAM role with permissions<\/strong> to interact with AWS ec2 services. I have added the required IAM policy JSON in Github. See <a href=\"https:\/\/github.com\/bibinwilson\/packer-templates\/blob\/main\/packer-aws-shell\/aws-policy.json?ref=devopscube.com\" rel=\"noreferrer noopener\">Packer IAM policy JSON<\/a><\/p>\n<p><strong>Step 5:<\/strong> Now let&#8217;s validate the Packer HCL template before we build it. Validation is very important in the CI process.<\/p>\n<pre><code>packer validate vm.pkr.hcl<\/code><\/pre>\n<p><strong>Step 6: <\/strong>Run the packer build using the following command.<\/p>\n<pre><code>packer build vm.pkr.hcl<\/code><\/pre>\n<p>The above command runs for almost 3 &#8211; 5 minutes.<\/p>\n<p>If you check your AMI list, you should see an AMI named <code>PACKER-DEMO-httpd<\/code>.<\/p>\n<h2 id=\"debugging-packer-build\">Debugging Packer Build<\/h2>\n<p>There are situations where you might need to debug a packer build.<\/p>\n<p>The -debug flag lets you debug the AMI build.<\/p>\n<p>For example,<\/p>\n<pre><code>packer build -debug vm.pkr.hcl<\/code><\/pre>\n<p>When you run packer in debug mode, You need to approve each packer step manually as shown in the image below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-38.png\" class=\"kg-image\" alt=\"Packer debug\" loading=\"lazy\" width=\"633\" height=\"643\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-11-38.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-38.png 633w\"><\/figure>\n<p>Also, You can <strong>ssh into the ec2 instance<\/strong> created by Packer using the temporary ssh key or the ssh key pair provided in the packer config.<\/p>\n<p>If you don&#8217;t specify the ssh key in the packer configuration, it creates a temporary ssh key and places it in the same folder where you run the packer command.<\/p>\n<p>So that you can use the IP address and temporary ssh key to log in to the packer VM to debug the issues.<\/p>\n<p>If you want to see how packer works in real world setup, you can checkout the following <a href=\"https:\/\/devopscube.com\/devops-projects\/\" rel=\"noreferrer\">DevOps project<\/a> video where I have used Packer with Terraform.<\/p>\n<p><!--kg-card-begin: html--><br \/>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/GLMJhF_cZ5M?si=H8-btnCD-vjliNkn\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><br \/>\n<!--kg-card-end: html--><\/p>\n<h2 id=\"packer-build-logs\">Packer Build Logs<\/h2>\n<p>Another way to debug the packer build is through packer detailed logs.<\/p>\n<p>To get the build logs, you need to set the following environment variable before running the packer build command.<\/p>\n<pre><code>export PACKER_LOG=1\nexport PACKER_LOG_PATH=\"\/path\/to\/packer.log\"<\/code><\/pre>\n<p>Replace <strong><code>\/path\/to\/packer.log<\/code><\/strong> with the required log path. if you just specify packer.log, packer creates the log file in the folder where the Packer command is getting executed.<\/p>\n<p>To persist this environment variable, add it to the<strong><code> $HOME\/.bashrc<\/code><\/strong> file or add it as s script in <strong><code>\/etc\/profile.d<\/code><\/strong> folder.<\/p>\n<h2 id=\"unit-testing-packer-templates\">Unit Testing Packer Templates<\/h2>\n<p>Packer templates should be treated as any other <a href=\"https:\/\/devopscube.com\/infrastructure-as-code-configuration-management\/\">Infrastructure as code<\/a> that involves testing.<\/p>\n<p>You can write Packer tests using <a href=\"https:\/\/github.com\/inspec\/inspec?ref=devopscube.com\" rel=\"noreferrer noopener\">Check Inspec<\/a>.<\/p>\n<p>When implementing packer in production workflows, ensure you write the necessary tests as part of your CI\/CD pipeline.<\/p>\n<p>Check out <a href=\"https:\/\/github.com\/travis-ci\/packer-templates\/tree\/master\/spec?ref=devopscube.com\" rel=\"noreferrer noopener\">this repo<\/a> for packer inspec reference.<\/p>\n<h2 id=\"packer-possible-errors\">Packer Possible Errors<\/h2>\n<p>You might get the following ssh error when working on AWS-based AMIs. A possible cause of the issue is the support for <code>ed25519<\/code> ssh key type for the temporary ssh key.  To rectify this issue, you need to upgrade Packer to the latest version that supports <code>ed25519<\/code>.<\/p>\n<pre><code>amazon-ebs: Error waiting for SSH: Packer experienced an authentication error when trying to connect via SSH. This can happen if your username\/password are wrong. You may want to double-check your credentials as part of your debugging process.<\/code><\/pre>\n<h2 id=\"few-packer-tips\">Few Packer Tips<\/h2>\n<p><strong>Tip 1:<\/strong> You can capture the output of the image ID in the manifest.json file. You can then parse the JSON file to get the exact image ID.<\/p>\n<p><strong>Tip 2:<\/strong> Use the -debug flag to log in to the server created by Packer for troubleshooting rather than running the packer build every time you have a change in provisioning scripts.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this <strong>packer tutorial for beginners,<\/strong> I have covered the basics of image creation using Packer.<\/p>\n<p>There are more customizations you can do in your Paker templates when it comes to a real-time project.<\/p>\n<p>Again, it depends on what you want to achieve using packer.<\/p>\n<p>You might be using Jenkins for image build pipelines or maybe custom utilities.<\/p>\n<p>Either way, drop a comment and let me know how you manage images in your project.<\/p>\n<p>Also, it is one of the devops tools that every devops engineer should know. I have explained this in my <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\">devops engineer skills<\/a> guide.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/packer-tutorial-for-beginners\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Connect to Cloudways Server [SSH &amp; SFTP] \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/packer-tutorial-for-beginners\/<\/p>\n","protected":false},"author":1,"featured_media":348,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-347","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=347"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/347\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/348"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}