Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iot): configure IoT Logging #31352

Merged
merged 11 commits into from
Sep 10, 2024
Prev Previous commit
Next Next commit
add unit test
  • Loading branch information
badmintoncryer committed Sep 7, 2024
commit 05dcfaab3621cf7c0405dfe8c231cf6b0a86a3ef
99 changes: 99 additions & 0 deletions packages/@aws-cdk/aws-iot-alpha/test/logging.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Template } from 'aws-cdk-lib/assertions';
import * as cdk from 'aws-cdk-lib';
import * as iot from '../lib';

test('Default property', () => {
const stack = new cdk.Stack();

new iot.Logging(stack, 'Logging');

Template.fromStack(stack).hasResourceProperties('AWS::IoT::Logging', {
DefaultLogLevel: 'ERROR',
AccountId: { Ref: 'AWS::AccountId' },
RoleArn: { 'Fn::GetAtt': ['LoggingRoleF8CB8FA1', 'Arn'] },
});

Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', {
AssumeRolePolicyDocument: {
Statement: [
{
Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: { Service: 'iot.amazonaws.com' },
},
],
Version: '2012-10-17',
},
Policies: [
{
PolicyDocument: {
Statement: [
{
Action: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents',
'logs:PutMetricFilter',
'logs:PutRetentionPolicy',
'iot:GetLoggingOptions',
'iot:SetLoggingOptions',
'iot:SetV2LoggingOptions',
'iot:GetV2LoggingOptions',
'iot:SetV2LoggingLevel',
'iot:ListV2LoggingLevels',
'iot:DeleteV2LoggingLevel',
],
Effect: 'Allow',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{ Ref: 'AWS::Partition' },
':logs:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':log-group:AWSIotLogsV2:*',
],
],
},
},
],
Version: '2012-10-17',
},
PolicyName: 'LoggingPolicy',
},
],
});
});

test.each([
iot.LogLevel.ERROR,
iot.LogLevel.WARN,
iot.LogLevel.INFO,
iot.LogLevel.DEBUG,
iot.LogLevel.DISABLED,
])('Log level %s', (logLevel) => {
const stack = new cdk.Stack();

new iot.Logging(stack, 'Logging', {
logLevel,
});

Template.fromStack(stack).hasResourceProperties('AWS::IoT::Logging', {
DefaultLogLevel: logLevel,
});
});

test('import by Log ID', () => {
const stack = new cdk.Stack();

const logId = 'Log-12345';

const logging = iot.Logging.fromLogId(stack, 'LoggingFromId', logId);

expect(logging).toMatchObject({
logId,
});
});
Loading