Skip to content

Commit be241dd

Browse files
RomainMullermergify[bot]
authored andcommitted
feat: add RandomWriter C# example (#153)
1 parent d858266 commit be241dd

File tree

8 files changed

+184
-0
lines changed

8 files changed

+184
-0
lines changed

csharp/random-writer/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/*/bin/
2+
src/*/obj/

csharp/random-writer/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# RandomWriter
2+
3+
## Overview
4+
This sample application demonstrates some essential mechanisms of the *AWS Cloud
5+
Development Kit* (*AWS CDK*) for .NET (in this case, using **C#**):
6+
- Creating a user-defined *construct*
7+
- Having this *construct* implement a feature interface
8+
(`Amazon.CDK.AWS.Events.IRuleTarget`) to bring specific capabilities to the
9+
*construct*
10+
- Granting permissions (for a *Lambda* function to write into a *DynamoDB*
11+
table) using the `.Grant*` methods.
12+
13+
## Application Resources
14+
> All resources provisioned by this application are *[free-tier] eligible* or
15+
> generally free to provision and use.
16+
17+
The application provisions the following elements (shown in their hierarchical
18+
presentation withing the *CDK construct tree*):
19+
20+
- A *stack* named `RandomWriterStack`
21+
- A user-defined *construct* named `RandomWriter`
22+
- A *DynamoDB* table (the physical name of which is to be determined by
23+
*AWS CloudFormation* upon creation)
24+
- A *Lambda* function
25+
* It writes a random hash into the *DynamoDB* table when invoked
26+
- A *CloudWatch* event rule that triggers an execution of the *Lambda*
27+
function *every minute*.
28+
29+
[free-tier]: https://aws.amazon.com/free/

csharp/random-writer/cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "dotnet run --project src/RandomWriter/RandomWriter.csproj"
3+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomWriter", "RandomWriter\RandomWriter.csproj", "{0B87A53B-C322-4A64-9C52-7A047E296339}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Debug|x64.Build.0 = Debug|Any CPU
25+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Debug|x86.Build.0 = Debug|Any CPU
27+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Release|x64.ActiveCfg = Release|Any CPU
30+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Release|x64.Build.0 = Release|Any CPU
31+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Release|x86.ActiveCfg = Release|Any CPU
32+
{0B87A53B-C322-4A64-9C52-7A047E296339}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Amazon.CDK;
2+
3+
namespace RandomWriter
4+
{
5+
public sealed class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
var app = new App();
10+
new RandomWriterStack(app, "RandomWriterStack");
11+
app.Synth();
12+
}
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Amazon.CDK" Version="1.15.0-devpreview" />
10+
<PackageReference Include="Amazon.CDK.AWS.DynamoDb" Version="1.15.0-devpreview" />
11+
<PackageReference Include="Amazon.CDK.AWS.Events" Version="1.15.0-devpreview" />
12+
<PackageReference Include="Amazon.CDK.AWS.Events.Targets" Version="1.15.0-devpreview" />
13+
<PackageReference Include="Amazon.CDK.AWS.Lambda" Version="1.15.0-devpreview" />
14+
15+
<PackageReference Include="Amazon.JSII.Analyzers" Version="0.20.1" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { DynamoDB } = require('aws-sdk');
2+
const crypto = require('crypto');
3+
4+
/**
5+
* This Lambda event handler expects the name of a DynamoDB table to be passed
6+
* in the `TABLE_NAME` environment variable. The Lambda function must be granted
7+
* WRITE permissions on the DynamoDB table (for it will add new items in the
8+
* table).
9+
*
10+
* The DynamoDB table must have a hash-only primary key, where the hash key is
11+
* named `ID` and is of type STRING.
12+
*/
13+
exports.handler = async function handler(event, context) {
14+
console.log(JSON.stringify(event, undefined, 2));
15+
16+
var seed = `${Date.now}${Math.random()}`;
17+
const id = crypto.createHash('sha1').update(seed).digest('hex');
18+
19+
const ddb = new DynamoDB();
20+
await ddb.putItem({
21+
TableName: process.env.TABLE_NAME,
22+
Item: {
23+
ID: { S: id }
24+
}
25+
}).promise();
26+
};

0 commit comments

Comments
 (0)