Last active
February 25, 2019 23:30
-
-
Save alexcasalboni/30cf93285fc93aeb5078e41fc9a0d4e4 to your computer and use it in GitHub Desktop.
AWS Config Custom Rule - AWS Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const aws = require('aws-sdk'); | |
const utility = require('./utility'); | |
const config = new aws.ConfigService(); | |
/** | |
* In this example, the resource is compliant if it is an instance and its type matches the type specified as the desired type. | |
* If the resource is not an instance, then this resource is not applicable. | |
*/ | |
function evaluateChangeNotificationCompliance(configurationItem, ruleParameters) { | |
if (configurationItem.resourceType !== 'AWS::EC2::Instance') { | |
return 'NOT_APPLICABLE'; // skip | |
} else if (ruleParameters.desiredInstanceType === configurationItem.configuration.instanceType) { | |
return 'COMPLIANT'; // OK | |
} | |
return 'NON_COMPLIANT'; // KO | |
} | |
/** | |
* Receives the event and context from AWS Lambda. | |
*/ | |
exports.handler = async (event, context, callback) => { | |
const invokingEvent = JSON.parse(event.invokingEvent); | |
const ruleParameters = JSON.parse(event.ruleParameters); | |
const configurationItem = await utility.getConfigurationItem(invokingEvent); | |
let compliance = 'NOT_APPLICABLE'; | |
if (utility.isApplicable(configurationItem, event)) { | |
// Invoke the compliance checking function. | |
compliance = evaluateChangeNotificationCompliance(configurationItem, ruleParameters); | |
} | |
// Initializes the request that contains the evaluation results. | |
const putEvaluationsRequest = { | |
ResultToken: event.resultToken, | |
Evaluations: [ | |
{ | |
ComplianceResourceType: configurationItem.resourceType, | |
ComplianceResourceId: configurationItem.resourceId, | |
ComplianceType: compliance, | |
OrderingTimestamp: configurationItem.configurationItemCaptureTime, | |
}, | |
] | |
}; | |
// Sends the evaluation results to AWS Config. | |
return await config.putEvaluations(putEvaluationsRequest).promise(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment