Find Owner of AWS Community Image

One of the required entries for Terraform code on the aws_ami is the owner . The problem is how do you find the owner of the AWS image?

First get the ami image id from AWS launch image page. You will have to login into your aws console and click the Launch image button. Search for your image you want. I wanted Fedora, so I searched for Fedora. I specifically wanted Fedora 34 and not the coreos but the regular OS. So I searched for “Fedora 34” and found the base image. The image ID was right next to the name. Example: ami-005b3d4774719af46

Next I went to my terminal on my laptop and use the AWS cli command to get the description of the image.

$ aws ec2 describe-images --image-id ami-005b3d4774719af46

From here I can see the owner and everything about the image.

{
    "Images": [
        {
            "Architecture": "arm64",
            "CreationDate": "2021-12-16T10:17:55.000Z",
            "ImageId": "ami-005b3d4774719af46",
            "ImageLocation": "125523088429/Fedora-Cloud-Base-34-20211216.0.aarch64-hvm-us-east-2-gp2-0",
            "ImageType": "machine",
            "Public": true,
            "OwnerId": "125523088429",
            "PlatformDetails": "Linux/UNIX",
            "UsageOperation": "RunInstances",
            "State": "available",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/sda1",
                    "Ebs": {
                        "DeleteOnTermination": true,
                        "SnapshotId": "snap-056bba4ae9c609dc1",
                        "VolumeSize": 6,
                        "VolumeType": "gp2",
                        "Encrypted": false
                    }
                }
            ],
            "Description": "Fedora AMI Description",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "Name": "Fedora-Cloud-Base-34-20211216.0.aarch64-hvm-us-east-2-gp2-0",
            "RootDeviceName": "/dev/sda1",
            "RootDeviceType": "ebs",
            "VirtualizationType": "hvm"
        }
    ]
}

Now I want to take this information and put it in my Terraform code. However, when I run my Terraform code, I want the latest Fedora 34 image that was put out. So I needed to do some basic (very basic regex and add a key)

data "aws_ami" "fedora" {
  most_recent = true
  owners = ["125523088429"]
  name_regex = "Fedora*"

  filter {
    name = "name"
    values = ["Fedora-Cloud-Base-34-*.x86_64-hvm-us-east-2-gp2-0"]
  }
  filter {
    name = "virtualization-type"
    values = ["hvm"]
  }
}

I add the key – most-recent so I get the latest ami. I added the name_regex key to get the latest Fedora images. I substituted an asterisk (*) in place of version to insure I get the latest Fedora base 34 version that is out.

From here I could create my instance from the latest Fedora base 34 image that is released in the Community AMI.

resource "aws_instance" "my_instance" {
  ami = data.aws_ami.fedora.id
  instance_type = "t2.micro"
}