Search code examples
pythonamazon-web-servicesboto3amazon-ami

Boto3/AWS: "ImageId doesn't exist" when creating Instance


I've made a copy of an AMI and I'm trying to run it with this code:

import boto3

instance_id=("i-0e2bbdf4fc43bf6db")

client = boto3.client("ec2",region_name="us-west-2")
ec2 = boto3.resource("ec2")

ec2.create_instances(ImageId="ami-9d623ee5",MinCount=1,MaxCount=1)

A ClientError is returned:

ClientError: An error occurred (InvalidAMIID.NotFound) when calling the RunInstances operation: The image id '[ami-9d623ee5]' does not exist

What could the issue be? Thanks!


Solution

  • Assuming that the AMI is in the same region ...

    Your code is wrong: Change to be similar to the following:

    import boto3
    
    instance_id=("i-0e2bbdf4fc43bf6db")
    
    session = boto3.Session("ec2",region_name="us-west-2")
    ec2 = session.resource("ec2")
    
    OR
    
    ec2 = boto3.resource('ec2', region_name='us-west-2')
    
    ec2.create_instances(ImageId="ami-9d623ee5",MinCount=1,MaxCount=1)