Skip to content

Instantly share code, notes, and snippets.

@bilousov94
Last active May 15, 2020 22:51
Show Gist options
  • Save bilousov94/3cff4aca6c8726dee4188a89de28e380 to your computer and use it in GitHub Desktop.
Save bilousov94/3cff4aca6c8726dee4188a89de28e380 to your computer and use it in GitHub Desktop.
Source code for lambda function to encrypt data with kms and store it in s3
import json
import boto3
import base64
def lambda_handler(event, context):
# data, which we will encrypt
mydata = {
"secret": "something important",
"password": "very_secret_password",
}
kms = boto3.Session().client('kms')
#encrypt the data
ciphertext = kms.encrypt(
KeyId='alias/eb-secret',
Plaintext=json.dumps(mydata)
)
encoded_ciphertext = base64.b64encode(ciphertext["CiphertextBlob"])
s3 = boto3.Session(region_name='us-east-1').client('s3')
#put encrypted data into s3 bucket
response = s3.put_object(
Body=encoded_ciphertext,
Bucket='eb-secret-bucket',
Key='secret_data.json',
)
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment