Created
February 25, 2019 23:30
-
-
Save alexcasalboni/60a3b45017ad3d44f052c2dd3c1661e4 to your computer and use it in GitHub Desktop.
AWS Config Custom Rule - AWS Lambda (utiity)
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 config = new aws.ConfigService(); | |
/** | |
* Get the configurationItem for the resource using the getResourceConfigHistory API. | |
*/ | |
async function getConfigurationFromHistory(configurationHistory, callback) { | |
const params = { | |
resourceType: configurationHistory.resourceType, | |
resourceId: configurationHistory.resourceId, | |
laterTime: new Date(configurationHistory.configurationItemCaptureTime), | |
limit: 1, | |
}; | |
const data = await config.getResourceConfigHistory(params).promise(); | |
return data.configurationItems[0]; | |
} | |
/** | |
* Convert the oversized configuration item from the API model to the original invocation model. | |
*/ | |
function convertApiConfiguration(apiConfiguration) { | |
apiConfiguration.awsAccountId = apiConfiguration.accountId; | |
apiConfiguration.ARN = apiConfiguration.arn; | |
apiConfiguration.configurationStateMd5Hash = apiConfiguration.configurationItemMD5Hash; | |
apiConfiguration.configurationItemVersion = apiConfiguration.version; | |
apiConfiguration.configuration = JSON.parse(apiConfiguration.configuration); | |
if ({}.hasOwnProperty.call(apiConfiguration, 'relationships')) { | |
for (let i = 0; i < apiConfiguration.relationships.length; i++) { | |
apiConfiguration.relationships[i].name = apiConfiguration.relationships[i].relationshipName; | |
} | |
} | |
return apiConfiguration; | |
} | |
/** | |
* Based on the message type, get the configuration item either from the configurationItem object in the invoking event | |
* or with the getResourceConfigHistory API in the getConfiguration function. | |
*/ | |
async function getConfigurationItem(invokingEvent, callback) { | |
if (invokingEvent.messageType === 'OversizedConfigurationItemChangeNotification') { | |
const apiConfigurationItem = await getConfigurationFromHistory(invokingEvent.configurationItemSummary); | |
return convertApiConfiguration(apiConfigurationItem); | |
} else { | |
return invokingEvent.configurationItem; | |
} | |
} | |
/** | |
* Check whether the resource has been deleted. If the resource was deleted, then the evaluation returns not applicable. | |
*/ | |
function isApplicable(configurationItem, event) { | |
const status = configurationItem.configurationItemStatus; | |
const eventLeftScope = event.eventLeftScope; | |
return (status === 'OK' || status === 'ResourceDiscovered') && eventLeftScope === false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment