Terraform - Searching for Windows AMIs
- Brian Greenfield
- Jun 10, 2021
- 3 min read
# brief
I need to find two AMIs using the terraform AMI filter:
Windows 2019 Base
Windows 2019 with SQL standard 2019
This seems harder that it first sounds as I have done the same with Ubuntu. Searching the internet didn't seem to reveal any examples on how to find the windows AMIs.
So this is how I did it.
(I'm sure this was obvious to a lot of people but I struggled, so thought I would share)
# method
I started with my existing AMI filter code for terraform, which I was using for our own images.
data "aws_ami" "my-image" {
most_recent = true
filter {
name = "name"
values = ["${var.env}-image-name-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["my-account-id"]
}
So I thought well if I just take the windows image name from AWS console, i'm not sure on the owner, so i'll leave that out:
data "aws_ami" "my-image" {
most_recent = true
filter {
name = "name"
values = ["Microsoft Windows Server 2019 Base"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
Well this didn't work, I didn't read the terraform documentation for this data resource, owner is mandatory field.
Fine, I'll just check the AWS console to find the owner ID/name or something..
Nothing in the "launch instance" area, so I went into the detail of the image on marketplace:
Nothing on there....hmmmm
Maybe the documentation on terraform registry has an example...
Not that I can see...but it does mention in the Argument reference section:
owners - (Required) List of AMI owners to limit search. At least 1 value must be specified. Valid values: an AWS account ID, self (the current account), or an AWS owner alias (e.g. amazon, aws-marketplace, microsoft).
OK, so the would make sense that the owner in the this case would be Amazon:
data "aws_ami" "my-image" {
most_recent = true
filter {
name = "name"
values = ["Microsoft Windows Server 2019 Base"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["amazon"]
}
Still nothing returned...
So must be the name that's wrong...thinking about it, this is probably likely to be a slug, i.e.: no spaces, but what exactly would be the syntax??
More searching reveals nothing...
Then I think to myslef...this must work through the PowerShell AWS cli tools, how do you find an image using that?
I end up at this article:
I run the second command specified:
Get-EC2ImageByName
this displays the list as depicted in the documentation, but nothing for Server 2019...
OK but I bet the naming convention is similar, lets inspect one of these image names..
Get-EC2ImageByName -Names WINDOWS_2016_BASE
The detail is display and I look for the name:
Name : Windows_Server-2016-English-Full-Base-2019.08.16
So on this basis I think my code should look like this:
data "aws_ami" "my-image" {
most_recent = true
filter {
name = "name"
values = ["Windows_Server-2019-English-Full-Base*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["amazon"]
}
note: I added a star at the end, as I want to find the latest image, so not interested in the release number.
This works! Now I need to find the SQL image I'm after...
So following the same principle:
Get-EC2ImageByName -Names WINDOWS_2016_SQL_SERVER_STANDARD_2016
and my code looks this this:
data "aws_ami" "my-image" {
most_recent = true
filter {
name = "name"
values = ["Windows_Server-2019-English-Full-SQL_2019_Standard*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["amazon"]
}
note: again I used the * star at the end
This also works!
I can now use these image IDs in my terraform plan.
# conclusion
This was much harder than I thought it would be, mind you I find that more and more these days.
Hope this helps.
Hozzászólások