Packer filter is not giving official CentOS image

Do any of you have a way to get the latest CentOS image when building packer images?

I’m trying to do this with CentOS images, this is what I get:

aws ec2 describe-images — owners aws-marketplace — filters “Name=name,Values=*b7ee8a69-ee97–4a49–9e68-afaee216db2e*” | jq -r ‘.Images[] | “\(.OwnerId)\t\(.Name)”’
679593333241 CentOS Linux 7 x86_64 HVM EBS 1708_11.01-b7ee8a69-ee97–4a49–9e68-afaee216db2e-ami-95096eef.4
679593333241 CentOS Linux 7 x86_64 HVM EBS ENA 1901_01-b7ee8a69-ee97–4a49–9e68-afaee216db2e-ami-05713873c6794f575.4
679593333241 CentOS Linux 7 x86_64 HVM EBS 1801_01-b7ee8a69-ee97–4a49–9e68-afaee216db2e-ami-0a537770.4
679593333241 CentOS Linux 7 x86_64 HVM EBS ENA 1805_01-b7ee8a69-ee97–4a49–9e68-afaee216db2e-ami-77ec9308.4
679593333241 CentOS Linux 7 x86_64 HVM EBS ENA 1803_01-b7ee8a69-ee97–4a49–9e68-afaee216db2e-ami-8274d6ff.4
679593333241 CentOS Linux 7 x86_64 HVM EBS ENA 1804_2-b7ee8a69-ee97–4a49–9e68-afaee216db2e-ami-55a2322a.4

Relevant parts of the Packer file:

"variables": {
    "aws_profile": "personal",
    "aws_region": "us-east-1"
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "name": "CentOS",
      "ssh_username": "ec2-user",
      "instance_type": "t3.micro",
      "region": "{{user `aws_region`}}",
      "profile": "{{user `aws_profile`}}",
      "ami_name": "centos",
      "ami_description": "CentOS image",
      "force_deregister": "true",
      "force_delete_snapshot": "true",
      "encrypt_boot": true,
      "source_ami_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "name": "CentOS Linux 7 x86_64 HVM EBS ENA*",
          "root-device-type": "ebs"
        },
        "owners": [
          "679593333241"
        ],
        "most_recent": true
      }
    }
  ]

As well as the CentOS image owner 679593333241
And when I run packer build I get:

Error launching source instance: OptInRequired: In order to use this AWS Marketplace product you need to accept terms and subscribe. To do so please visit https://aws.amazon.com/marketplace/pp?sku=67xglex2rdpaymxh17620nfoy

That link takes me to the page of a vendor I don’t trust which is “Supported Images”, any clue on why I’m not getting to the CentOS image?

You could try searching for the image by using the product code (https://wiki.centos.org/Cloud/AWS#x86_64)

aws ec2 describe-images \
    --owners 'aws-marketplace' \
    --filters 'Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce' \
    --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \
    --output 'text'

and compare the owner. Apparently your search does not find the provider you are looking for.

For the packer template you could use the product code instead: https://packer.io/docs/builders/amazon-ebs.html#ami_product_codes

Since I don’t have an AWS account, I can’t test it for you.

Hello Tobias.

Thank you so much for answering, I obtained the owner of the official CentOS AMI that I placed on my config by running the code from the CentOS wiki so I’m pretty sure that’s the owner.

I tried adding the product code to my Packer file like this:

"ami_product_codes": [
        "aw0evgkw8e5c1q413zgy5pjce"
],

And it gives me the same message as before:

Error launching source instance: OptInRequired: In order to use this AWS Marketplace product you need to accept terms and subscribe. To do so please visit https://aws.amazon.com/marketplace/pp?sku=67xglex2rdpaymxh17620nfoy

For some unknown reason it continues to take me to the Supported Images AMI which is not the one I want.

You are not the only one:
https://forums.aws.amazon.com/thread.jspa?messageID=899949

The product-code filter solved it for them… Hm…

And I quote:

For anyone who has the same issue I have worked around this issue for now by adding the CentOS product code to my filter:

    "source_ami_filter": {
      "filters": {
        "name": "CentOS Linux 7 x86_64 HVM EBS *",
    "product-code": "aw0evgkw8e5c1q413zgy5pjce"
      },
      "owners": ["679593333241"],
      "most_recent": true
    },