5단계: (선택 사항) 세션의 명령에 대한 액세스 제한 - AWS Systems Manager

5단계: (선택 사항) 세션의 명령에 대한 액세스 제한

사용자 지정 Session 유형 AWS Systems Manager(SSM) 문서를 사용하여 사용자가 AWS Systems Manager Session Manager세션에서 실행할 수 있는 명령을 제한할 수 있습니다. 문서에서 사용자가 세션을 시작할 때 실행할 수 있는 명령과 사용자가 명령에 제공할 수 있는 파라미터를 정의합니다. Session 문서 schemaVersion은 1.0이어야 하고 문서의 sessionTypeInteractiveCommands여야 합니다. 그러면 직접 정의하는 Session 문서에만 사용자가 액세스할 수 있도록 허용하는 AWS Identity and Access Management(IAM) 정책을 생성할 수 있습니다. IAM 정책을 사용하여 세션의 명령에 대한 액세스를 제한하는 방법에 대한 자세한 내용은 대화형 명령에 대한 IAM 정책 예제 섹션을 참조하세요.

InteractiveCommandssessionType이 포함된 문서는 AWS Command Line Interface(AWS CLI) 에서 시작된 세션의 경우에만 지원됩니다. 사용자는 사용자 지정 문서 이름을 --document-name 파라미터 값으로 제공하고 --parameters 옵션을 사용하여 모든 명령 파라미터 변수 값을 제공합니다. 대화형 명령 실행에 대한 자세한 내용은 세션 시작(대화형 및 비대화형 명령) 섹션을 참조하세요.

다음 절차를 사용하여 사용자가 실행할 수 있는 명령을 정의하는 사용자 지정 Session 유형 SSM 문서를 생성합니다.

세션의 명령에 대한 액세스 제한(콘솔)

사용자가 Session Manager 세션에서 실행할 수 있는 명령을 제한하려면(콘솔)
  1. AWS Systems Manager 콘솔(https://console.aws.amazon.com/systems-manager/)을 엽니다.

  2. 탐색 창에서 Documents를 선택합니다.

  3. 명령 또는 세션 생성을 선택합니다.

  4. [이름(Name)]에 문서에 대한 설명이 포함된 이름을 입력합니다.

  5. 문서 유형에서 세션 문서를 선택합니다.

  6. 다음 예제와 같이 사용자가 JSON 또는 YAML을 사용하여 Session Manager 세션에서 실행할 수 있는 명령을 정의하는 문서 내용을 입력합니다.

    YAML
    --- schemaVersion: '1.0' description: Document to view a log file on a Linux instance sessionType: InteractiveCommands parameters: logpath: type: String description: The log file path to read. default: "/var/log/amazon/ssm/amazon-ssm-agent.log" allowedPattern: "^[a-zA-Z0-9-_/]+(.log)$" properties: linux: commands: "tail -f {{ logpath }}" runAsElevated: true
    JSON
    { "schemaVersion": "1.0", "description": "Document to view a log file on a Linux instance", "sessionType": "InteractiveCommands", "parameters": { "logpath": { "type": "String", "description": "The log file path to read.", "default": "/var/log/amazon/ssm/amazon-ssm-agent.log", "allowedPattern": "^[a-zA-Z0-9-_/]+(.log)$" } }, "properties": { "linux": { "commands": "tail -f {{ logpath }}", "runAsElevated": true } } }
  7. 문서 생성을 선택합니다.

세션의 명령에 대한 액세스 제한(명령줄)

시작하기 전 준비 사항

아직 하지 않은 경우 AWS Command Line Interface(AWS CLI) 또는 AWS Tools for PowerShell을 설치하고 구성합니다. 자세한 내용은 최신 버전의 AWS CLI 설치 또는 업데이트AWS Tools for PowerShell 설치를 참조하세요.

사용자가 Session Manager 세션에서 실행할 수 있는 명령을 제한하려면(명령줄)
  1. 다음 예제와 같이 사용자가 Session Manager 세션에서 실행할 수 있는 명령을 정의하는 문서 내용에 JSON 또는 YAML 파일을 생성합니다.

    YAML
    --- schemaVersion: '1.0' description: Document to view a log file on a Linux instance sessionType: InteractiveCommands parameters: logpath: type: String description: The log file path to read. default: "/var/log/amazon/ssm/amazon-ssm-agent.log" allowedPattern: "^[a-zA-Z0-9-_/]+(.log)$" properties: linux: commands: "tail -f {{ logpath }}" runAsElevated: true
    JSON
    { "schemaVersion": "1.0", "description": "Document to view a log file on a Linux instance", "sessionType": "InteractiveCommands", "parameters": { "logpath": { "type": "String", "description": "The log file path to read.", "default": "/var/log/amazon/ssm/amazon-ssm-agent.log", "allowedPattern": "^[a-zA-Z0-9-_/]+(.log)$" } }, "properties": { "linux": { "commands": "tail -f {{ logpath }}", "runAsElevated": true } } }
  2. 다음 명령을 실행하여 사용자가 Session Manager 세션에서 실행할 수 있는 명령을 정의하는 내용을 사용하여 SSM 문서를 생성합니다.

    Linux & macOS
    aws ssm create-document \ --content file://path/to/file/documentContent.json \ --name "exampleAllowedSessionDocument" \ --document-type "Session"
    Windows
    aws ssm create-document ^ --content file://C:\path\to\file\documentContent.json ^ --name "exampleAllowedSessionDocument" ^ --document-type "Session"
    PowerShell
    $json = Get-Content -Path "C:\path\to\file\documentContent.json" | Out-String New-SSMDocument ` -Content $json ` -Name "exampleAllowedSessionDocument" ` -DocumentType "Session"

대화형 명령 파라미터 및 AWS CLI

AWS CLI를 사용할 때 대화형 명령 파라미터를 제공할 수 있는 다양한 방법이 있습니다. AWS CLI가 있는 관리형 노드에 연결하는 데 사용하는 클라이언트 시스템의 운영 체제(OS)에 따라 특수 문자나 이스케이프 문자가 포함된 명령에 제공하는 구문이 다를 수 있습니다. 다음 예는 AWS CLI 사용 시 명령 파라미터를 제공할 수 있는 몇 가지 다른 방법과 특수 문자 또는 이스케이프 문자를 처리하는 방법을 보여줍니다.

Parameter Store에 저장된 파라미터는 다음 예와 같이 명령 파라미터에 대해 AWS CLI에서 참조할 수 있습니다.

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters '{"command":["{{ssm:mycommand}}"]}'
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters '{"command":["{{ssm:mycommand}}"]}'

다음 예에서는 AWS CLI에 약식 구문을 사용하여 파라미터를 전달하는 방법을 보여줍니다.

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters command="ifconfig"
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters command="ipconfig"

다음 예와 같이 JSON에서 파라미터를 제공할 수도 있습니다.

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters '{"command":["ifconfig"]}'
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters '{"command":["ipconfig"]}'

파라미터를 JSON 파일에 저장하고 다음 예와 같이 AWS CLI에 제공할 수도 있습니다. 파일에서 AWS CLI 파라미터 사용에 대한 자세한 내용은 AWS Command Line Interface 사용 설명서파일에서 AWS CLI 파라미터 로드를 참조하세요.

{ "command": [ "my command" ] }
Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters file://complete/path/to/file/parameters.json
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters file://complete/path/to/file/parameters.json

다음 예와 같이 JSON 입력 파일에서 AWS CLI 스켈레톤을 생성할 수도 있습니다. JSON 입력 파일에서 AWS CLI 스켈레톤 생성에 대한 자세한 내용은 AWS Command Line Interface 사용 설명서JSON 또는 YAML 입력 파일에서 AWS CLI 스켈레톤 및 입력 파라미터 생성을 참조하세요.

{ "Target": "instance-id", "DocumentName": "MyInteractiveCommandDocument", "Parameters": { "command": [ "my command" ] } }
Linux & macOS
aws ssm start-session \ --cli-input-json file://complete/path/to/file/parameters.json
Windows
aws ssm start-session ^ --cli-input-json file://complete/path/to/file/parameters.json

따옴표 안의 문자를 이스케이프 처리하려면 다음 예와 같이 이스케이프 문자에 백슬래시를 추가해야 합니다.

Linux & macOS
aws ssm start-session \ --target instance-id \ --document-name MyInteractiveCommandDocument \ --parameters '{"command":["printf \"abc\\\\tdef\""]}'
Windows
aws ssm start-session ^ --target instance-id ^ --document-name MyInteractiveCommandDocument ^ --parameters '{"command":["printf \"abc\\\\tdef\""]}'

AWS CLI에서 명령 파라미터에 따옴표를 사용에 대한 자세한 내용은 AWS Command Line Interface 사용 설명서AWS CLI에서 문자열에 따옴표 사용을 참조하세요.

대화형 명령에 대한 IAM 정책 예제

직접 정의한 Session 문서에만 사용자가 액세스할 수 있도록 허용하는 IAM정책을 생성할 수 있습니다. 이렇게 하면 사용자가 Session Manager 세션에서 실행할 수 있는 명령이 사용자 정의 Session 유형 SSM 문서에 정의된 명령으로만 제한됩니다.

사용자가 단일 관리형 노드에서 대화형 명령을 실행하도록 허용
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:region:987654321098:instance/i-02573cafcfEXAMPLE", "arn:aws:ssm:region:987654321098:document/exampleAllowedSessionDocument" ], "Condition":{ "BoolIfExists":{ "ssm:SessionDocumentAccessCheck":"true" } } } ] }
사용자가 모든 관리형 노드에서 대화형 명령을 실행하도록 허용
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:us-west-2:987654321098:instance/*", "arn:aws:ssm:us-west-2:987654321098:document/exampleAllowedSessionDocument" ], "Condition":{ "BoolIfExists":{ "ssm:SessionDocumentAccessCheck":"true" } } } ] }
사용자가 모든 관리형 노드에서 여러 대화형 명령을 실행하도록 허용
{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":"ssm:StartSession", "Resource":[ "arn:aws:ec2:us-west-2:987654321098:instance/*", "arn:aws:ssm:us-west-2:987654321098:document/exampleAllowedSessionDocument", "arn:aws:ssm:us-west-2:987654321098:document/exampleAllowedSessionDocument2" ], "Condition":{ "BoolIfExists":{ "ssm:SessionDocumentAccessCheck":"true" } } } ] }