lambda_function.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import sys
  2. import pymysql
  3. import json
  4. import decimal
  5. import boto3
  6. import uuid
  7. import logging
  8. import os
  9. import time
  10. from datetime import datetime, timedelta
  11. from botocore.exceptions import ClientError
  12. class DecimalnDateTimeEncoder(json.JSONEncoder):
  13. def default(self, obj):
  14. if isinstance(obj, decimal.Decimal) or isinstance(obj, datetime):
  15. return str(obj)
  16. return json.JSONEncoder.default(self, obj)
  17. def get_secret():
  18. secret_name = "ambt-preden-gurigalmae-valleydb"
  19. region_name = "ap-northeast-2"
  20. # Create a Secrets Manager client
  21. session = boto3.session.Session()
  22. client = session.client(
  23. service_name='secretsmanager',
  24. region_name=region_name
  25. )
  26. # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
  27. # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
  28. # We rethrow the exception by default.
  29. try:
  30. get_secret_value_response = client.get_secret_value(
  31. SecretId=secret_name
  32. )
  33. except ClientError as e:
  34. if e.response['Error']['Code'] == 'DecryptionFailureException':
  35. # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
  36. # Deal with the exception here, and/or rethrow at your discretion.
  37. raise e
  38. elif e.response['Error']['Code'] == 'InternalServiceErrorException':
  39. # An error occurred on the server side.
  40. # Deal with the exception here, and/or rethrow at your discretion.
  41. raise e
  42. elif e.response['Error']['Code'] == 'InvalidParameterException':
  43. # You provided an invalid value for a parameter.
  44. # Deal with the exception here, and/or rethrow at your discretion.
  45. raise e
  46. elif e.response['Error']['Code'] == 'InvalidRequestException':
  47. # You provided a parameter value that is not valid for the current state of the resource.
  48. # Deal with the exception here, and/or rethrow at your discretion.
  49. raise e
  50. elif e.response['Error']['Code'] == 'ResourceNotFoundException':
  51. # We can't find the resource that you asked for.
  52. # Deal with the exception here, and/or rethrow at your discretion.
  53. raise e
  54. else:
  55. # Decrypts secret using the associated KMS CMK.
  56. # Depending on whether the secret is a string or binary, one of these fields will be populated.
  57. if 'SecretString' in get_secret_value_response:
  58. secret = get_secret_value_response['SecretString']
  59. return json.loads(secret)
  60. def lambda_handler(event, context):
  61. os.environ['TZ'] = 'Asia/Seoul'
  62. time.tzset()
  63. logger = logging.getLogger()
  64. logger.setLevel(logging.INFO)
  65. secret = get_secret()
  66. try:
  67. conn = pymysql.connect(host=secret['host'], port=int(secret['port']), user=secret['username'], passwd=secret['password'], db=secret['dbname'], connect_timeout=5)
  68. except:
  69. logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
  70. sys.exit()
  71. logger.info("SUCCESS: Connection to RDS mysql instance succeeded")
  72. today = datetime.now()
  73. yesterday = today - timedelta(days=1)
  74. sql = "SELECT * FROM MONTHENERGY WHERE ENERGY_YEAR={} AND ENERGY_MONTH={}".format(yesterday.year, yesterday.month)
  75. with conn.cursor(pymysql.cursors.DictCursor) as cur:
  76. cur.execute(sql)
  77. logger.info(sql)
  78. rows = cur.fetchall()
  79. data = ""
  80. for row in rows:
  81. data += json.dumps(row, cls=DecimalnDateTimeEncoder) + '\n'
  82. logger.info(row)
  83. s3=boto3.resource('s3')
  84. object = s3.Object('hdci-ambt-homenetserver-raw','dev/site_name=gurigalmae/table_name=monthenergy/year={}/ambt-preden-lambda-migration-dev-{}-{}-{}'.format(yesterday.year, yesterday.year, yesterday.month, str(uuid.uuid4())))
  85. return object.put(Body=data)