【问题标题】:AttributeError: 'ec2.ServiceResource' object has no attribute 'send_command'AttributeError:“ec2.ServiceResource”对象没有属性“send_command”
【发布时间】:2020-10-07 16:41:23
【问题描述】:

我尝试使用 Boto3 AWS-RunPowerShellScript 在 AWS EC2 Windows 实例中创建用户凭证。但是当我尝试运行下面的代码时,出现错误“AttributeError:'ec2.ServiceResource'对象没有属性'send_command'”。

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)```

【问题讨论】:

  • 嗨。我注意到您有很多问题,其中大多数都提供了答案。但是,您尚未接受其中任何一个。接受好的答案是一种很好的做法,因为它可以帮助遇到相同/相似问题的其他人。如果不接受正确的答案,它会提示人们在错误的假设下提出重复的问题,即对您的问题的现有答案没有帮助。
  • 确定。以后我不会那样做了。如果提供的答案没有解决我的问题,那我该怎么办?
  • 您通常会评论为什么答案不能解决问题。这样,回答的人就有机会修改它,其他人可以根据评论提供更有帮助的答案。
  • 好的。我一定会做到的。非常感谢

标签: python amazon-ec2 boto3


【解决方案1】:

您的ssm_client 不正确,因为send_commandssm 的属性,而不是ec2。因此,它应该是:

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

您的session 也根本没有使用。仍然可能存在其他尚不明显的错误。

【讨论】:

  • 它运行没有错误。我得到低于输出。但用户不是在 Windows Ec2 实例中创建的。
  • @LakshminarayananS 这一定与您的RunPowerShellScript 有关。可悲的是,我不知道如何解决这个问题。我的回答与发布的有关“AttributeError:'ec2.ServiceResource'”的原始问题有关。
  • 输出看起来不错,但用户未在 EC2 实例中创建。 HTTPStatusCode': 200
  • 我会检查我的 Powershell 脚本,一旦解决了我就会在这里发布
  • 最终脚本在创建用户时执行良好,我使用了 powershell 命令:net user username "Password" /ADD .
猜你喜欢
  • 2021-09-24
  • 2016-03-28
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
相关资源
最近更新 更多