1

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.

CodeTalker
  • 1,683
  • 2
  • 21
  • 31
  • If you do not know the value then I think you need query instead of filter , here is the aws-cli command convert it accordingly `aws ec2 describe-instances --query "Reservations[].Instances[].ImageId"` – Adiii Jul 19 '20 at 11:52
  • @Adiii Hi, the command you provided, seems like it gives the AMI ids of the instances in my account. I want to know the AMI ids before launching the instance, assuming there is no instance present. Let's say i want to get an free ami id of an ubuntu image? How can I get it using describe image api or any other way if possible. – CodeTalker Jul 19 '20 at 12:35
  • @AWS PS Can you please throw some light on this? – CodeTalker Jul 19 '20 at 12:49
  • I see, then you need `describe image` API. Here is the most official ubuntu distribution that you can filter. `aws ec2 describe-images --filters "Name=owner-id,Values=099720109477"` or get all images `aws ec2 describe-images` – Adiii Jul 19 '20 at 12:51
  • Alright, now I see something that can be useful. Can you please tell me what this `099720109477` owner-id corresponds to? It is official AWS owner and provides free tier AMI ids? – CodeTalker Jul 19 '20 at 12:54
  • The account that manage the ami, I think you'll get some thing about the account may be here but I'm not sure https://docs.aws.amazon.com/cli/latest/reference/organizations/describe-account.html – Adiii Jul 19 '20 at 13:17

1 Answers1

1

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.

Harish KM
  • 1,303
  • 7
  • 17