Đề bài − Sử dụng thư viện boto3 trong Python để lấy chi tiết một cấu hình bảo mật được chỉ định trong AWS Glue Security.
Ví dụ − Lấy chi tiết cấu hình bảo mật ('job-security-settings') trong AWS Glue Security.
Cách tiếp cận/Thuật toán giải quyết
Bước 1 − Nhập boto3 và botocore exceptions để xử lý ngoại lệ.
Bước 2 − security_name là tham số bắt buộc cần lấy chi tiết cấu hình.
Bước 3 − Tạo phiên AWS bằng boto3. Đảm bảo region_name trong profile mặc định. Nếu không, truyền rõ ràng region_name khi tạo phiên.
Bước 4 − Tạo client AWS cho glue.
Bước 5 − Sử dụng hàm get_security_configuration và truyền security_name làm tham số Name.
Bước 6 − Nó trả về cấu hình của bảo mật đã cho.
Bước 7 − Xử lý ngoại lệ chung nếu có lỗi xảy ra khi kiểm tra job.
Ví dụ
Sử dụng code sau để lấy cấu hình của bảo mật đã cho:
import boto3
from botocore.exceptions import ClientError
def get_detail_security_configuration(security_name):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_security_configuration(Name=security_name)
return response
except ClientError as e:
raise Exception("boto3 client error in get_detail_security_configuration: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_detail_security_configuration: " + e.__str__())
print(get_detail_security_configuration("job-security-settings"))
Kết quả
{'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}}