Terraformで最新のLinuxのAMIを取得するためのコード
Posted On 2024-12-23
Terraformを使ってAmazon Linux 2023とUbuntu 24.04の最新AMIを取得する際のフィルター設定例をまとめてみました。
目次
はじめに
- TerraformでEC2用のAmazon Linux/Ubuntuの最新のAMIを取得するためのコード。フィルターのかけかたをいつも忘れてしまうのでメモとして書いておく
- Amazon Linux 2023とUbuntu 24.04が対象
Terraformコード
# Ubuntu 24.04
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # CanonicalのAMI所有者ID
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
# Amazon Linux 2023
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["137112412989"] # AmazonのAMI所有者ID
filter {
name = "name"
# Amazon Linux 2023 AMIの名前パターン。minimumを除外する
values = ["al2023-ami-2023*-kernel-*-x86_64"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
# 出力
output "ubuntu_ami_details" {
value = data.aws_ami.ubuntu
}
output "amazon_linux_ami_details" {
value = data.aws_ami.amazon_linux
}
Amazon Linuxの場合は、minimum
とプレフィックスがついたAMIが入ってしまい、これが必要なライブラリを全然持っていないことが多いのでそれを除外するために「2023」を入れている。アーキテクチャーのフィルタリングはname
でしなくてもよかったかもしれない。
結果
terraform apply
の結果。以下のような結果が返ってくるはず。
amazon_linux_ami_details = {
"architecture" = "x86_64"
"arn" = "arn:aws:ec2:ap-northeast-1::image/ami-0ab02459752898a60"
"block_device_mappings" = toset([
{
"device_name" = "/dev/xvda"
"ebs" = tomap({
"delete_on_termination" = "true"
"encrypted" = "false"
"iops" = "3000"
"snapshot_id" = "snap-0846e1db4a7c52e3a"
"throughput" = "125"
"volume_size" = "8"
"volume_type" = "gp3"
})
"no_device" = ""
"virtual_name" = ""
},
])
"boot_mode" = "uefi-preferred"
"creation_date" = "2024-12-12T08:31:35.000Z"
"deprecation_time" = "2025-03-12T08:32:00.000Z"
"description" = "Amazon Linux 2023 AMI 2023.6.20241212.0 x86_64 HVM kernel-6.1"
"ena_support" = true
"executable_users" = tolist(null) /* of string */
"filter" = toset([
{
"name" = "architecture"
"values" = toset([
"x86_64",
])
},
{
"name" = "name"
"values" = toset([
"al2023-ami-2023*-kernel-*-x86_64",
])
},
{
"name" = "virtualization-type"
"values" = toset([
"hvm",
])
},
])
"hypervisor" = "xen"
"id" = "ami-0ab02459752898a60"
"image_id" = "ami-0ab02459752898a60"
"image_location" = "amazon/al2023-ami-2023.6.20241212.0-kernel-6.1-x86_64"
"image_owner_alias" = "amazon"
"image_type" = "machine"
"imds_support" = "v2.0"
"include_deprecated" = false
"kernel_id" = ""
"most_recent" = true
"name" = "al2023-ami-2023.6.20241212.0-kernel-6.1-x86_64"
"name_regex" = tostring(null)
"owner_id" = "137112412989"
"owners" = tolist([
"137112412989",
])
"platform" = ""
"platform_details" = "Linux/UNIX"
"product_codes" = toset([])
"public" = true
"ramdisk_id" = ""
"root_device_name" = "/dev/xvda"
"root_device_type" = "ebs"
"root_snapshot_id" = "snap-0846e1db4a7c52e3a"
"sriov_net_support" = "simple"
"state" = "available"
"state_reason" = tomap({
"code" = "UNSET"
"message" = "UNSET"
})
"tags" = tomap({})
"timeouts" = null /* object */
"tpm_support" = ""
"usage_operation" = "RunInstances"
"virtualization_type" = "hvm"
}
ubuntu_ami_details = {
"architecture" = "x86_64"
"arn" = "arn:aws:ec2:ap-northeast-1::image/ami-08e7fa5b787fe2608"
"block_device_mappings" = toset([
{
"device_name" = "/dev/sda1"
"ebs" = tomap({
"delete_on_termination" = "true"
"encrypted" = "false"
"iops" = "0"
"snapshot_id" = "snap-0647e247c4ece4173"
"throughput" = "0"
"volume_size" = "8"
"volume_type" = "gp3"
})
"no_device" = ""
"virtual_name" = ""
},
{
"device_name" = "/dev/sdb"
"ebs" = tomap({})
"no_device" = ""
"virtual_name" = "ephemeral0"
},
{
"device_name" = "/dev/sdc"
"ebs" = tomap({})
"no_device" = ""
"virtual_name" = "ephemeral1"
},
])
"boot_mode" = "uefi-preferred"
"creation_date" = "2024-12-18T09:07:39.000Z"
"deprecation_time" = "2026-12-18T09:07:39.000Z"
"description" = "Canonical, Ubuntu, 24.04, amd64 noble image"
"ena_support" = true
"executable_users" = tolist(null) /* of string */
"filter" = toset([
{
"name" = "architecture"
"values" = toset([
"x86_64",
])
},
{
"name" = "name"
"values" = toset([
"ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*",
])
},
{
"name" = "virtualization-type"
"values" = toset([
"hvm",
])
},
])
"hypervisor" = "xen"
"id" = "ami-08e7fa5b787fe2608"
"image_id" = "ami-08e7fa5b787fe2608"
"image_location" = "amazon/ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-20241218"
"image_owner_alias" = "amazon"
"image_type" = "machine"
"imds_support" = "v2.0"
"include_deprecated" = false
"kernel_id" = ""
"most_recent" = true
"name" = "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-20241218"
"name_regex" = tostring(null)
"owner_id" = "099720109477"
"owners" = tolist([
"099720109477",
])
"platform" = ""
"platform_details" = "Linux/UNIX"
"product_codes" = toset([])
"public" = true
"ramdisk_id" = ""
"root_device_name" = "/dev/sda1"
"root_device_type" = "ebs"
"root_snapshot_id" = "snap-0647e247c4ece4173"
"sriov_net_support" = "simple"
"state" = "available"
"state_reason" = tomap({
"code" = "UNSET"
"message" = "UNSET"
})
"tags" = tomap({})
"timeouts" = null /* object */
"tpm_support" = ""
"usage_operation" = "RunInstances"
"virtualization_type" = "hvm"
}
Shikoan's ML Blogの中の人が運営しているサークル「じゅ~しぃ~すくりぷと」の本のご案内
技術書コーナー
北海道の駅巡りコーナー