ss321c
ss321c

Reputation: 799

creating amazon ec2 instance can't assign to operator error boto3

I am trying to create 2 instances of linux on amazon ec2 there is one instance which I am starting in script

import sys
import boto3


instance_id = "i-03e7f6391a0f523ee"
action = 'ON'

ec2 = boto3.client('ec2')

if action == 'ON':
   response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)
   x2=boto3.resource('ec2')
   instance=x2.Instance(instance_id)
   foo=instance.wait_until_running('self',Filters=[{'Name':'instance-state-name','Values':['running']}])
   print ("instance is now running")
else:
    response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False)
print(response)

ec2 = boto3.resource('ec2', region_name="ap-south-1")
ami-ImageId="ami-00b6a8a2bd28daf19"
#creating instances
ec2.create_instances(ImageId=ami-ImageId,MinCount=1,MaxCount=2,KeyName="datastructutre key",InstanceType="t2.micro")
#using filter to retrive instance status
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in instances:
    print(instance.id, instance.instance_type)
#Checking health status of instances
for status in ec2.meta.client.describe_instance_status()['InstanceStatuses']:
    print(status)

However this line

ami-ImageId="ami-00b6a8a2bd28daf19"

is giving me errors instead of double quotes I have also tried single quotes

ami-ImageId='ami-00b6a8a2bd28daf19'

but in both the cases error is same, the error is

Syntax error: can't assign to operator

What is the mistake in assigning ami-id in this way? It is normal string being assigned to a variable.

Upvotes: 0

Views: 136

Answers (2)

AlexL
AlexL

Reputation: 355

The expression "ami-ImageId" is a substraction of ami and ImageId variables but not a correct single variable name. Use ami_ImageId correct name for example.

Upvotes: 0

k.wahome
k.wahome

Reputation: 1062

The problem is not in the assignment. It's in the variable name. The - in ami-ImageId is an operator. Use a different variable name e.g. ami_ImageId

Upvotes: 1

Related Questions