|
| 1 | +using System.Collections.Generic; |
| 2 | +using Amazon.CDK; |
| 3 | +using Amazon.CDK.AWS.DynamoDB; |
| 4 | +using Amazon.CDK.AWS.Events; |
| 5 | +using Amazon.CDK.AWS.Events.Targets; |
| 6 | +using Amazon.CDK.AWS.Lambda; |
| 7 | + |
| 8 | +namespace RandomWriter |
| 9 | +{ |
| 10 | + internal sealed class RandomWriterStack : Stack |
| 11 | + { |
| 12 | + public RandomWriterStack(Construct parent, string id, IStackProps props = null) : base(parent, id, props) |
| 13 | + { |
| 14 | + // The code that defines your stack goes here |
| 15 | + var randomWriter = new RandomWriter(this, "RandomWriter"); |
| 16 | + new Rule(this, "Trigger", new RuleProps() |
| 17 | + { |
| 18 | + Description = "Triggers a RandomWrite every minute", |
| 19 | + Schedule = Schedule.Rate(Duration.Minutes(1)), |
| 20 | + Targets = new [] { randomWriter } |
| 21 | + }); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + internal sealed class RandomWriter : Construct, IRuleTarget |
| 26 | + { |
| 27 | + private IFunction Function { get; } |
| 28 | + |
| 29 | + public RandomWriter(Construct scope, string id): base(scope, id) |
| 30 | + { |
| 31 | + var table = new Table(this, "Table", new TableProps |
| 32 | + { |
| 33 | + PartitionKey = new Attribute |
| 34 | + { |
| 35 | + Name = "ID", |
| 36 | + Type = AttributeType.STRING |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + Function = new Function(this, "Lambda", new FunctionProps |
| 41 | + { |
| 42 | + Runtime = Runtime.NODEJS_10_X, |
| 43 | + Handler = "index.handler", |
| 44 | + Code = Code.FromAsset("src/RandomWriter/resources"), |
| 45 | + Environment = new Dictionary<string, string> |
| 46 | + { |
| 47 | + { "TABLE_NAME", table.TableName } |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + table.GrantReadWriteData(Function); |
| 52 | + } |
| 53 | + |
| 54 | + public IRuleTargetConfig Bind(IRule rule, string id = null) |
| 55 | + { |
| 56 | + return new LambdaFunction(Function).Bind(rule, id); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments