2017-10-28 8 views
1

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.

이 사람이 어떻게이 문제를 해결하기 위해 말해 줄 수 있음을 생각 나게?

답변

1

IAM 사용자 또는 SSM에 액세스하는 역할에 대한 사용 권한이 없습니다.

STS를 사용하여 수행해야 할 작업을 복잡하게하는 액세스 권한을 얻으려고합니다. STS가 가정해야하는 정책에는 동일한 사용 권한이 필요합니다. STS (최소 권한의 규칙)를 사용하는 좋은 사례가 많이 있지만 STS가 필요 없다고 생각합니다.

Configuring Access to Systems Manager

+0

감사 :이 링크를 사용하면 시스템 관리자에 대한 액세스를 구성하는 데 도움이됩니다

AmazonEC2RoleForSSM AmazonSSMFullAccess AmazonSSMReadOnlyAccess 

:

아마존은 신속 같은 정책이나 역할에 추가 할 수있는 SSM에 대한 사전 정의 된 정책을 제공합니다 당신의 상세한 답변을! 문제가 해결되었습니다! –