Search code examples
tagspackerhashicorp

Tags format on Packer ec2-ami deployment


I'm trying out to create an amazon ec2 ami for the 1st time using Hashicorp Packer, however getting this failure on the tag creation, I already tried some retries on trial and error test for the format but still unlucky

[ec2-boy-oh-boy@ip-172-168-99-23 pogi]$ packer init .
Error: Missing item separator

  on variables.pkr.hcl line 28, in variable "tags":
  27:   default = [
  28:    "environment"    : "testing"

Expected a comma to mark the beginning of the next item. 

My code ec2.pkr.hcl looks like this:

[ec2-boy-oh-boy@ip-172-168-99-23 pogi]$ cat ec2.pkr.hcl 
packer {
  required_plugins {
    amazon = {
      version = ">= 0.0.2"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

source "amazon-ebs" "ec2" {
  ami_name      = "${var.ami_prefix}-${local.timestamp}"
  instance_type = "t2.micro"
  region        = "us-east-1"
  vpc_id        = "${var.vpc}"
  subnet_id     = "${var.subnet}"
  security_group_ids = ["${var.sg}"]
  ssh_username = "ec2-boy-oh-boy"
  source_ami_filter {
    filters = {
      name                = "amzn2-ami-hvm-2.0*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["12345567896"]
  }
  launch_block_device_mappings = [
    {
        "device_name": "/dev/xvda",
        "delete_on_termination": true
        "volume_size": 10
        "volume_type": "gp2"
    }
   ]
  run_tags = "${var.tags}"
  run_volume_tags = "${var.tags}"
}

build {
  sources = [
    "source.amazon-ebs.ec2"
  ]
}
[ec2-boy-oh-boy@ip-172-168-99-23 pogi]$ 

Then my code variables.pkr.hcl looks like this:

 [ec2-boy-oh-boy@ip-172-168-99-23 pogi]$ cat variables.pkr.hcl 
locals {
  timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

variable "ami_prefix" {
  type    = string
  default = "ec2-boy-oh-boy"
}

variable "vpc" {
  type    = string
  default = "vpc-asd957d"
}

variable "subnet" {
  type    = string
  default = "subnet-asd957d"
}

variable "sg" {
  type    = string
  default = "sg-asd957d"
}

variable "tags" {
  type = map
  default = [
    environment        = "testing"
    type               = "none"
    production         = "later"
  ]
}

Solution

  • Your default value for the tags variable is of type list(string). Both the run_tags and run_volume_tags directives expect type map[string]string.

    I was able to make the following changes to your variables file and run packer init successfully:

    variable "tags" {
      default = {
        environment = "testing"
        type        = "none"
        production  = "later"
      }
    
      type = map(string)
    }