AttributeError ec2 ServiceResource object has no attribute send command

0 votes

Dear all,

I have tried to creating user credentials in windows EC2 instance using boto3 script. But I get an error like below

"AttributeError: 'ec2.ServiceResource' object has no attribute 'send_command"

It's my Boto3 code

#!/usr/bin/env python3

import boto3
import os
import subprocess

ssm_client = boto3.resource('ec2',aws_access_key_id='xxxxxx',aws_secret_access_key='yyyyy', region_name='us-west-2')

session = boto3.Session (profile_name='exampleaws')

response = ssm_client.send_command(
             InstanceIds=[
                "i-xxxxxx" # use instance id on which you want to execute, even multiple is allowd
                     ],
             DocumentName="AWS-RunPowerShellScript",
             Parameters={
                'commands':[
                     'New-LocalUser "Latchu" -Password "ABCD123" -FullName "LakshmiNarayanan" -Description "User-Details'
                       ]
                   },
             )
command_id = response['Command']['CommandId']
output = ssm_client.get_command_invocation(
      CommandId=command_id,
      InstanceId='i-xxxxxx',
    )
print(output)

----

What's the issue?

Oct 7, 2020 in AWS by Lakshminarayanan
• 1,370 points

edited Oct 7, 2020 by MD 5,270 views

1 answer to this question.

+2 votes
Best answer

Hi@Lakshminarayanan,

The send_command attribute is not available in boto3. You can check as shown below.

 import boto3
client = boto3.resource('ec2',aws_access_key_id='xxxxxx',aws_secret_access_key='yyyyy', region_name='us-west-2')
dir(client)
['ClassicAddress', 'DhcpOptions', 'Image', 'Instance', 'InternetGateway', 'KeyPair', 'NetworkAcl', 'NetworkInterface', 'NetworkInterfaceAssociation', 'PlacementGroup', 'Route', 'RouteTable', 'RouteTableAssociation', 'SecurityGroup', 'Snapshot', 'Subnet', 'Tag', 'Volume', 'Vpc', 'VpcAddress', 'VpcPeeringConnection', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'classic_addresses', 'create_dhcp_options', 'create_instances', 'create_internet_gateway', 'create_key_pair', 'create_network_acl', 'create_network_interface', 'create_placement_group', 'create_route_table', 'create_security_group', 'create_snapshot', 'create_subnet', 'create_tags', 'create_volume', 'create_vpc', 'create_vpc_peering_connection', 'dhcp_options_sets', 'disassociate_route_table', 'get_available_subresources', 'images', 'import_key_pair', 'instances', 'internet_gateways', 'key_pairs', 'meta', 'network_acls', 'network_interfaces', 'placement_groups', 'register_image', 'route_tables', 'security_groups', 'snapshots', 'subnets', 'volumes', 'vpc_addresses', 'vpc_peering_connections', 'vpcs']
answered Oct 7, 2020 by MD
• 95,460 points

selected Oct 7, 2020 by Lakshminarayanan

Hi,

what should I use instead of

response = ssm_client.send_command(
             InstanceIds=[
                "i-xxxxxx" # use instance id on which you want to execute, even multiple is allowd
                     ],
             DocumentName="AWS-RunPowerShellScript",
             Parameters={
                'commands':[
                     'New-LocalUser "Latchu" -Password "ABCD123" -FullName "LakshmiNarayanan" -Description "User-Details'
                       ]
                   },
             )

Hi,

I think you just need to add the below line in your code.

ssm_client = boto3.client('ssm')

This module contains the send_command argument.

#!/usr/bin/env python3
import boto3
import os
import subprocess
client = boto3.resource('ec2',aws_access_key_id='xxxxxx',aws_secret_access_key='yyyyy', region_name='us-west-2')

session = boto3.Session (profile_name='exampleaws')
ssm_client = boto3.client('ssm')
response = ssm_client.send_command(
             InstanceIds=[
                "i-xxxxxx" # use instance id on which you want to execute, even multiple is allowd
                     ],
             DocumentName="AWS-RunPowerShellScript",
             Parameters={
                'commands':[
                     'New-LocalUser "Latchu" -Password "ABCD123" -FullName "LakshmiNarayanan" -Description "User-Details'
                       ]
                   },
             )
command_id = response['Command']['CommandId']
output = ssm_client.get_command_invocation(
      CommandId=command_id,
      InstanceId='i-xxxxxx',
    )
print(output)

yes, but its getting error like below

    raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidInstanceId: An error occurred (InvalidInstanceId) when calling the SendCommand operation:

Even I mentioned exact instance id.

Hi,

Ok. Check your region name properly because EC2 is region-specific. Just cross-check id, region. Your code is totally ok.

Everything is properly configured. but still saying "invalid instance id"
Ok, do one thing first print all the instance id using boto3. Check what it is showing.
Finally created user. Just i have used "net user username "password" /ADD " in powershell script. its created successfully. Many thanks for your support.

Related Questions In AWS

0 votes
1 answer

No package python3 available in AWS EC2.

Hi@akhtar, It seems the python3 package is not ...READ MORE

answered Dec 8, 2020 in AWS by MD
• 95,460 points
1,413 views
+5 votes
2 answers

EC2 instance status check failed

Creating a snapshot from an EBS volume ...READ MORE

answered Mar 27, 2018 in AWS by DragonLord999
• 8,450 points
3,014 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,820 points
2,868 views
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 58,020 points
5,958 views
0 votes
1 answer

Pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

Python String and Integer concatenation >>> print("arn:aws:swf:us-east-2:{0}:action/actions/AWS_EC2.InstanceId.Stop/1.0".format(acccnum)) arn:aws:swf:us-east-2:12312312312312:action/actions/AWS_EC2.InstanceId.Stop/1.0 >>> print("arn:aws:swf:us-east-2:" ...READ MORE

answered Oct 5, 2018 in AWS by Priyaj
• 58,020 points
1,583 views
0 votes
1 answer

dynamodb.ServiceResource' object has no attribute 'exceptions'.

Hi@akhtar, You'll need to use boto3.client('dynamodb') to be able to ...READ MORE

answered Jul 13, 2020 in AWS by MD
• 95,460 points
14,624 views
0 votes
1 answer

Error: 'No package Tableau server available on the EC2 Linux instance'

Hi, Tableau may not come with your ISO ...READ MORE

answered Oct 20, 2020 in AWS by MD
• 95,460 points
813 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP