Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. # Run this Lambda function inside a VPC with a MongoDB instance inside it.
  2. # Set up security groups to allow the function to communicate with the MongoDB endpoint
  3. # NAT service or NAT instance is required because the function need to access AWS KMS, which an external endpoint
  4. # Set four environment variables for the function containing the details required for the MongoDB endpoint uri
  5. # I used the Bitnami MongoDB install from the AWS Marketplace
  6.  
  7. import boto3
  8. import os
  9.  
  10. from base64 import b64decode
  11. from pymongo import MongoClient
  12.  
  13. print "decrypting enviroment variables"
  14. ENCRYPTEDusername = os.environ['MongoDBusername']
  15. MongoDBusername = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTEDusername))['Plaintext']
  16. ENCRYPTEDpassword = os.environ['MongoDBpassword']
  17. MongoDBpassword = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTEDpassword))['Plaintext']
  18. ENCRYPTEDaddress = os.environ['MongoDBaddress']
  19. MongoDBaddress = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTEDaddress))['Plaintext']
  20. ENCRYPTEDname = os.environ['MongoDBname']
  21. MongoDBname = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTEDname))['Plaintext']
  22.  
  23.  
  24. def lambda_handler(event, context):
  25. print "starting function"
  26. print "constructing uri for MongoDB connection"
  27. uri = "mongodb://" + MongoDBusername + ":" + MongoDBpassword + "@" + MongoDBaddress + "/" + MongoDBname
  28. client = MongoClient(uri)
  29. print client
  30. db = client['lambda_demo']
  31. print db
  32. result = db.lambda_demo.insert_one(event)
  33. print result
  34. print "function end"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement