연습: Run Command에서 AWS CLI 사용 - AWS Systems Manager

연습: Run Command에서 AWS CLI 사용

다음 예제 시연에서는 AWS Command Line Interface(AWS CLI)를 사용하여 명령 및 명령 파라미터에 대한 정보를 보는 방법, 명령을 실행하는 방법, 해당 명령의 상태를 보는 방법을 보여줍니다.

중요

신뢰할 수 있는 관리자만 이번 주제에서 언급하는 AWS Systems Manager 사전 구성 문서를 사용할 수 있도록 허용해야 합니다. Systems Manager 문서에서 지정하는 명령 또는 스크립트는 관리형 노드에 대한 관리자 권한으로 실행됩니다. 미리 정의된 Systems Manager 문서(AWS-로 시작하는 모든 문서)를 실행할 권한이 있는 사용자는 해당 노드에 대한 관리자 권한도 보유합니다. 그 밖의 모든 사용자들에 대해서는 제한된 문서를 생성하여 그 문서를 특정 사용자와 공유해야 합니다.

1단계: 시작하기

구성할 관리형 노드에 대한 관리자 권한이 있거나 AWS Identity and Access Management(IAM)에서 적절한 권한을 부여받아야 합니다. 또한 이 예제에서는 미국 동부(오하이오) 리전(us-east-2)을 사용합니다. Run Command는 Amazon Web Services 일반 참조의 Systems Manager 서비스 엔드포인트에 나열된 AWS 리전에서 사용할 수 있습니다. 자세한 내용은 AWS Systems Manager 설정 단원을 참조하십시오.

AWS CLI을 사용하여 명령을 실행하려면
  1. 아직 하지 않은 경우 AWS Command Line Interface(AWS CLI)를 설치하고 구성합니다.

    자세한 내용은 최신 버전의 AWS CLI 설치 또는 업데이트를 참조하세요.

  2. 사용할 수 있는 모든 문서를 나열합니다.

    이 명령을 실행하면 IAM 권한에 따라 계정에 사용할 수 있는 문서를 모두 나열합니다.

    aws ssm list-documents
  3. 관리형 노드가 명령을 수신할 준비가 되었는지 확인합니다.

    다음 명령의 출력은 관리형 노드가 온라인 상태인지 여부를 보여줍니다.

    Linux & macOS
    aws ssm describe-instance-information \ --output text --query "InstanceInformationList[*]"
    Windows
    aws ssm describe-instance-information ^ --output text --query "InstanceInformationList[*]"
  4. 다음 명령을 실행하여 특정 관리형 노드에 대한 세부 정보를 봅니다.

    참고

    이 시연에서 명령을 실행하려면 인스턴스 및 명령 ID를 바꿉니다. 관리형 AWS IoT Greengrass 코어 디바이스에서, 인스턴스 ID로 mi-ID_number를 사용합니다. send-command의 응답으로 명령 ID가 반환됩니다. AWS Systems Manager의 기능인 Fleet Manager에서 인스턴스 ID를 사용할 수 있습니다.

    Linux & macOS
    aws ssm describe-instance-information \ --instance-information-filter-list key=InstanceIds,valueSet=instance-ID
    Windows
    aws ssm describe-instance-information ^ --instance-information-filter-list key=InstanceIds,valueSet=instance-ID

2단계: 셸 스크립트를 실행하여 리소스 세부 정보 보기

Run Command 및 AWS-RunShellScript 문서를 사용하면 로컬로 로그인한 것처럼 관리형 노드에서 명령이나 스크립트를 실행할 수 있습니다.

설명 및 사용 가능한 파라미터 보기

다음 명령을 실행하여 Systems Manager JSON 문서에 대한 설명을 봅니다.

Linux & macOS
aws ssm describe-document \ --name "AWS-RunShellScript" \ --query "[Document.Name,Document.Description]"
Windows
aws ssm describe-document ^ --name "AWS-RunShellScript" ^ --query "[Document.Name,Document.Description]"

다음 명령을 실행하여 사용 가능한 파라미터 및 해당 파라미터에 대한 세부 정보를 봅니다.

Linux & macOS
aws ssm describe-document \ --name "AWS-RunShellScript" \ --query "Document.Parameters[*]"
Windows
aws ssm describe-document ^ --name "AWS-RunShellScript" ^ --query "Document.Parameters[*]"

3단계: AWS-RunShellScript 문서를 사용하여 간단한 명령 전송

다음 명령을 실행하여 관리형 노드에 대한 IP 정보를 가져옵니다.

Windows Server 관리형 노드를 대상으로 지정했다면, document-nameAWS-RunPowerShellScript로 바꾸고 commandifconfig에서 ipconfig로 바꿉니다.

Linux & macOS
aws ssm send-command \ --instance-ids "instance-ID" \ --document-name "AWS-RunShellScript" \ --comment "IP config" \ --parameters commands=ifconfig \ --output text
Windows
aws ssm send-command ^ --instance-ids "instance-ID" ^ --document-name "AWS-RunShellScript" ^ --comment "IP config" ^ --parameters commands=ifconfig ^ --output text
응답 데이터와 함께 명령 정보 가져오기

다음 명령은 이전 명령에서 반환된 명령 ID를 사용하여 명령 실행의 세부 정보와 응답 데이터를 가져옵니다. 명령이 완료되면 시스템에서 응답 데이터가 반환됩니다. 명령 실행에서 "Pending" 또는 "InProgress"가 표시되는 경우, 이 명령을 다시 실행하여 응답 데이터를 확인하십시오.

Linux & macOS
aws ssm list-command-invocations \ --command-id $sh-command-id \ --details
Windows
aws ssm list-command-invocations ^ --command-id $sh-command-id ^ --details

사용자 식별

다음 명령은 명령을 실행하는 기본 사용자를 표시합니다.

Linux & macOS
sh_command_id=$(aws ssm send-command \ --instance-ids "instance-ID" \ --document-name "AWS-RunShellScript" \ --comment "Demo run shell script on Linux managed node" \ --parameters commands=whoami \ --output text \ --query "Command.CommandId")
명령 상태 가져오기

다음 명령은 명령 ID를 사용하여 관리형 노드에서 명령 실행 상태를 가져옵니다. 이 예에서는 이전 명령에서 반환된 명령 ID를 사용합니다.

Linux & macOS
aws ssm list-commands \ --command-id "command-ID"
Windows
aws ssm list-commands ^ --command-id "command-ID"
명령 세부 정보 가져오기

다음 명령은 이전 명령의 명령 ID를 사용하여 관리형 노드 단위로 명령 실행 상태를 가져옵니다.

Linux & macOS
aws ssm list-command-invocations \ --command-id "command-ID" \ --details
Windows
aws ssm list-command-invocations ^ --command-id "command-ID" ^ --details
특정 관리형 노드에 대한 응답 데이터로 명령 정보 가져오기

다음 명령은 특정 관리형 노드에 대한 원래 aws ssm send-command 요청을 반환합니다.

Linux & macOS
aws ssm list-command-invocations \ --instance-id instance-ID \ --command-id "command-ID" \ --details
Windows
aws ssm list-command-invocations ^ --instance-id instance-ID ^ --command-id "command-ID" ^ --details

Python 버전 표시

다음 명령은 노드에서 실행되는 Python의 버전을 반환합니다.

Linux & macOS
sh_command_id=$(aws ssm send-command \ --instance-ids "instance-ID" \ --document-name "AWS-RunShellScript" \ --comment "Demo run shell script on Linux Instances" \ --parameters commands='python -V' \ --output text --query "Command.CommandId") \ sh -c 'aws ssm list-command-invocations \ --command-id "$sh_command_id" \ --details \ --query "CommandInvocations[].CommandPlugins[].{Status:Status,Output:Output}"'

4단계: Run Command을 사용하여 간단한 Python 스크립트 실행

다음 명령은 Run Command을 사용하여 간단한 Python “Hello World” 스크립트를 실행합니다.

Linux & macOS
sh_command_id=$(aws ssm send-command \ --instance-ids "instance-ID" \ --document-name "AWS-RunShellScript" \ --comment "Demo run shell script on Linux Instances" \ --parameters '{"commands":["#!/usr/bin/python","print \"Hello World from python\""]}' \ --output text \ --query "Command.CommandId") \ sh -c 'aws ssm list-command-invocations \ --command-id "$sh_command_id" \ --details \ --query "CommandInvocations[].CommandPlugins[].{Status:Status,Output:Output}"'

5단계: Run Command를 사용하여 Bash 스크립트 실행

이 섹션의 예에서는 Run Command를 사용하여 다음 bash 스크립트를 실행하는 방법을 보여줍니다.

Run Command를 사용하여 원격 위치에 저장된 스크립트를 실행하는 예는 Amazon S3에서 스크립트 실행GitHub에서 스크립트 실행를 참조하세요.

#!/bin/bash yum -y update yum install -y ruby cd /home/ec2-user curl -O https://aws-codedeploy-us-east-2.s3.amazonaws.com/latest/install chmod +x ./install ./install auto

이 스크립트는 AWS CodeDeploy User GuideCreate an Amazon EC2 instance for CodeDeploy에 설명된 대로 Amazon Linux 및 Red Hat Enterprise Linux(RHEL) 인스턴스에 AWS CodeDeploy 에이전트를 설치합니다.

이 스크립트는 미국 동부(오하이오) 리전(us-east-2)인 aws-codedeploy-us-east-2의 AWS 관리형 S3 버킷에서 CodeDeploy 에이전트를 설치합니다.

AWS CLI 명령에서 bash 스크립트 실행

다음 샘플은 --parameters 옵션을 사용하여 CLI 명령에 bash 스크립트를 포함하는 방법을 보여줍니다.

Linux & macOS
aws ssm send-command \ --document-name "AWS-RunShellScript" \ --targets '[{"Key":"InstanceIds","Values":["instance-id"]}]' \ --parameters '{"commands":["#!/bin/bash","yum -y update","yum install -y ruby","cd /home/ec2-user","curl -O https://aws-codedeploy-us-east-2.s3.amazonaws.com/latest/install","chmod +x ./install","./install auto"]}'

JSON 파일에서 bash 스크립트 실행

다음 예에서는 bash 스크립트의 내용을 JSON 파일로 저장하고, --cli-input-json 옵션을 사용하여 해당 파일을 명령에 포함합니다.

Linux & macOS
aws ssm send-command \ --document-name "AWS-RunShellScript" \ --targets "Key=InstanceIds,Values=instance-id" \ --cli-input-json file://installCodeDeployAgent.json
Windows
aws ssm send-command ^ --document-name "AWS-RunShellScript" ^ --targets "Key=InstanceIds,Values=instance-id" ^ --cli-input-json file://installCodeDeployAgent.json

참조되는 installCodeDeployAgent.json 파일의 내용이 다음 예제에 나와 있습니다.

{ "Parameters": { "commands": [ "#!/bin/bash", "yum -y update", "yum install -y ruby", "cd /home/ec2-user", "curl -O https://aws-codedeploy-us-east-2.s3.amazonaws.com/latest/install", "chmod +x ./install", "./install auto" ] } }