Skip to content

Instantly share code, notes, and snippets.

@akshaymohite
Created May 28, 2021 05:17
Show Gist options
  • Save akshaymohite/42cd03d12e432c4e49bb2bcca39a0926 to your computer and use it in GitHub Desktop.
Save akshaymohite/42cd03d12e432c4e49bb2bcca39a0926 to your computer and use it in GitHub Desktop.
lambda-test-internet.py
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
"""Secrets Manager RDS PostgreSQL Handler
This handler uses the master-user rotation scheme to rotate an RDS PostgreSQL user credential. During the first rotation, this
scheme logs into the database as the master user, creates a new user (appending _clone to the username), and grants the
new user all of the permissions from the user being rotated. Once the secret is in this state, every subsequent rotation
simply creates a new secret with the AWSPREVIOUS user credentials, adds any missing permissions that are in the current
secret, changes that user's password, and then marks the latest secret as AWSCURRENT.
The Secret SecretString is expected to be a JSON string with the following format:
{
'engine': <required: must be set to 'postgres'>,
'host': <required: instance host name>,
'username': <required: username>,
'password': <required: password>,
'dbname': <optional: database name, default to 'postgres'>,
'port': <optional: if not specified, default port 5432 will be used>,
'masterarn': <required: the arn of the master secret which will be used to create users/change passwords>
}
Args:
event (dict): Lambda dictionary of event parameters. These keys must include the following:
- SecretId: The secret ARN or identifier
- ClientRequestToken: The ClientRequestToken of the secret version
- Step: The rotation step (one of createSecret, setSecret, testSecret, or finishSecret)
context (LambdaContext): The Lambda runtime information
Raises:
ResourceNotFoundException: If the secret with the specified arn and stage does not exist
ValueError: If the secret is not properly configured for rotation
KeyError: If the secret json does not contain the expected keys
"""
arn = event['SecretId']
token = event['ClientRequestToken']
step = event['Step']
logger.info("Nothing in here, just plain logs.")
if connect():
logger.info('connected')
else:
logger.info('No connected')
import urllib.request
def connect():
try:
urllib.request.urlopen('http://google.com') #Python 3.x
return True
except:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment