How to SSH and run commands in EC2 using boto3? 이 질문을 읽은 후 SSM
을 사용하여 EC2 인스턴스에서 명령을 자동으로 실행하려고합니다. 나는이AWS Boto3 : 요청에 포함 된 보안 토큰이 올바르지 않습니다.
def excute_command_on_instance(client, command, instance_id):
response = client.send_command(
DocumentName="AWS-RunShellScript", # One of AWS' preconfigured documents
Parameters={'commands': command},
InstanceIds=instance_id,
)
return response
# Using SSM in boto3 to send command to EC2 instances.
ssm_client = boto3.client('ssm')
commands = ['echo "hello world']
instance_id = running_instance[0:1]
excute_command_on_instance(ssm_client, commands, instance_id)
같은 코드를 작성할 때, 그것은 그
botocore.exceptions.ClientError: An error occurred (AccessDeniedException) when calling the SendCommand operation: User: arn:aws:iam::62771xxxx946:user/Python_CloudComputing is not authorized to perform: ssm:SendCommand on resource: arn:aws:ec2:eu-west-2:6277xxxx3946:instance/i-074f862c3xxxxfc07
저를 생각 나게한다.
SST
을 사용하여 client
에 대한 자격 증명을 생성하면 아래와 같은 코드가 나타납니다.
def excute_command_on_instance(client, command, instance_id):
response = client.send_command(
DocumentName="AWS-RunShellScript", # One of AWS' preconfigured documents
Parameters={'commands': command},
InstanceIds=instance_id,
)
return response
# Using SSM in boto3 to send command to EC2 instances.
sts = boto3.client('sts')
sts_response = sts.get_session_token()
ACCESS_KEY = sts_response['Credentials']['AccessKeyId']
SECRET_KEY = sts_response['Credentials']['SecretAccessKey']
ssm_client = boto3.client(
'ssm',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
)
commands = ['echo "hello world']
instance_id = running_instance[0:1]
excute_command_on_instance(ssm_client, commands, instance_id)
그러나이 시간은
botocore.exceptions.ClientError: An error occurred (UnrecognizedClientException) when calling the SendCommand operation: The security token included in the request is invalid.
이 사람이 어떻게이 문제를 해결하기 위해 말해 줄 수 있음을 생각 나게?
감사 :이 링크를 사용하면 시스템 관리자에 대한 액세스를 구성하는 데 도움이됩니다
:
아마존은 신속 같은 정책이나 역할에 추가 할 수있는 SSM에 대한 사전 정의 된 정책을 제공합니다 당신의 상세한 답변을! 문제가 해결되었습니다! –