devops_97

AWS_Secret_Manager

Aug 11th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. # Use this code snippet in your app.
  2. # If you need more information about configurations or implementing the sample code, visit the AWS docs:  
  3. # https://aws.amazon.com/developers/getting-started/python/
  4.  
  5. import boto3
  6. import base64
  7. from botocore.exceptions import ClientError
  8.  
  9.  
  10. def get_secret():
  11.  
  12.     secret_name = "ro_landingi_production"
  13.     region_name = "eu-west-1"
  14.  
  15.     # Create a Secrets Manager client
  16.     session = boto3.session.Session()
  17.     client = session.client(
  18.         service_name='secretsmanager',
  19.         region_name=region_name
  20.     )
  21.  
  22.     # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
  23.     # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
  24.     # We rethrow the exception by default.
  25.  
  26.     try:
  27.         get_secret_value_response = client.get_secret_value(
  28.             SecretId=secret_name
  29.         )
  30.     except ClientError as e:
  31.         if e.response['Error']['Code'] == 'DecryptionFailureException':
  32.             # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
  33.             # Deal with the exception here, and/or rethrow at your discretion.
  34.             raise e
  35.         elif e.response['Error']['Code'] == 'InternalServiceErrorException':
  36.             # An error occurred on the server side.
  37.             # Deal with the exception here, and/or rethrow at your discretion.
  38.             raise e
  39.         elif e.response['Error']['Code'] == 'InvalidParameterException':
  40.             # You provided an invalid value for a parameter.
  41.             # Deal with the exception here, and/or rethrow at your discretion.
  42.             raise e
  43.         elif e.response['Error']['Code'] == 'InvalidRequestException':
  44.             # You provided a parameter value that is not valid for the current state of the resource.
  45.             # Deal with the exception here, and/or rethrow at your discretion.
  46.             raise e
  47.         elif e.response['Error']['Code'] == 'ResourceNotFoundException':
  48.             # We can't find the resource that you asked for.
  49.             # Deal with the exception here, and/or rethrow at your discretion.
  50.             raise e
  51.     else:
  52.         # Decrypts secret using the associated KMS CMK.
  53.         # Depending on whether the secret is a string or binary, one of these fields will be populated.
  54.         if 'SecretString' in get_secret_value_response:
  55.             secret = get_secret_value_response['SecretString']
  56.         else:
  57.             decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
  58.            
  59.     # Your code goes here.
  60.  
Add Comment
Please, Sign In to add comment