constructor()

in source/infrastructure/lib/sqs-message-consumer.ts [39:129]


  constructor(scope: Construct, id: string, props: SQSMessageConsumerConstructProps) {
    super(scope, id);

    const sourceCodeBucket = props.solutionConfig.sourceCodeBucket;
    const sourceCodePrefix = props.solutionConfig.sourceCodePrefix;

    const sqsToLambda = new SqsToLambda(this, 'SQSMessageConsumer', {
      lambdaFunctionProps: {
        code: Code.fromBucket(sourceCodeBucket, `${sourceCodePrefix}/sqs-message-consumer.zip`),
        description: 'Machine to Cloud Connectivity SQS message consumer function',
        environment: {
          LOGGING_LEVEL: props.solutionConfig.loggingLevel,
          SOLUTION_ID: props.solutionConfig.solutionId,
          SOLUTION_VERSION: props.solutionConfig.solutionVersion
        },
        handler: 'sqs-message-consumer/index.handler',
        retryAttempts: 0,
        runtime: Runtime.NODEJS_14_X,
        timeout: Duration.minutes(1)
      },
      queueProps: {
        visibilityTimeout: Duration.minutes(1)
      },
      maxReceiveCount: 3
    });

    const lambdaToDynamoDb = new LambdaToDynamoDB(this, 'LogsDynamoDB', {
      existingLambdaObj: sqsToLambda.lambdaFunction,
      dynamoTableProps: {
        partitionKey: {
          name: 'connectionName',
          type: AttributeType.STRING
        },
        sortKey: {
          name: 'timestamp',
          type: AttributeType.NUMBER
        },
        timeToLiveAttribute: 'ttl'
      },
      tableEnvironmentVariableName: 'LOGS_DYNAMODB_TABLE'
    });
    this.logsTable = lambdaToDynamoDb.dynamoTable;

    const cfnDynamoTable = this.logsTable.node.defaultChild as CfnTable;
    cfnDynamoTable.cfnOptions.deletionPolicy = CfnDeletionPolicy.DELETE;

    // Info and error logs rule
    const sqsSendRole = new Role(this, 'IoTRuleSQSRole', {
      assumedBy: new ServicePrincipal('iot.amazonaws.com'),
      path: '/service-role/',
      inlinePolicies: {
        'IoTRuleSQSPolicy': new PolicyDocument({
          statements: [
            new PolicyStatement({
              effect: Effect.ALLOW,
              actions: ['sqs:SendMessage'],
              resources: [sqsToLambda.sqsQueue.queueArn]
            })
          ]
        })
      }
    });
    new CfnTopicRule(this, 'InfoLogsRule', { // NOSONAR: typescript:S1848
      topicRulePayload: {
        actions: [{
          sqs: {
            roleArn: sqsSendRole.roleArn,
            queueUrl: sqsToLambda.sqsQueue.queueUrl
          }
        }],
        awsIotSqlVersion: '2016-03-23',
        description: 'Processing info logs from the edge device Lambda functions',
        ruleDisabled: false,
        sql: this.sql.replace('{type}', 'info')
      }
    });
    new CfnTopicRule(this, 'ErrorLogsRule', { // NOSONAR: typescript:S1848
      topicRulePayload: {
        actions: [{
          sqs: {
            roleArn: sqsSendRole.roleArn,
            queueUrl: sqsToLambda.sqsQueue.queueUrl
          }
        }],
        awsIotSqlVersion: '2016-03-23',
        description: 'Processing error logs from the edge device Lambda functions',
        ruleDisabled: false,
        sql: this.sql.replace('{type}', 'error')
      }
    });
  }