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 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南