【问题标题】:How to run shell script on multiple EC2 instances using AWS CLI?如何使用 AWS CLI 在多个 EC2 实例上运行 shell 脚本?
【发布时间】:2018-04-07 23:25:01
【问题描述】:

我正在尝试执行一个简单的 AWS CLI 命令,该命令可以对多个实例运行 shell 命令。

我知道首先我需要获取实例 ID 列表:

aws ec2 describe-instances --filter "Name=tag:Group,Values=Development" --query 'Reservations[].Instances[].[InstanceId]' --output text

然后我必须将它们分配给一个数组。然后遍历每个实例 id 并发送命令。

我们是否可以选择让 aws 向具有特定 ID 的实例发送 shell 命令?

类似这样的:

aws ssm send-command --instance-ids "i-xxxxxxxxxxxxxxxx" --document-name "shellscript"

我不断收到此错误:

调用 SendCommand 操作时发生错误(InvalidInstanceId):

我已确保 SSM 代理正在该特定实例上运行,并根据 these docs pages 确保一切正确。

【问题讨论】:

标签: shell amazon-web-services


【解决方案1】:

您可以使用ssm send-command

查看实例IP地址的示例命令:

aws ssm send-command --instance-ids "your id's" --document-name "AWS-RunShellScript" --comment "IP config" --parameters "commands=ifconfig" --output text

根据需要修改命令。


如果您遇到错误,则可能是在您尝试访问的实例上没有设置 SSM 时发生这种情况。有关可以运行 SSM 命令的实例列表,请运行:

aws ssm describe-instance-information --output text

参见:InvalidInstanceId: An error occurred (InvalidInstanceId) when calling the SendCommand operation

【讨论】:

  • 我已尝试过该命令,但一直收到此错误:“调用 SendCommand 操作时发生错误 (InvalidInstanceId)”
  • 什么错误,请发布或者如果它是更大的编辑问题
  • 更新了答案
  • 您必须将实例 ID 作为单独的字符串值传递 --instance-ids 等等,而不是 --instance-ids "”。引用此处显示的 id 列表会将它们全部作为一个字符串传递。
【解决方案2】:

我能够使用 Boto3 使用 Python 创建脚本。

import boto3
import botocore
import paramiko

tagkey = 'Environment'
tagvalue = 'DEV'

# list_instances functions returns a list of ip addresses containing a set of tags
def list_instances(tagkey, tagvalue):

    ec2client = boto3.client('ec2')

    response = ec2client.describe_instances(
        Filters=[
            {
                'Name': 'tag:'+tagkey,
                'Values': [tagvalue]
            }
       ]
    )
    instancelist = []
    for reservation in (response["Reservations"]):
        for instance in reservation["Instances"]:
            instancelist.append(instance["PublicDnsName"])
    return instancelist

# Results of the function get stored in a list.
list = list_instances(tagkey, tagvalue)

key = paramiko.RSAKey.from_private_key_file("/home/ec2-user/key.pem")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Looping through all the instannces in the list
for instance_ip in list[:]:
                # Connect/ssh to an instance
    try:
                # Here 'ec2-user' is user name and 'instance_ip' is public IP of EC2
                client.connect(hostname=instance_ip, username="ec2-user", pkey=key)

                # Execute a command after connecting/ssh to an instance
                stdin, stdout, stderr = client.exec_command("touch test")

                # close the client connection once the job is done
                print "Command sent:",instance_ip

    except Exception, e:
        print e

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2023-03-13
    • 2019-03-13
    • 2020-02-06
    • 2020-01-13
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多