# DevOps Training Session 12: Cloud - Packer ###### tags: `devops` `reliable` `research` Hello btb again with Xeus-Cooking with DevOps :coffee: and today we will talk about the packer. The tool use for case when you want to reproduce the image on cloud or VM you want for project and storge that in case manifest and you it if you want using the image you want --> Let go [:coffee:](https://docs.google.com/presentation/d/10W5pvmmKtbn6pX3iPAsAwp5U1oNTIcj45n2gQH8Jbfo/edit?usp=sharing) ## Overview ![](https://i.imgur.com/NliAtUI.png) - Packer is a free and open source tool for creating golden images for multiple platforms from a single source configuration - Packer is product from Hashicorp so you can use the same as language for Terraform to working with Packer ## Implement - Scenerio, If you use the linux VM for doing what you want but it will not setup the tool and somewhat you want inside. So you can resolve it with packer. - Packer will use twice language for produce the image is Json and HCL - like terraform. So for easy using packer we will take care both of json and hcl for have flag view with packer - For install packer you can use this article for doing this task [Installer](https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli) - After you got packer, u need to understand the workflow will apply for packer and component inside - First, the component inside include: ![](https://i.imgur.com/NV48K6U.png) - Builder is the important block where you put some kind version of image, resource for place to storing the image, comunicator for doing some job inside the image SSH, WinRM,... - Provisioner: This block for you put the script which you want to doing with image - Post-Processors: This block is the end of packer which store some binary or unicode manifest - quite hardy to understanding - So don't expect something about that. It can be the checksum for validate your Image what ever - Secondly, the workflow of packer ![](https://i.imgur.com/bkupAnw.png) - So go on the script for you want to doing with packer, i will put both of version json and hcl for comparing ``` HCL-verion. variable "os_type" { type = string default = "Linux" } variable "publisher" { type = string default = "Canonical" } variable "offer" { type = string default = "UbuntuServer" } variable "sku" { type = string default = "18.04-LTS" } variable "version" { type = string default = "latest" } variable "render_image_name" { type = string } variable "resource_group_name" { type = string } variable "build_resource_group_name" { type = string } variable "location" { type = string default = "southeastasia" } variable "vm_size" { type = string default = "Standard_B1s" } variable "tags" { type = map(string) default = { managed = "packer" environment = "dev" } } variable "communicator" { type = string default = "ssh" } source "azure-arm" "linux-docker" { use_azure_cli_auth = true os_type = var.os_type image_publisher = var.publisher image_offer = var.offer image_sku = var.sku image_version = var.version managed_image_name = var.render_image_name managed_image_resource_group_name = var.resource_group_name build_resource_group_name = var.build_resource_group_name vm_size = var.vm_size azure_tags = var.tags communicator = var.communicator } build { sources = ["source.azure-arm.linux-docker"] provisioner "shell" { scripts = ["${abspath(path.root)}/script/setup-az.sh", "${abspath(path.root)}/script/setup-docker.sh"] } } ``` ``` { "variables": { "name": "linux-docker", "managed_image_resource_group_name": "", "managed_image_name": "linux-docker", "client_id":"", "subscription_id":"", "os_type": "Linux", "image_publisher": "Canonical", "image_offer": "UbuntuServer", "image_sku": "18.04-LTS", "communicator": "ssh", "managed_by": "packer", "environment": "", "location": "southeastasia", "vm_size": "Standard_B1s" }, "builders": [{ "name": "{{user `name`}}", "type": "azure-arm", "use_azure_cli_auth": true, "managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}", "managed_image_name": "{{user `managed_image_name`}}", "os_type": "{{user `os_type`}}", "image_publisher": "{{user `image_publisher`}}", "image_offer": "{{user `image_offer`}}", "image_sku": "{{user `image_sku`}}", "communicator": "{{user `communicator`}}", "azure_tags": { "managed_by": "{{user `managed_by`}}", "environment": "{{user `environment`}}" }, "location": "{{user `location`}}", "vm_size": "{{user `vm_size`}}" } ], "provisioners": [{ "type": "shell", "scripts": [ "{{template_dir}}/script/setup-az.sh", "{{template_dir}}/script/setup-docker.sh" ] }] } ``` - So on my vision, i think the both of version have not different between but if you want to do easily to view sucript with not indent to much HCL, is the best but if you want reduce the line you put for script and doing with pipeline, Json is the best choice. - After you apply the packer workflow `init-validate-build` and it complete with your image to deploy on cloud ![](https://i.imgur.com/sPhOEov.png) - So in this task i have receive the mission to run packer on pipeline. So just create a new pipeline for doing packer job ``` ## packer-pipelines.yaml trigger: none pool: name: linuxAgent stages: - stage: setup_packer_and_identity_for_packer jobs: - job: install_packer steps: - task: CmdLine@2 displayName: Install inputs: 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 - job: authentication_packer steps: - task: CmdLine@2 displayName: Authenticate inputs: script: | az login --identity - stage: validate_for_packer dependsOn: [setup_packer_and_identity_for_packer] condition: succeeded('setup_packer_and_identity_for_packer') jobs: - job: validate_for_packer steps: - task: CmdLine@2 displayName: Validate inputs: workingDirectory: "packer/" script: | packer init linux-docker.pkr.hcl packer validate linux-docker.pkr.hcl env: PKR_VAR_render_image_name: $(renderImageName) PKR_VAR_resource_group_name: $(resourceGroup) PKR_VAR_build_resource_group_name: $(buildResourceGroupName) - stage: build_for_packer dependsOn: [validate_for_packer] condition: succeeded('validate_for_packer') jobs: - job: build_for_packer steps: - task: CmdLine@2 displayName: Build inputs: workingDirectory: "packer/" script: | packer build -force linux-docker.pkr.hcl env: PKR_VAR_render_image_name: $(renderImageName) PKR_VAR_resource_group_name: $(resourceGroup) PKR_VAR_build_resource_group_name: $(buildResourceGroupName) ``` - So just put it azure-pipeline, run and you have image - But the next step we will reproduce the VM using the image so go to VM module or VMSS module to config again ``` ## data.tf data "azurerm_image" "main" { name = var.source_image_name resource_group_name = var.resource_group_root_name } ## variables.tf variable "source_image_name" { type = string } ## vmss.tf source_image_id = var.source_image_id ## /dev/main.tf module "vmss" { source = "../modules/vmss" resource_group_name = azurerm_resource_group.main.name resource_group_location = azurerm_resource_group.main.location source_image_id = data.azurerm_image.main.id ssh_public_key_name = data.azurerm_ssh_public_key.main.public_key container_registry_name = data.azurerm_container_registry.main.name user_identity_id = module.iam.user_identity_id subnet_id = module.network.subnet_id application_security_group_ids = module.network.application_security_group_ids load_balancer_backend_address_pool_ids = module.balancer.load_balancer_backend_address_pool_ids storage_account_name = module.storage.storage_account_name storage_container_name = module.storage.storage_container_name storage_blob_name = module.storage.storage_blob_name environment = var.environment tags = var.tags depends_on = [ module.storage ] } ``` - So the infra will change by replace current image by source_id of image create from packer - And so do pipeline like previous session again and you have VMSS or VM with image created by packer ## Conclusion - So this all of session packer, packer is replace me do the job for create a image for purpose like environment and package install inside. Quite easy ! :thinking_face: - So if you meet promblem inside process building image with packer it will be 2 situation: - 1. Using not available syntax from previous version. IDK what version it change but i cost some time to solving this problem - 2. Grant permission agent pipeline because principle of packer is create a new RG and building image, run task via script and compress it into once became Image. ## Ending - Hopefully, you meet something u need in this session and have knowledge for give packer do it what it job. - Reduce time and work like purpose easily --> GG :coffee: ![](https://i.imgur.com/3qZpDVU.png) *anywhere gg* ![](https://i.imgur.com/1bdIaxi.png) ## Reference [Packer for Azure](https://developer.hashicorp.com/packer/plugins/builders/azure/arm) [Packer syntax](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/syntax)