I want to enable the AWS IoT security audit through a CDK stack but it is not working.
First I followed this documentation for the interfaceAuditCheckConfigurationProperty
and the following was the CDK code to enable the IoT security audit:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import { aws_iot as iot } from 'aws-cdk-lib';
export class CdkIotDeviceDefenderStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const auditCheckConfigurationProperty: iot.CfnAccountAuditConfiguration.AuditCheckConfigurationProperty = {
enabled: true,
};
//https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iot.CfnAccountAuditConfiguration.AuditCheckConfigurationsProperty.html
const auditCheckConfigurationsProperty: iot.CfnAccountAuditConfiguration.AuditCheckConfigurationsProperty = {
deviceCertificateExpiringCheck: {
enabled: true,
}
};
}
}
But the previous approach did not work. Then I followed another approach which was to use the AWS SDK inside the CDK to generate the resources in cloudformation. This is the full code:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { IoTClient, UpdateAccountAuditConfigurationCommand } from "@aws-sdk/client-iot";
import * as sns from 'aws-cdk-lib/aws-sns';
import * as subscriptions from 'aws-cdk-lib/aws-sns-subscriptions';
export class CdkIotDeviceDefenderStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// SNS to receive the device defender results
const expiredDeviceCertificateSNTopic = new sns.Topic(this, "SNSDeviceDefender", {
displayName: 'Device Defender Expired Certificate SNS',
fifo: false
});
const clientIoT = new IoTClient({ region: "us-west-2" });
const auditCheckConfigParams: any = {
roleArn: "arn:aws:iam::996242555412:role/Role_AWSIoTDeviceDefenderAudit", //Role manually created - AWSIoTDeviceDefenderAudit policy
auditNotificationTargetConfigurations: {
"SNS": {
"targetArn": expiredDeviceCertificateSNTopic.topicArn,
"roleArn": "arn:aws:iam::996242555412:role/Role_AWSIoTDeviceDefenderAudit",
"enabled": true
}
},
auditCheckConfigurations: {
"DEVICE_CERTIFICATE_EXPIRING_CHECK": {
enabled: true,
}
}
};
(async () => {
try {
const iotUpdateCmd = new UpdateAccountAuditConfigurationCommand(auditCheckConfigParams);
const iotUpdateResponse = await clientIoT.send(iotUpdateCmd);
} catch { }
})();
}
}
Here I am creating a SNS to receive the results from device defender audit.
It did not work as well.
This is the expected result after run the CDK (The Device Defender audit settings enabled to true and the Device certificate expiring enabled to true):
But this is what I am getting in both attempts (The Device Defender audit settings not enabled - IoT security audit is off):

I am out of ideas. I don't know what I am missing. Any idea would be helpful.

Here is my CDK stack (in python) to enable Device Defender, create audit and get notification in SNS: