constructor()

in lib/tweet-queue.ts [50:93]


  constructor(parent: cdk.Construct, id: string, props: TweetQueueProps) {
    super(parent, id, {
      retentionPeriod: props.retentionPeriodSec === undefined ? Duration.seconds(60) : Duration.seconds(props.retentionPeriodSec),
      visibilityTimeout: props.visibilityTimeoutSec === undefined ? Duration.seconds(60) : Duration.seconds(props.visibilityTimeoutSec),
    });

    const keyName = 'id';
    const table = new dynamodb.Table(this, 'CheckpointTable', {
      partitionKey: { name: keyName, type: dynamodb.AttributeType.STRING },
    });

    const fn = new lambda.NodejsFunction(this, 'Poller', {
      entry: path.join(__dirname, 'poller', 'index.ts'),
      timeout: Duration.minutes(15),
      environment: {
        CREDENTIALS_SECRET: props.secretArn,
        TWITTER_QUERY: props.query,
        QUEUE_URL: this.queueUrl,
        CHECKPOINT_TABLE_NAME: table.tableName,
        CHECKPOINT_TABLE_KEY_NAME: keyName,
      },
    });

    fn.addToRolePolicy(new iam.PolicyStatement({
      resources: [props.secretArn],
      actions: ['secretsmanager:GetSecretValue'],
    }));

    fn.addToRolePolicy(new iam.PolicyStatement({
      resources: [this.queueArn],
      actions: ['sqs:SendMessage', 'sqs:SendMessageBatch'],
    }));

    table.grantReadWriteData(fn);

    const interval = props.intervalMin === undefined ? 1 : props.intervalMin;
    if (interval > 0) {
      const timer = new events.Rule(this, 'PollingTimer', {
        schedule: events.Schedule.rate(Duration.minutes(interval)),
      });

      timer.addTarget(new targets.LambdaFunction(fn));
    }
  }