source/remediation_runbooks/scripts/CreateLogMetricFilterAndAlarm_createtopic.py [22:68]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
boto_config = Config(
    retries ={
        'mode': 'standard'
    }
)

def connect_to_sns():
    return boto3.client('sns', config=boto_config)

def connect_to_ssm():
    return boto3.client('ssm', config=boto_config)

def create_encrypted_topic(event, context):

    kms_key_arn = event['kms_key_arn']
    new_topic = False
    topic_arn = ''
    topic_name = event['topic_name']

    try:
        sns = connect_to_sns()
        topic_arn = sns.create_topic(
            Name=topic_name,
            Attributes={
                'KmsMasterKeyId': kms_key_arn.split('key/')[1]
            }
        )['TopicArn']
        new_topic = True

    except ClientError as client_exception:
        exception_type = client_exception.response['Error']['Code']
        if exception_type == 'InvalidParameter':
            print(f'Topic {topic_name} already exists. This remediation may have been run before.')
            print('Ignoring exception - remediation continues.')
            topic_arn = sns.create_topic(
                Name=topic_name
            )['TopicArn']
        else:
            exit(f'ERROR: Unhandled client exception: {client_exception}')
      
    except Exception as e:
        exit(f'ERROR: could not create SNS Topic {topic_name}: {str(e)}')

    if new_topic:
        try:
            ssm = connect_to_ssm()
            ssm.put_parameter(
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



source/remediation_runbooks/scripts/EnableAWSConfig_createtopic.py [22:68]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
boto_config = Config(
    retries ={
        'mode': 'standard'
    }
)

def connect_to_sns():
    return boto3.client('sns', config=boto_config)

def connect_to_ssm():
    return boto3.client('ssm', config=boto_config)

def create_encrypted_topic(event, context):

    kms_key_arn = event['kms_key_arn']
    new_topic = False
    topic_arn = ''
    topic_name = event['topic_name']

    try:
        sns = connect_to_sns()
        topic_arn = sns.create_topic(
            Name=topic_name,
            Attributes={
                'KmsMasterKeyId': kms_key_arn.split('key/')[1]
            }
        )['TopicArn']
        new_topic = True

    except ClientError as client_exception:
        exception_type = client_exception.response['Error']['Code']
        if exception_type == 'InvalidParameter':
            print(f'Topic {topic_name} already exists. This remediation may have been run before.')
            print('Ignoring exception - remediation continues.')
            topic_arn = sns.create_topic(
                Name=topic_name
            )['TopicArn']
        else:
            exit(f'ERROR: Unhandled client exception: {client_exception}')
      
    except Exception as e:
        exit(f'ERROR: could not create SNS Topic {topic_name}: {str(e)}')

    if new_topic:
        try:
            ssm = connect_to_ssm()
            ssm.put_parameter(
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



