By using AWS re:Post, you agree to the Terms of Use
/Creating EventBridge Bus, Lambdas and Security/

Creating EventBridge Bus, Lambdas and Security

0

I'm looking for sample C# code to create EventBridge Bus, Rules, a few Lambdas and IM Security Roles to access them. I prefer infrastructure as code versus cloud formation script.

2 Answers
1
Accepted Answer

Consider AWS Cloud Development Kit (CDK) for this, as it supports the C# programming language. CDK helps you concentrate on your resources by automatically implementing some aspects such as adding IAM permissions and defining the dependencies. AWS CDK synthesizes a CloudFormation template as a final step, so you don't have to write it manually. The CLI tool can also deploy the synthesized CloudFormation stack for you so you don't even have to care about uploading assets used in your stack - this will be done for you.

In this GitHub repository, you can find some examples of CDK projects demonstrating the usage and the project structure - for various programming languages, including C#.

For example, the RandomWriter project shows you how to configure an EventBridge rule, a Lambda function, and a DynamoDB table, including required IAM permissions.

The LambdaCron example is a tiny one but also shows you how to connect a Lambda function to an EventBridge rule.

Creating a custom EventBus can also be done very easily. Here's a code snippet to give you an idea how to do that. Note that all the required (basic) IAM permissions will be automatically created and attached by CDK for you.

using Amazon.CDK.AWS.Events;
using Amazon.CDK.AWS.Lambda;
using Targets = Amazon.CDK.AWS.Events.Targets;

// adding a Python function as an example here,
// but you can also use C# for the Lambda code for sure
var myLambda = new Function(this, "MyEventProcessor", new FunctionProps
{
    Code = new InlineCode("def main(event, context):\n\tprint(event)\n\treturn {'statusCode': 200, 'body': 'Hello, World'}"),
    Handler = "index.main",
    Runtime = Runtime.PYTHON_3_7
});

// a custom event bus
var eventBus = new EventBus(this, "MyLanguageBus");

// an EventBridge rule attached to the custom event bus
new Rule(this, "LambdaProcessorRule", new RuleProps
{
    EventBus = eventBus,
    EventPattern = new EventPattern { Source = new[] { "com.amazon.alexa.english" } },
    Targets = new[] { new Targets.LambdaFunction(myLambda) }
});
answered 3 months ago
1

Why not use CDK.

EXPERT
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions