Thursday, March 17, 2022

[SOLVED] How to find the list of applicable instance types for an AMI

Issue

Used ami-0fd3c3c68a2a8066f from ap-south-1 region http://cloud-images.ubuntu.com/locator/ec2/, but unable to use t2.micro instance type against this.

Error: Error launching source instance: InvalidParameterValue: The architecture 'x86_64' of the specified instance type does not match the architecture 'arm64' of the specified AMI. Specify an instance type and an AMI that have matching architectures, and try again. You can use 'describe-instance-types' or 'describe-images' to discover the architecture of the instance type or AMI.

How to find the list of applicable instance types for an AMI, before trying to launch an instance using terraform


Solution

Using AWS CLI you can use describe-instance-types:

aws ec2 describe-instance-types --filters Name=processor-info.supported-architecture,Values=arm64 --query "InstanceTypes[*].InstanceType" --output text

E.g. output:

r6gd.large  m6g.metal   m6gd.medium c6gd.metal  m6gd.12xlarge   c6g.16xlarge    r6g.large   r6gd.medium r6g.8xlarge m6gd.metal  r6gd.xlarge t4g.medium  r6gd.2xlarge    m6gd.xlarge c6g.xlarge  c6g.12xlarge    r6g.medium  a1.medium   m6g.xlarge  m6gd.4xlarge    t4g.nano    r6g.16xlarge
t4g.2xlarge m6g.12xlarge    r6gd.8xlarge    a1.large    m6g.4xlarge c6gd.16xlarge   t4g.xlarge  c6g.large   m6g.large   c6gd.xlarge a1.metal    m6g.8xlarge m6gd.16xlarge   a1.xlarge   r6g.12xlarge    r6gd.metal  t4g.micro   r6g.4xlarge t4g.small   a1.2xlarge  r6gd.4xlarge    t4g.large
m6g.16xlarge    c6g.4xlarge m6gd.2xlarge    c6gd.medium c6gd.8xlarge    r6gd.16xlarge   m6gd.8xlarge    c6g.2xlarge r6gd.12xlarge   a1.4xlarge  c6g.8xlarge r6g.2xlarge m6g.2xlarge m6g.medium  c6gd.large  c6g.medium  c6gd.2xlarge    r6g.metal   c6gd.4xlarge    m6gd.large  r6g.xlarge

I don't see any equivalent in TF for that. In the worst case, you could define external data source for that.

update

There is no single call to get the list of instance types based on ami. It must be done in two steps.

  1. Use aws_ami data source to get architecture of a given AMI.
  2. Use describe-instance-types to get instance types for that architecture.


Answered By - Marcin
Answer Checked By - Pedro (WPSolving Volunteer)