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 回答
1
已接受的回答

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) }
});
profile pictureAWS
已回答 2 年前
profile picture
专家
已审核 1 个月前
1

Why not use CDK.

profile pictureAWS
专家
Uri
已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则