hashicorp/packer

Build fails for valid ami-name

sydrawat01 opened this issue ยท 6 comments

Community Note
  • Please vote on this issue by adding a ๐Ÿ‘ reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

While trying to have a dynamic ami_name in the packer template, the build command throws an error saying the AMI name is invalid, whereas the variables and locals have valid values of the git branch name and the commit sha. This error occurs while using the packer plugin git-commit.

Overview of the Issue

Using the git-commit plugin to use the git branch name and commit sha in the ami_name for amazon-ebs, packer validate and build commands fail throwing an error which says that the ami_name contains invalid characters.

Reproduction Steps

Create a packer file with a dynamic ami_name for amazon-ebs

Run packer init, validate and build commands in succession

packer init.;
packer validate .;
packer build ami.pkr.hcl
  • The validate command throws and error.
  • The build commnad throws an error.

Errors:

Error: 1 error(s) occurred:

* AMIName should only contain alphanumeric characters, parentheses (()), square brackets ([]), spaces ( ), periods (.), slashes (/), dashes (-), single quotes ('), at-signs (@), or underscores(_). You can use the `clean_resource_name` template filter to automatically clean your ami name.

  on ami.pkr.hcl line 48:
  (source code not available)

Another interesting thing to notice here is, when I run the validate -syntax-only, it runs without errors, verifying there's no issue with the syntax.

I'm trying something like this with my packer template.

Packer version

packer version v1.8.3

Simplified Packer Template

packer {
  required_plugins {
    git = {
      version = ">=v0.3.2"
      source  = "github.com/ethanmdavidson/git"
    }
  }
}

locals {
  truncated_sha = substr(data.git-commit.cwd-head.hash, 0, 8)
  version       = data.git-repository.cwd.head == "master" && data.git-repository.cwd.is_clean ? var.ubuntu_version : "${var.ubuntu_version}-${local.truncated_sha}"
}

data "git-repository" "cwd" {}
data "git-commit" "cwd-head" {}

source "amazon-ebs" "ec2" {
  ami_name = "ubuntu-${local.version}"
  ami_description = "EC2 AMI for CSYE 6225 built by ${data.git-commit.cwd-head.author}"
}

build {
  sources = ["source.amazon-ebs.ec2"]
}

Operating system and Environment details

MacOS Monterey (v12.6), Intel i5

Hi @sydrawat01 thanks for reaching out. Looking at the provided error messaging it seems like on packer build that the generated name contains an invalid character.

By chance does the ubuntu version variable contain dots (e.g 22.04)?

Dots in the AMI name are not supported so that might be the reason you are seeing the error above. If you remove the dots it should work, assuming there are no other illegal characters. I don't believe the clean_resource_name function works for HCL templates so it seems like a bug to me to mention that.

Another interesting thing to notice here is, when I run the validate -syntax-only, it runs without errors, verifying there's no issue with the syntax.

Data sources are not evaluated when running packer validate so the values returned from the Git data source will be ignored or empty in case of the ami_name. That said running with -syntax-only just checks the HCL syntax for errors it doesn't evaluate any of the HCL hence why you do not get any errors when running packer validate -syntax-only ..

Possible bug here that clearn_resource_name is displayed as an option for HCL builds ๐Ÿ‘‡ We may need to consider adding a clean_resource_name function to HCL or provide guidance on using the regex_replace function https://www.packer.io/docs/templates/hcl_templates/functions/string/regex_replace

Error: 1 error(s) occurred:

* AMIName should only contain alphanumeric characters, parentheses (()), square brackets ([]), spaces ( ), periods (.), slashes (/), dashes (-), single quotes ('), at-signs (@), or underscores(_). You can use the `clean_resource_name` template filter to automatically clean your ami name.

By chance does the ubuntu version variable contain dots (e.g 22.04)?
Dots in the AMI name are not supported so that might be the reason you are seeing the error above. If you remove the dots it should work, assuming there are no other illegal characters

My version name does not contain dots. Also I think ami_name is too strict here? Like for example:

ami_name        = "EC2-AMI-${substr(data.git-commit.commit.hash, 0, 8)}" # does not work
tags = {
    Name        = "EC2-AMI-${substr(data.git-commit.commit.hash, 0, 8)}" # works
}

I don't think there's any invalid character in the commit hash, as the Name tag gets the commit hash without throwing any errors, and consists only of characters and digits.

Hi @sydrawat01 I took another looks at this issue and I believe I misunderstood. I see you mention both packer validate and packer build. But I thought you were just getting the error on build. Data sources are not evaluated during validate, which is why Packer is erroring with a bad ami name.

Is it both validate and build that is failing or just validate?

If the error is happening solely on packer validate there is a fix in the latest release of Packer that will help with this issue. In v1.8.5 a new flag -evaluate-datasources was introduced to the validate command that will instruct Packer to execute each data source as it would when running packer build. Using 1.8.5 against the provided template snippet, with a variable block for ubuntu_version, I can validate properly.

~>  packer validate -evaluate-datasources source.pkr.hcl
Error: 4 error(s) occurred:

* An ssh_username must be specified
  Note: some builders used to default ssh_username to "root".
* A source_ami or source_ami_filter must be specified
* For security reasons, your source AMI filter must declare an owner.
* either instance_type or spot_instance_types must be specified

  on source.pkr.hcl line 23:
  (source code not available)

Running a build with a completed template using the provide configuration for the ami_name and ami_description results in a successful build.


==> amazon-ebs.basic-example: Prevalidating any provided VPC information
==> amazon-ebs.basic-example: Prevalidating AMI Name: ubuntu-22.04-f110c896
...
Build 'amazon-ebs.basic-example' finished after 5 minutes 3 seconds.

==> Wait completed after 5 minutes 3 seconds

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs.basic-example: AMIs were created:
us-east-1: ami-0cfff3ec85bbb68ef

I think it might be safe to close this issue but I will keep it open in case you run into the same issue with Packer 1.8.5. Otherwise feel free to close.

Thanks for the detailed explanation and the fix @nywilken! I used v1.8.5, and it works as expected!

My workflows should now pass successfully! ๐Ÿ˜