Data Lifecycle Manager 사전 및 사후 스크립트의 기타 사용 사례
사전 및 사후 스크립트를 사용하여 애플리케이션에 일관되게 적용되는 스냅샷을 자동화하는 것 외에도 사전 및 사후 스크립트를 함께 사용하거나 개별적으로 사용하여 스냅샷 생성 전후에 다른 관리 작업을 자동화할 수 있습니다. 예:
-
스냅샷을 생성하기 전에 사전 스크립트를 사용하여 패치를 적용합니다. 이렇게 하면 정기 주간 또는 월간 소프트웨어 업데이트를 적용한 후 스냅샷을 생성할 수 있습니다.
참고
사전 스크립트만 실행하도록 선택하면 중단 일관성 스냅샷으로 기본 설정이 활성화됩니다.
-
스냅샷을 생성한 후 사후 스크립트를 사용하여 패치를 적용합니다. 이렇게 하면 정기 주간 또는 월간 소프트웨어 업데이트를 적용하기 전 스냅샷을 생성할 수 있습니다.
다른 사용 사례를 위한 시작하기
이 섹션에서는 애플리케이션에 일관되게 적용되는 스냅샷 이외의 사용 사례에 사전 및/또는 사후 스크립트를 사용할 때 수행해야 하는 단계를 설명합니다.
사전 및/또는 사후 스크립트를 위한 대상 인스턴스 준비
-
SSM Agent가 아직 설치되지 않은 경우 대상 인스턴스에 SSM Agent를 설치합니다. SSM Agent가 대상 인스턴스에 이미 설치되어 있는 경우 이 단계를 건너뜁니다.
-
(Linux 인스턴스) Linux용 EC2 인스턴스에 수동으로 SSM Agent 설치
-
(Windows 인스턴스) Windows Server용 EC2 인스턴스에서 SSM 에이전트 작업
-
-
SSM Agent가 실행 중인지 확인합니다. 자세한 내용은 SSM Agent 상태 확인 및 에이전트 시작을 참조하세요.
-
Amazon EC2 인스턴스용 Systems Manager를 설정합니다. 자세한 내용은 AWS Systems Manager 사용 설명서의 Amazon EC2 인스턴스용 Systems Manager 설정을 참조하세요.
실행하려는 명령과 함께 사전 및/또는 사후 스크립트를 포함하는 SSM 명령 문서를 생성해야 합니다.
아래의 빈 SSM 문서 템플릿을 사용하여 SSM 문서를 생성하고 해당 문서 섹션에 사전 및 사후 스크립트 명령을 추가할 수 있습니다.
다음 사항에 유의하세요.
-
SSM 문서가 워크로드에 필요한 올바른 작업을 수행하는지 확인하는 것은 사용자의 책임입니다.
-
SSM 문서에는
pre-script,post-script및dry-run을 포함하여allowedValues에 대한 필수 필드가 포함되어야 합니다. Amazon Data Lifecycle Manager는 이러한 섹션의 내용을 기반으로 인스턴스에서 명령을 실행합니다. SSM 문서에 이러한 섹션이 없는 경우 Amazon Data Lifecycle Manager는 해당 문서를 실패한 실행으로 간주합니다.
###===============================================================================### # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ###===============================================================================### schemaVersion: '2.2' description: SSM Document Template for Amazon Data Lifecycle Manager Pre/Post script feature parameters: executionId: type: String default: None description: (Required) Specifies the unique identifier associated with a pre and/or post execution allowedPattern: ^(None|[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})$ command: # Data Lifecycle Manager will trigger the pre-script and post-script actions during policy execution. # 'dry-run' option is intended for validating the document execution without triggering any commands # on the instance. The following allowedValues will allow Data Lifecycle Manager to successfully # trigger pre and post script actions. type: String default: 'dry-run' description: (Required) Specifies whether pre-script and/or post-script should be executed. allowedValues: - pre-script - post-script - dry-run mainSteps: - action: aws:runShellScript description: Run Database freeze/thaw commands name: run_pre_post_scripts precondition: StringEquals: - platformType - Linux inputs: runCommand: - | #!/bin/bash ###===============================================================================### ### Error Codes ###===============================================================================### # The following Error codes will inform Data Lifecycle Manager of the type of error # and help guide handling of the error. # The Error code will also be emitted via AWS Eventbridge events in the 'cause' field. # 1 Pre-script failed during execution - 201 # 2 Post-script failed during execution - 202 # 3 Auto thaw occurred before post-script was initiated - 203 # 4 Pre-script initiated while post-script was expected - 204 # 5 Post-script initiated while pre-script was expected - 205 # 6 Application not ready for pre or post-script initiation - 206 ###===============================================================================### ### Global variables ###===============================================================================### START=$(date +%s) # For testing this script locally, replace the below with OPERATION=$1. OPERATION={{ command }} # Add all pre-script actions to be performed within the function below execute_pre_script() { echo "INFO: Start execution of pre-script" } # Add all post-script actions to be performed within the function below execute_post_script() { echo "INFO: Start execution of post-script" } # Debug logging for parameters passed to the SSM document echo "INFO: ${OPERATION} starting at $(date) with executionId: ${EXECUTION_ID}" # Based on the command parameter value execute the function that supports # pre-script/post-script operation case ${OPERATION} in pre-script) execute_pre_script ;; post-script) execute_post_script ;; dry-run) echo "INFO: dry-run option invoked - taking no action" ;; *) echo "ERROR: Invalid command parameter passed. Please use either pre-script, post-script, dry-run." exit 1 # return failure ;; esac END=$(date +%s) # Debug Log for profiling the script time echo "INFO: ${OPERATION} completed at $(date). Total runtime: $((${END} - ${START})) seconds."
참고
이 단계는 다음과 같은 경우 필요합니다.
-
사용자 지정 IAM 역할을 사용하는 사전/사후 스크립트 지원 스냅샷 정책을 생성하거나 업데이트합니다.
-
명령줄을 사용하여 기본값을 사용하는 사전/사후 스크립트 지원 스냅샷 정책을 생성하거나 업데이트합니다.
콘솔을 사용하여 스냅샷 관리를 위한 기본 역할(AWSDataLifecycleManagerDefaultRole)을 사용하는 사전/사후 스크립트 지원 스냅샷 정책을 생성하거나 업데이트하려면 이 단계를 건너뜁니다. 이 경우 AWSDataLifecycleManagerSSMFullAccess 정책을 해당 역할에 자동으로 연결합니다.
정책에 사용하는 IAM 역할이 Amazon Data Lifecycle Manager에 정책 대상 인스턴스에서 사전 및 사후 스크립트를 실행하는 데 필요한 SSM 작업을 수행할 수 있는 권한을 부여하는지 확인해야 합니다.
Amazon Data Lifecycle Manager는 필요한 권한이 포함된 관리형 정책(AWSDataLifecycleManagerSSMFullAccess)을 제공합니다. 이 정책을 스냅샷 관리를 위한 IAM 역할에 연결하여 권한이 포함되도록 할 수 있습니다.
중요
AWSDataLifecycleManagerSSMFullAccess 관리형 정책은 사전 및 사후 스크립트를 사용할 때 aws:ResourceTag 조건 키를 사용하여 특정 SSM 문서에 대한 액세스를 제한합니다. Amazon Data Lifecycle Manager가 SSM 문서에 액세스할 수 있도록 하려면 SSM 문서에 DLMScriptsAccess:true 태그가 지정되어 있는지 확인해야 합니다.
또는 사용자 지정 정책을 수동으로 생성하거나 사용하는 IAM 역할에 필요한 권한을 직접 할당할 수 있습니다. AWSDataLifecycleManagerSSMFullAccess 관리형 정책에 정의된 동일한 권한을 사용할 수 있지만 aws:ResourceTag 조건 키는 선택 사항입니다. 해당 조건 키를 사용하지 않기로 결정하면 SSM 문서에 DLMScriptsAccess:true로 태그를 지정할 필요가 없습니다.
다음 방법 중 하나를 사용하여 IAM 역할에 AWSDataLifecycleManagerSSMFullAccess 정책을 추가합니다.