You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Java SDK的DescribeImages API获取指定区域免费层OS的EC2 AMI ID

Great question! When trying to fetch free-tier eligible EC2 AMIs via the AWS Java SDK, the trick is to use precise, targeted filters that eliminate third-party or paid images while focusing on official base OS builds. Let's walk through a working solution for Ubuntu 18.04 LTS, then cover how to adapt it for other systems and regions.

Solution for Ubuntu 18.04 LTS

The key filters here are:

  • owner-id: Targets Canonical's official Ubuntu images (avoids random third-party builds)
  • name: Uses Canonical's standardized naming pattern to match only base Ubuntu 18.04 LTS HVM SSD images
  • product-code.type: Explicitly filters for free-tier eligible images (this is what was missing from your initial attempt!)
  • is-public: Ensures we only pull publicly available official images

Here's the complete Java code example:

import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.DescribeImagesRequest;
import com.amazonaws.services.ec2.model.DescribeImagesResult;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Image;

import java.util.Arrays;
import java.util.List;

public class FreeTierAmiFetcher {
    public static void main(String[] args) {
        // Configure your target region (e.g., ap-south-1 for Mumbai)
        AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
                .withRegion(Regions.AP_SOUTH_1)
                .build();

        DescribeImagesRequest amiRequest = new DescribeImagesRequest()
                .withFilters(Arrays.asList(
                        // Canonical's official owner ID for Ubuntu images
                        new Filter("owner-id", Arrays.asList("099720109477")),
                        // Match official Ubuntu 18.04 LTS HVM SSD x86_64 images
                        new Filter("name", Arrays.asList("ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*")),
                        // Ensure image is publicly accessible
                        new Filter("is-public", Arrays.asList("true")),
                        // Critical filter for free-tier eligibility
                        new Filter("product-code.type", Arrays.asList("free-tier-eligible"))
                ));

        DescribeImagesResult result = ec2Client.describeImages(amiRequest);
        List<Image> eligibleAmis = result.getImages();

        // Sort to get the latest published image (most up-to-date patches)
        eligibleAmis.sort((img1, img2) -> img2.getCreationDate().compareTo(img1.getCreationDate()));

        if (!eligibleAmis.isEmpty()) {
            Image latestFreeTierAmi = eligibleAmis.get(0);
            System.out.println("Latest Free-Tier Ubuntu 18.04 LTS AMI ID: " + latestFreeTierAmi.getImageId());
            System.out.println("AMI Name: " + latestFreeTierAmi.getName());
        } else {
            System.out.println("No free-tier eligible Ubuntu 18.04 LTS AMIs found in the specified region.");
        }

        ec2Client.shutdown();
    }
}

Adapting for Other Operating Systems

To fetch free-tier eligible AMIs for other OSes, adjust the owner-id and name filters to match the official publisher's details:

SUSE Linux Enterprise Server (SLES)

  • Owner ID: 137112412989
  • Name filter example: "suse/sles-15-sp*-hvm-ssd-x86_64-*" (update the version wildcard as needed)
  • Note: SUSE offers free-tier eligible images for specific supported versions.

Debian

  • Owner ID: 136693071363
  • Name filter example: "debian/images/debian-10-*-amd64-hvm-ssd" (for Debian 10) or "debian/images/debian-11-*-amd64-hvm-ssd" (for Debian 11)

Red Hat Enterprise Linux (RHEL)

  • Owner ID: 309956199498
  • Name filter example: "RHEL-8.*-x86_64-HVM-ssd"
  • Important: RHEL's free-tier eligibility has restrictions (e.g., single instance, non-production use). Verify the product-code.type filter still applies, or cross-check with the AWS Console if you get no results.

Handling Regional Differences

  • Client Configuration: Always initialize the EC2 client with your target region using withRegion(Regions.YOUR_REGION) (e.g., Regions.US_EAST_1, Regions.EU_WEST_1).
  • AMI ID Uniqueness: AMI IDs are region-specific—same OS versions will have different IDs across regions. The filters above automatically fetch the correct AMI for your configured region.
  • Naming Variations: Some regions might have minor naming differences for official images. If your filter returns no results, cross-reference the official AMI names in the AWS EC2 Console for that region and adjust the name filter accordingly.

By combining official owner IDs, precise naming patterns, and the product-code.type filter, you'll reliably retrieve only free-tier eligible base OS images, avoiding paid builds with extra software like SQL Server.

内容的提问来源于stack exchange,提问作者CodeTalker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:57:57