0

Iā€™m trying to use Python to create EC2 instances but I keep getting these errors.

Here is my code:

#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
    ImageId='ami-0922553b7b0369273',
    MinCount=1,
    MaxCount=1,
    InstanceType='t2.micro')
print instance[0].id

Here are the errors I'm getting

Traceback (most recent call last):
  File "./createinstance.py", line 8, in <module>
    InstanceType='t2.micro')
  File "/usr/lib/python2.7/site-packages/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/usr/lib/python2.7/site-packages/botocore/client.py", line 320, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/lib/python2.7/site-packages/botocore/client.py", line 623, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAMIID.NotFound) when calling the RunInstances operation: The image id '[ami-0922553b7b0369273]' does not exist

I also get an error when trying to create a key pair

Here's my code for creating the keypair

  import boto3
ec2 = boto3.resource('ec2')

# create a file to store the key locally
outfile = open('ec2-keypair.pem','w')

# call the boto ec2 function to create a key pair
key_pair = ec2.create_key_pair(KeyName='ec2-keypair')

# capture the key and store it in a file
KeyPairOut = str(key_pair.key_material)
print(KeyPairOut)
outfile.write(KeyPairOut)

response = ec2.instance-describe()
print response

Here's are the error messages

./createkey.py: line 1: import: command not found
./createkey.py: line 2: syntax error near unexpected token `('
./createkey.py: line 2: `ec2 = boto3.resource('ec2')'

What I am I missing?

John Rotenstein
  • 165,783
  • 13
  • 223
  • 298
  • The error message in the first issue seems very clear. You've asked to launch from ami-0922553b7b0369273 but that's not available to you (perhaps it's a valid AMI ID for a different region, or it's the ID of a legacy AMI that no longer exists). For the second issue, why haven't you started your Python script in the same way as your (working) first script? ā€“ jarmod Oct 12 '18 at 21:04

1 Answers1

1

For your first script, one of two possibilities could be occurring: 1. The AMI you are referencing by the ID is not available because the key is incorrect or the AMI doesn't exist 2. AMI is unavailable in the region that your machine is setup for

You most likely are running your script from a machine that is not configured for the correct region. If you are running your script locally or on a server that does not have roles configured, and you are using the aws-cli, you can run the aws configure command to configure your access keys and region appropriately. If you are running your instance on a server with roles configured, your server needs to be ran in the correct region, and your roles need to allow access to EC2 AMI's.

For your second question (which in the future should probably be posted separate), your syntax error in your script is a side effect of not following the same format for how you wrote your first script. It is most likely that your python script is not in fact being interpreted as a python script. You should add the shebang at the top of the file and remove the spacing preceding your import boto3 statement.

#!/usr/bin/env python
import boto3
# create a file to store the key locally
outfile = open('ec2-keypair.pem','w')

# call the boto ec2 function to create a key pair
key_pair = ec2.create_key_pair(KeyName='ec2-keypair')

# capture the key and store it in a file
KeyPairOut = str(key_pair.key_material)
print(KeyPairOut)
outfile.write(KeyPairOut)

response = ec2.instance-describe()
print response
Preston Martin
  • 2,173
  • 2
  • 21
  • 36
  • I copied and pasted the AMI directly from AWS and I'm running the script on a VM hosted locally. I did an aws configure and supplied the correct access keys and region. My aws user has full ec2 access. I have not messed with the roles at all. As for the second script, good catch, I'll try it again with the shebang. ā€“ hotsauceonerrything Oct 14 '18 at 03:39
  • For the first case, can you try to provision an EC2 instance from a public AMI such as the AWS Linux 2 or Ubuntu image? Do you still receive the same error? ā€“ Preston Martin Oct 14 '18 at 04:07