0

Issue with parsing string in python boto3 code

In my lambda function has following code

    instance_id = "'" + str(instance[0]) + "'"

    ssmresponse = ssm_client.send_command(InstanceIds=[ instance_id ],DocumentName='AWS- 
      RunShellScript',Parameters={'commands': ['ls -l']}, )
    command_id = ssmresponse['Command']['CommandId']

if I specify the instance_id as

 instance_id = str(instance[0])

send_command is erroring out with

"WARN: Exception occured An error occurred (ValidationException) when calling the SendCommand operation: 1 validation error detected: Value '[ec2.Instance(id='i-0xxxxxxxxxxxxxxx')]' at 'instanceIds' failed to satisfy constraint: Member must satisfy constraint: [Member must have length less than or equal to 20, Member must have length greater than or equal to 10, Member must satisfy regular expression pattern: (^i-(\w{8}|\w{17})$)|(^mi-\w{17}$)]."

if I specify instance_id as

instance_id = "'" + str(instance[0]) + "'"

send_command is erroring out with

"WARN: Exception occured An error occurred (ValidationException) when calling the SendCommand operation: 1 validation error detected: Value '['ec2.Instance(id='xxxxxxxxxxxxxxxxxx')']' at 'instanceIds' failed to satisfy constraint: Member must satisfy constraint: [Member must have length less than or equal to 20, Member must have length greater than or equal to 10, Member must satisfy regular expression pattern: (^i-(\w{8}|\w{17})$)|(^mi-\w{17}$)]."

When I hardcode the instance id within the send_command, there are no errors

ssmresponse = ssm_client.send_command(InstanceIds=[ 'i-xxxxxxxxxxxxxxxx' ],DocumentName='AWS-RunShellScript',Parameters={'commands': ['ls -l']}, )

there are no errors

I need to dynamically set the instance_id. What could possibly be wrong in this?

Any help is appreciated.

Thanks D

Daniel
  • 605
  • 1
  • 8
  • 19
  • Can you `print(instance[0])`? – Marcin Mar 22 '22 at 04:40
  • I get an exception for it WARN: Exception occured can only concatenate str (not "ec2.Instance") to str . When I try this print("\ninstance[0].id : " + instance[0].id) , it does not throw an exception, but it prints following instance[0].id : i-04f15611751dbb33a – Daniel Mar 23 '22 at 20:13
  • print(instance[0]) , shows "ec2.Instance(id='i-0a19b21b9f64db8c4')" , When I use ssmresponse = ssm_client.send_command(InstanceIds=[ instance[0].id ],DocumentName='AWS-RunShellScript',Parameters={'commands': ['echo hostname']}, ) , I get exception "WARN: Exception occured An error occurred (InvalidInstanceId) when calling the SendCommand operation:" – Daniel Mar 23 '22 at 20:37

1 Answers1

0

I am guessing you are first querying instances using Ec2 Service Resource and getting back a resource object, which would have instance[0].id as "i-123..." but when you specify it just as 'instance[0], it is simply returning the first instance "object"

For context, boto3 has clients and resources. When dealing with client, you get back text (mostly json) but resource returns pythonic 'objects'.

Service Resource: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#service-resource

vs Client: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#client

In case it is still not clear, use: (InstanceIds=[ instance[0].id ]

Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28
  • Thanks Adil, I used ssmresponse = ssm_client.send_command(InstanceIds=[ instance[0].id ],DocumentName='AWS-RunShellScript',Parameters={'commands': ['echo hostname']}, ) and it is throwing exception "WARN: Exception occured An error occurred (InvalidInstanceId) when calling the SendCommand operation:" – Daniel Mar 23 '22 at 20:39
  • @Daniel - How do you get the `instance` variable before you arrive at ssmresponse? Some versions of boto3 would have you use instance[0].instance_id instead of instance[0[.id. Is it possible to enumerate what you are getting back and print both to see what you are working with? – Adil Hindistan Mar 23 '22 at 21:09
  • Thanks, I have tried following code instance[0].instance_id value is i-0164f232ffce15fc4 . When I tried code ssmresponse = ssm_client.send_command(InstanceIds=[ instance[0].instance_id ],DocumentName='AWS-RunShellScript',Parameters={'commands': ['echo hostname']}, ), I get following exception WARN: Exception occured An error occurred (InvalidInstanceId) when calling the SendCommand operation: – Daniel Mar 23 '22 at 21:38
  • so your problem is not instance id. You are providing the correct value for the instance id at this point. See if any of these apply? https://stackoverflow.com/questions/47034797/invalidinstanceid-an-error-occurred-invalidinstanceid-when-calling-the-sendco – Adil Hindistan Mar 23 '22 at 21:44