문제 상황 − Python에서 boto3 라이브러리를 사용하여 AWS Glue에 등록된 모든 보안 구성(Security Configuration)의 세부 정보를 조회해야 합니다.
예시 − AWS Glue에 존재하는 모든 보안 구성의 상세 정보를 가져와 보겠습니다.
문제 해결 접근 방식
1단계 − boto3와 botocore 예외 처리 모듈을 임포트하여 예외 상황을 처리합니다.
2단계 − 이 작업에는 별도의 파라미터가 필요하지 않습니다. 사용자의 AWS Glue 계정에 존재하는 모든 보안 구성을 자동으로 가져옵니다.
3단계 − boto3 라이브러리를 사용해 AWS 세션(Session)을 생성합니다. 기본 프로필에 region_name이 설정되어 있지 않다면, 세션 생성 시 region_name을 명시적으로 전달해야 합니다.
4단계 − glue 서비스를 위한 AWS 클라이언트를 생성합니다.
5단계 − get_security_configurations 함수를 호출합니다.
6단계 − 해당 함수는 모든 보안 구성 정보를 딕셔너리 형태로 반환합니다.
7단계 − API 호출 중 오류가 발생할 경우를 대비해 일반 예외(generic exception)도 함께 처리합니다.
예제 코드
아래 코드를 사용하면 모든 보안 구성 정보를 가져올 수 있습니다 −
import boto3
from botocore.exceptions import ClientError
def get_all_security_configuration():
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_security_configurations()
return response
except ClientError as e:
raise Exception("boto3 client error in get_all_security_configuration: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_all_security_configuration: " + e.__str__())
print(get_all_security_configuration())실행 결과
{'SecurityConfiguration': {'Name': 'job-security-settings',
'CreatedTimeStamp': datetime.datetime(2020, 9, 24, 1, 53, 21, 265000,
tzinfo=tzlocal()), 'EncryptionConfiguration': {'S3Encryption':
[{'S3EncryptionMode': 'SSE-KMS', 'KmsKeyArn': 'arn:aws:kms:us-east1:**************:key/************-bd27-f3ec3b590d0f'}]}},
'ResponseMetadata': {'RequestId': 'b1***************-afd048ed7d07',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 01 Mar 2021
05:48:47 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '417', 'connection': 'keep-alive', 'x-amzn-requestid':
'b1*******************-afd048ed7d07'}, 'RetryAttempts': 0}}위 출력 결과를 보면, SecurityConfiguration 키 아래에 구성 이름(Name), 생성 시각(CreatedTimeStamp), 그리고 암호화 설정(EncryptionConfiguration)이 포함되어 있는 것을 확인할 수 있습니다. 특히 S3 암호화 모드가 SSE-KMS로 설정되어 있으며, 해당 KMS 키의 ARN 값도 함께 반환됩니다. 또한 ResponseMetadata에는 요청 ID, HTTP 상태 코드(200), 재시도 횟수 등 API 응답에 대한 메타데이터가 담겨 있어 정상적으로 요청이 처리되었음을 알 수 있습니다.