Amazon.CDK.AWS.IoT.Alpha
The CDK Construct Library for AWS::IoT (Stability: Experimental)
Install
| Product | Versions |
|---|---|
| .NET |
README
AWS IoT Construct Library
The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
AWS IoT Core lets you connect billions of IoT devices and route trillions of messages to AWS services without managing infrastructure.
TopicRule
Create a topic rule that give your devices the ability to interact with AWS services. You can create a topic rule with an action that invoke the Lambda action as following:
var func = new Function(this, "MyFunction", new FunctionProps {
Runtime = Runtime.NODEJS_LATEST,
Handler = "index.handler",
Code = Code.FromInline(@"
exports.handler = (event) => {
console.log(""It is test for lambda action of AWS IoT Rule."", event);
};")
});
new TopicRule(this, "TopicRule", new TopicRuleProps {
TopicRuleName = "MyTopicRule", // optional
Description = "invokes the lambda function", // optional
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"),
Actions = new [] { new LambdaFunctionAction(func) }
});
Or, you can add an action after constructing the TopicRule instance as following:
Function func;
var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'")
});
topicRule.AddAction(new LambdaFunctionAction(func));
You can also supply errorAction as following,
and the IoT Rule will trigger it if a rule's action is unable to perform:
using Amazon.CDK.AWS.Logs;
var logGroup = new LogGroup(this, "MyLogGroup");
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"),
ErrorAction = new CloudWatchLogsAction(logGroup)
});
If you wanna make the topic rule disable, add property enabled: false as following:
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"),
Enabled = false
});
See also @aws-cdk/aws-iot-actions-alpha for other actions.
Logging
AWS IoT provides a logging feature that allows you to monitor and log AWS IoT activity.
You can enable IoT logging with the following code:
new Logging(this, "Logging", new LoggingProps {
LogLevel = LogLevel.INFO
});
Note: All logs are forwarded to the AWSIotLogsV2 log group in CloudWatch.
Audit
An AWS IoT Device Defender audit looks at account- and device-related settings and policies to ensure security measures are in place. An audit can help you detect any drifts from security best practices or access policies.
Account Audit Configuration
The IoT audit includes various audit checks, and it is necessary to configure settings to enable those checks.
You can enable an account audit configuration with the following code:
// Audit notification are sent to the SNS topic
ITopic targetTopic;
new AccountAuditConfiguration(this, "AuditConfiguration", new AccountAuditConfigurationProps {
TargetTopic = targetTopic
});
By default, all audit checks are enabled, but it is also possible to enable only specific audit checks.
new AccountAuditConfiguration(this, "AuditConfiguration", new AccountAuditConfigurationProps {
CheckConfiguration = new CheckConfiguration {
// enabled
AuthenticatedCognitoRoleOverlyPermissiveCheck = true,
// enabled by default
CaCertificateExpiringCheck = undefined,
// disabled
CaCertificateKeyQualityCheck = false,
ConflictingClientIdsCheck = false,
DeviceCertificateAgeCheck = false,
DeviceCertificateExpiringCheck = false,
DeviceCertificateKeyQualityCheck = false,
DeviceCertificateSharedCheck = false,
IntermediateCaRevokedForActiveDeviceCertificatesCheck = false,
IoTPolicyPotentialMisConfigurationCheck = false,
IotPolicyOverlyPermissiveCheck = false,
IotRoleAliasAllowsAccessToUnusedServicesCheck = false,
IotRoleAliasOverlyPermissiveCheck = false,
LoggingDisabledCheck = false,
RevokedCaCertificateStillActiveCheck = false,
RevokedDeviceCertificateStillActiveCheck = false,
UnauthenticatedCognitoRoleOverlyPermissiveCheck = false
}
});
To configure the device certificate age check, you can specify the duration for the check:
using Amazon.CDK;
new AccountAuditConfiguration(this, "AuditConfiguration", new AccountAuditConfigurationProps {
CheckConfiguration = new CheckConfiguration {
DeviceCertificateAgeCheck = true,
// The default value is 365 days
// Valid values range from 30 days (minimum) to 3650 days (10 years, maximum)
DeviceCertificateAgeCheckDuration = Duration.Days(365)
}
});
Scheduled Audit
You can create a scheduled audit that is run at a specified time interval. Checks must be enabled for your account by creating AccountAuditConfiguration.
AccountAuditConfiguration config;
// Daily audit
var dailyAudit = new ScheduledAudit(this, "DailyAudit", new ScheduledAuditProps {
AccountAuditConfiguration = config,
Frequency = Frequency.DAILY,
AuditChecks = new [] { AuditCheck.AUTHENTICATED_COGNITO_ROLE_OVERLY_PERMISSIVE_CHECK }
});
// Weekly audit
var weeklyAudit = new ScheduledAudit(this, "WeeklyAudit", new ScheduledAuditProps {
AccountAuditConfiguration = config,
Frequency = Frequency.WEEKLY,
DayOfWeek = DayOfWeek.SUNDAY,
AuditChecks = new [] { AuditCheck.CA_CERTIFICATE_EXPIRING_CHECK }
});
// Monthly audit
var monthlyAudit = new ScheduledAudit(this, "MonthlyAudit", new ScheduledAuditProps {
AccountAuditConfiguration = config,
Frequency = Frequency.MONTHLY,
DayOfMonth = DayOfMonth.Of(1),
AuditChecks = new [] { AuditCheck.CA_CERTIFICATE_KEY_QUALITY_CHECK }
});
Changes from Previous Version
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.