A data source is basically just a query of the AWS API to receive information needed to deploy a resource.
In this case - we need an AMI id based on some filters we're going to provide.
One thing to make sure - is to have the correct owner - so:
data "aws_ami" "server_ami" {
most_recent = true
owners = ["099720109477"] # put saved owner
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] # instead of date put asterisk to get the most recent one
}
}
Run plan and apply.terraform state show 'data.aws_ami.server_ami'
Output:
...
id = "ami-0a24ce26f4e187f9a"
image_id = "ami-0a24ce26f4e187f9a"
image_location = "amazon/ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20220810"
image_owner_alias = "amazon"
image_type = "machine"
most_recent = true
name = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20220810"
...