CodeTalker
CodeTalker

Reputation: 1791

How to get AWS EC2 AMI image-id of a given operating system using Java sdk describe instances api

How can I get AWS Ec2 image id using describe instance Java SDK api for a given operating system which is free tier as well for the given region? I tried to use the describe Instance api but I don't know how to use Filters.

AmazonEC2ClientBuilder.defaultClient().describeImages(new DescribeImagesRequest().withFilters(Arrays.asList(
        new Filter().withName("is-public").withValues("true"),
        new Filter().withName("some name i dont know").withValues("some values i dont know")
)))

I am struggling with getting an image Id, it returns a lot of images using only is-public filter. I want to get free tier eligible instances of core os only, no extra software installed like SQL server, etc. I tried this documentation but couldn't understand much.

To be very precise, I want to get the AMI ids of images which are given at AWS console while launching an Instance marked as free tier eligible as shown in image below

ec2 amis in aws console

Please give me a example code snippet for getting a ubuntu 18.04 LTS ami-id of this free tier eligible Image shown in image above i.e. ami-02d55cb47e83a99a0. And suggest how can i change it to get AMI id for some another images like Suse or debian or redhat etc.

P.S:

I suppose these AMI ids are different for each region even for same operating system, I think the solution should include something like region for getting image id in that region. In the solution, please include the solution for ap-south-1 as this screenshot is for this region.

Edit 1

I know it has something to do with Filtering name or description. I can use wildcards in the withValue(), like i want to search for AMI like new Filter().withName("description").withValue("*ubuntu-18.04*") where * is wildcard character. But it returns all the AMIs which contains ubuntu-18.04 in description, including all those paid AMIS which have existing software installed. How can I filter to get only free tier eligible, basic, default ubuntu image.

Upvotes: 1

Views: 4308

Answers (1)

Harish KM
Harish KM

Reputation: 1353

Being free-tier eligible has more to do with the instance type, not so much with the AMI itself. By that logic, if you could find all AMIs that will work with a free-tier eligible instance type like t2.micro, it should serve your purpose. Fortunately, AWS Marketplace search has an Instance Type filter. The following Marketplace URL finds free AMIs by AWS in ap-south-1 compatible with t2.micro:

https://aws.amazon.com/marketplace/search?page=1&filters=VendorId%2CPricingPlan%2CFulfillmentOptionType%2CRegion%2CAmi::InstanceType&VendorId=e6a5002c-6dd0-4d1e-8196-0a1d1857229b&PricingPlan=Free&FulfillmentOptionType=Ami&Region=ap-south-1&Ami::InstanceType=t2.micro

Do the same using AWS Java SDK to get the AMIs programmatically.

Upvotes: 1

Related Questions