@@ -7,15 +7,20 @@ import {
77 aws_iam as IAM ,
88 aws_iot as IoT ,
99 aws_lambda as Lambda ,
10+ aws_logs as Logs ,
11+ RemovalPolicy ,
12+ Stack ,
1013} from 'aws-cdk-lib'
1114import { Construct } from 'constructs'
1215import type { BackendLambdas } from '../BackendLambdas.js'
1316import type { PublicDevices } from './PublicDevices.js'
17+ import { RetentionDays } from 'aws-cdk-lib/aws-logs'
1418
1519/**
1620 * Handle incoming SenML messages
1721 */
1822export class SenMLMessages extends Construct {
23+ public readonly importLogsFn : Lambda . IFunction
1924 constructor (
2025 parent : Construct ,
2126 {
@@ -24,12 +29,19 @@ export class SenMLMessages extends Construct {
2429 publicDevices,
2530 } : {
2631 baseLayer : Lambda . ILayerVersion
27- lambdaSources : Pick < BackendLambdas , 'senMLToLwM2M' >
32+ lambdaSources : Pick < BackendLambdas , 'senMLToLwM2M' | 'senMLImportLogs' >
2833 publicDevices : PublicDevices
2934 } ,
3035 ) {
3136 super ( parent , 'senml-messages' )
3237
38+ const importLogs = new Logs . LogGroup ( this , 'importLogs' , {
39+ logGroupName : `${ Stack . of ( this ) . stackName } /senml-device-message-import` ,
40+ retention : RetentionDays . ONE_MONTH ,
41+ logGroupClass : Logs . LogGroupClass . INFREQUENT_ACCESS ,
42+ removalPolicy : RemovalPolicy . DESTROY ,
43+ } )
44+
3345 const fn = new Lambda . Function ( this , 'fn' , {
3446 handler : lambdaSources . senMLToLwM2M . handler ,
3547 architecture : Lambda . Architecture . ARM_64 ,
@@ -43,6 +55,7 @@ export class SenMLMessages extends Construct {
4355 environment : {
4456 VERSION : this . node . getContext ( 'version' ) ,
4557 PUBLIC_DEVICES_TABLE_NAME : publicDevices . publicDevicesTable . tableName ,
58+ IMPORT_LOGGROUP_NAME : importLogs . logGroupName ,
4659 } ,
4760 initialPolicy : [
4861 new IAM . PolicyStatement ( {
@@ -53,6 +66,7 @@ export class SenMLMessages extends Construct {
5366 ...new LambdaLogGroup ( this , 'fnLogs' ) ,
5467 } )
5568 publicDevices . publicDevicesTable . grantReadData ( fn )
69+ importLogs . grantWrite ( fn )
5670
5771 const rule = new IoT . CfnTopicRule ( this , 'rule' , {
5872 topicRulePayload : {
@@ -86,5 +100,28 @@ export class SenMLMessages extends Construct {
86100 ) as IAM . IPrincipal ,
87101 sourceArn : rule . attrArn ,
88102 } )
103+
104+ this . importLogsFn = new Lambda . Function ( this , 'importLogsFn' , {
105+ handler : lambdaSources . senMLImportLogs . handler ,
106+ architecture : Lambda . Architecture . ARM_64 ,
107+ runtime : Lambda . Runtime . NODEJS_20_X ,
108+ timeout : Duration . minutes ( 1 ) ,
109+ memorySize : 1792 ,
110+ code : Lambda . Code . fromAsset ( lambdaSources . senMLImportLogs . zipFile ) ,
111+ description : 'Returns the last import messages for a device.' ,
112+ layers : [ baseLayer ] ,
113+ environment : {
114+ VERSION : this . node . getContext ( 'version' ) ,
115+ IMPORT_LOGGROUP_NAME : importLogs . logGroupName ,
116+ } ,
117+ ...new LambdaLogGroup ( this , 'importLogsFnLogs' ) ,
118+ initialPolicy : [
119+ new IAM . PolicyStatement ( {
120+ actions : [ 'logs:StartQuery' , 'logs:GetQueryResults' ] ,
121+ resources : [ importLogs . logGroupArn ] ,
122+ } ) ,
123+ ] ,
124+ } )
125+ importLogs . grantRead ( this . importLogsFn )
89126 }
90127}
0 commit comments