Securing the secrets on Serverless Framework by AWS KMS encryption.
- Serverless Framework 1.0 or higher
npm install serverless-crypt --save
provider:
name: aws
runtime: nodejs4.3
# runtime: python2.7
plugins:
- serverless-crypt
custom:
cryptKeyId: ${env:AWS_KMS_KEYID}
- python2.7
- nodejs4.3
serverless encrypt -n $SECRET_NAME -t $PLAINTEXT --save
serverless decrypt -n $SECRET_NAME
See: https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html
Policy example:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Decrypt"
],
"Resource": [
"arn:aws:kms:us-east-1:<your-account-number>:key/<your-key-id>"
]
}
]
}
Configuration example:
- serverless.yml
provider:
name: aws
runtime: nodejs4.3
# runtime: python2.7
functions:
hello:
handler: handler.hello
plugins:
- serverless-crypt
custom:
cryptKeyId: ${env:AWS_KMS_KEYID}
Command example:
serverless encrypt -n secret_name -t "This is a secret" --save
** slscrypt
module is automatically injected into your deployment package. **
Code example:
- Node.js
'use strict';
const slscrypt = require('slscrypt');
module.exports.hello = (event, context, callback) => {
slscrypt.get('secret_name').then((txt) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: txt,
input: event,
}),
};
callback(null, response);
});
};
- Python
import json
import slscrypt
def hello(event, context):
body = {
"message": slscrypt.get('secret_name'),
"input": event
}
response = {
"statusCode": 200,
"body": json.dumps(body)
};
return response
Command example:
serverless deploy
or
serverless deploy function -f $FUNCTION_NAME
Command example:
serverless invoke -f $FUNCTION_NAME
Result example:
{
"body": "{\"input\": {}, \"message\": \"This is a secret\"}",
"statusCode": 200
}
- Source hosted at GitHub
- Report issues/questions/feature requests on GitHub Issues
Pull requests are very welcome! Make sure your patches are well tested. Ideally create a topic branch for every separate change you make. For example:
- Fork the repo
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Created and maintained by Masashi Terui (marcy9114@gmail.com)
MIT License (see LICENSE)