How to use @aws-cdk/aws-events - 10 common examples

To help you get started, we’ve selected a few @aws-cdk/aws-events examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github punchcard / punchcard / examples / lib / data-lake.ts View on Github external
// Kinesis -> Lambda
// Note: the type-safety of the `record`
lake.pipelines.dataPoints.stream
  .records()
  .forEach(stack, 'ForEachDataPoint', {}, async (record) => {
    console.log('key', record.key);
    console.log('value', record.value);
    console.log('data points', record.data_points);
    console.log('timestamp', record.timestamp);
    // console.log('this does not compile', record.doesNotExist)
  });

// send some dumy data to the dataPoints schema
Lambda.schedule(stack, 'DummyDataPoints', {
  depends: lake.pipelines.dataPoints.stream.writeAccess(),
  schedule: Schedule.rate(Duration.minutes(1)),
}, async (_, stream) => {
  await stream.putRecord({
    Data: {
      key: 'key',
      data_points: [0, 1, 2],
      timestamp: new Date(),
      value: 'some-value'
    }
  });
});
github punchcard / punchcard / examples / lib / invoke-function.ts View on Github external
count: 1,
        anyProperty: {
          this: 'property can be any type supported by the AWS.DynamoDB.DocumentClient',
        }
      },
      if: item => DynamoDB.attribute_not_exists(item.id)
    });
    newCount = 1;
  }
  return newCount;
});

// call the incrementer function from another Lambda Function
Lambda.schedule(stack, 'Caller', {
  depends: incrementer.invokeAccess(),
  schedule: Schedule.rate(Duration.minutes(1)),
}, async (_, incrementer) => {
  const newCount = await incrementer.invoke({
    id: 'id'
  });
  console.log(`new count of 'id' is ${newCount}`);
});
github duo-labs / cloudmapper / auditor / lib / cloudmapperauditor-stack.js View on Github external
taskDefinition.addToTaskRolePolicy(new iam.PolicyStatement({
      // This IAM privilege has no paths or conditions
      resources: ["*"],
      actions: ['cloudwatch:PutMetricData']
    }));

    // Grant the ability to read from Secrets Manager
    taskDefinition.addToTaskRolePolicy(new iam.PolicyStatement({
      // This IAM privilege has no paths or conditions
      resources: ["*"],
      actions: ['secretsmanager:GetSecretValue'],
      conditions: {'ForAnyValue:StringLike':{'secretsmanager:SecretId': '*cloudmapper-slack-webhook*'}}
    }));

    // Create rule to trigger this be run every 24 hours
    new events.Rule(this, "scheduled_run", {
      ruleName: "cloudmapper_scheduler",
      // Run at 2am EST (6am UTC) every night
      schedule: events.Schedule.expression("cron(0 6 * * ? *)"),
      description: "Starts the CloudMapper auditing task every night",
      targets: [new targets.EcsTask({
        cluster: cluster,
        taskDefinition: taskDefinition,
        subnetSelection: {subnetType: ec2.SubnetType.PUBLIC}
      })]
    });

    // Create rule to trigger this manually
    new events.Rule(this, "manual_run", {
      ruleName: "cloudmapper_manual_run",
      eventPattern: {source: ['cloudmapper']},
      description: "Allows CloudMapper auditing to be manually started",
github aws / aws-cdk / packages / @aws-cdk / aws-ecs-patterns / lib / base / scheduled-task-base.ts View on Github external
constructor(scope: Construct, id: string, props: ScheduledTaskBaseProps) {
    super(scope, id);

    this.cluster = props.cluster || this.getDefaultCluster(this, props.vpc);
    this.desiredTaskCount = props.desiredTaskCount || 1;

    // An EventRule that describes the event trigger (in this case a scheduled run)
    this.eventRule = new Rule(this, 'ScheduledEventRule', {
      schedule: props.schedule,
    });

    this.logDriver = props.logDriver !== undefined
                        ? props.logDriver
                        : this.createAWSLogDriver(this.node.id);
  }
github aws-samples / aws-cdk-changelogs-demo / changelogs-md.js View on Github external
runtime: lambda.Runtime.NODEJS_10_X,
      handler: 'recrawl.handle',
      code: lambda.Code.asset('./app/recrawl'),
      timeout: cdk.Duration.minutes(5),
      environment: {
        CHANGELOGS_TABLE_NAME: props.changelogsTable.tableName,
        DISCOVERED_TOPIC_NAME: props.toCrawlTopic.topicArn
      }
    });

    // Grant the lambda permission to modify the tables
    props.changelogsTable.grantReadWriteData(recrawlLambda.role);
    props.toCrawlTopic.grantPublish(recrawlLambda.role);

    // Schedule the recrawler to run once every minute
    this.eventRule = new events.Rule(this, 'recrawl-check-schedule', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
      targets: [
        new targets.LambdaFunction(recrawlLambda)
      ]
    });
  }
}
github aws-samples / aws-cdk-changelogs-demo / changelogs-md.js View on Github external
runtime: lambda.Runtime.NODEJS_10_X,
      handler: 'rubygem-recent.handle',
      code: lambda.Code.asset('./app/rubygem-recent'),
      timeout: cdk.Duration.minutes(1),
      environment: {
        CHANGELOGS_TABLE_NAME: props.changelogsTable.tableName,
        DISCOVERED_TOPIC_NAME: props.toCrawlTopic.topicArn
      }
    });

    // Grant this application access to the DynamoDB table and SNS topic
    props.changelogsTable.grantReadWriteData(rubygemFollower.role);
    props.toCrawlTopic.grantPublish(rubygemFollower.role);

    // Schedule the follower to run once every minute
    this.eventRule = new events.Rule(this, 'check-recent-rubygems', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(5)),
      targets: [
        new targets.LambdaFunction(rubygemFollower)
      ]
    });
  }
}
github aws-samples / aws-cdk-changelogs-demo / changelogs-md.js View on Github external
runtime: lambda.Runtime.NODEJS_10_X,
      handler: 'pypi-recent.handle',
      code: lambda.Code.asset('./app/pypi-recent'),
      timeout: cdk.Duration.minutes(1),
      environment: {
        CHANGELOGS_TABLE_NAME: props.changelogsTable.tableName,
        DISCOVERED_TOPIC_NAME: props.toCrawlTopic.topicArn
      }
    });

    // Grant this application access to the DynamoDB table and SNS topic
    props.changelogsTable.grantReadWriteData(pypiFollower.role);
    props.toCrawlTopic.grantPublish(pypiFollower.role);

    // Schedule the follower to run once every minute
    this.eventRule = new events.Rule(this, 'check-recent-pypi', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(5)),
      targets: [
        new targets.LambdaFunction(pypiFollower)
      ]
    });
  }
}
github punchcard / punchcard / examples / lib / dynamodb.ts View on Github external
});

// 'count' is the sortKey in this case
const sortedTable: DynamoDB.Table<'id', 'count', Item> = new DynamoDB.Table(stack, 'sorted-table', {
  partitionKey: 'id',
  sortKey: 'count',
  attributes: Item,
  tableProps: Build.of({
    billingMode: BillingMode.PAY_PER_REQUEST
  })
});

// call the incrementer function from another Lambda Function
Lambda.schedule(stack, 'Caller', {
  depends: Core.Dependency.concat(table.readWriteAccess(), sortedTable.readAccess()),
  schedule: Schedule.rate(Duration.minutes(1)),
}, async (_, [table, sortedTable]) => {
  await table.get({
    id: 'id',
  });

  await table.put({
    // the item is type-safe and well structured
    item: {
      id: 'id',
      count: 1,
      name: 'name',
      any: {
        a: 'value'
      },
      array: ['some', 'values'],
      struct: {
github aws-samples / aws-cdk-changelogs-demo / changelogs-md.js View on Github external
handler: 'pypi-recent.handle',
      code: lambda.Code.asset('./app/pypi-recent'),
      timeout: cdk.Duration.minutes(1),
      environment: {
        CHANGELOGS_TABLE_NAME: props.changelogsTable.tableName,
        DISCOVERED_TOPIC_NAME: props.toCrawlTopic.topicArn
      }
    });

    // Grant this application access to the DynamoDB table and SNS topic
    props.changelogsTable.grantReadWriteData(pypiFollower.role);
    props.toCrawlTopic.grantPublish(pypiFollower.role);

    // Schedule the follower to run once every minute
    this.eventRule = new events.Rule(this, 'check-recent-pypi', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(5)),
      targets: [
        new targets.LambdaFunction(pypiFollower)
      ]
    });
  }
}
github duo-labs / cloudtrail-partitioner / lib / cloudtrail_partitioner-stack.js View on Github external
"DATABASE": config["database"],
        "TABLE_PREFIX": config["table_prefix"]
      }
    });

    if (config['output_s3_bucket'] == "default") {
      // This is only used for the IAM policy, we leave this as *-* because there is not
      // an easy way of figuring out the AWS account from within the CDK
      config['output_s3_bucket'] = "aws-athena-query-results-*-*"
    }

    // Create rule to trigger this be run every 24 hours
    new events.Rule(this, "scheduled_run", {
      ruleName: "athena_partitioner_for_cloudtrail",
      // Run at 10pm EST (midnight UTC) every night
      schedule: events.Schedule.expression("cron(0 0 * * ? *)"),
      description: "Starts the CloudMapper auditing task every night",
      targets: [new targets.LambdaFunction(partitioner)]
    });

    // Grant access to Athena, Glue, and identifying the regions
    partitioner.addToRolePolicy(new iam.PolicyStatement({
      resources: ['*'],
      actions: [
        "athena:StartQueryExecution",
        "athena:GetQueryExecution",
        "athena:GetQueryResults",
        "glue:BatchCreatePartition",
        "glue:BatchGetPartition",
        "glue:CreateTable",
        "glue:CreateDatabase",
        "glue:GetDatabase",