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

Why not use CDK.

profile pictureAWS
전문가
Uri
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠