generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 27
Update sample apps readme #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0674123
Update sample apps
GarrettBeatty 7d44c76
update readme
GarrettBeatty 5e6da1a
update deps
GarrettBeatty 0339d9c
more readmes
GarrettBeatty 7b1adcd
launch profile
GarrettBeatty 2af9f3f
update readme
GarrettBeatty ff164e2
update readme
GarrettBeatty bd8f983
update package versions
GarrettBeatty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "Records": [ | ||
| { | ||
| "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78", | ||
| "receiptHandle": "MessageReceiptHandle", | ||
| "body": "{\"id\":\"d9b4bfc7-9398-44aa-8049-85c07490fb35\",\"source\":\"/AWSLambda/FunctionName\",\"specversion\":\"1.0\",\"type\":\"chatMessage\",\"time\":\"2024-03-22T21:01:03.5484607+00:00\",\"data\":\"{\\u0022MessageDescription\\u0022:\\u0022Testing!!!\\u0022}\"}", | ||
| "attributes": { | ||
| "ApproximateReceiveCount": "1", | ||
| "SentTimestamp": "1523232000000", | ||
| "SenderId": "123456789012", | ||
| "ApproximateFirstReceiveTimestamp": "1523232000001" | ||
| }, | ||
| "messageAttributes": {}, | ||
| "md5OfBody": "7b270e59b47ff90a553787216d55d91d", | ||
| "eventSource": "aws:sqs", | ||
| "eventSourceARN": "arn:{partition}:sqs:{region}:123456789012:MyQueue", | ||
| "awsRegion": "{region}" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| { | ||
| "profiles": { | ||
| "Mock Lambda Test Tool": { | ||
| "Default": { | ||
| "workingDirectory": ".\\bin\\$(Configuration)\\net8.0", | ||
| "commandName": "Executable", | ||
| "commandLineArgs": "--port 5050", | ||
| "workingDirectory": ".\\bin\\$(Configuration)\\net6.0", | ||
| "executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe" | ||
| "commandLineArgs": "exec --depsfile ./LambdaMessaging.deps.json --runtimeconfig ./LambdaMessaging.runtimeconfig.json %USERPROFILE%/.dotnet/tools/.store/amazon.lambda.testtool/${VERSION}/amazon.lambda.testtool/${VERSION}/content/Amazon.Lambda.RuntimeSupport/net8.0/Amazon.Lambda.RuntimeSupport.dll LambdaMessaging::LambdaMessaging.Function_FunctionHandler_Generated::FunctionHandler", | ||
| "executablePath": "dotnet", | ||
| "environmentVariables": { | ||
| "AWS_LAMBDA_RUNTIME_API": "localhost:5050/MyFunction" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Lambda Messaging Sample Application | ||
|
|
||
| This sample application demonstrates how to use AWS Lambda with the AWS Message Processing Framework for .NET to process messages from SQS queues. | ||
|
|
||
| ## Overview | ||
|
|
||
| This sample shows how to: | ||
| - Configure a Lambda function to process messages from SQS | ||
| - Use dependency injection with Lambda Annotations | ||
| - Handle message batch processing | ||
| - Implement partial batch failure responses | ||
| - Set up message handlers for specific message types | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - .NET 8.0 or later | ||
|
|
||
|
|
||
| ## Project Structure | ||
|
|
||
| ``` | ||
| LambdaMessaging/ | ||
| ├── Function.cs # Lambda function handler | ||
| ├── Startup.cs # DI and service configuration | ||
| ├── ChatMessage.cs # Message type definition | ||
| ├── ChatMessageHandler.cs # Message handler implementation | ||
| ├── LambdaMessaging.csproj # Project file | ||
| ``` | ||
|
|
||
|
|
||
| ## Getting Started | ||
|
|
||
| In order to test the lambda function locally with the messaging processing framework, it requires installing the Lambda Test Tool https://github.com/aws/aws-lambda-dotnet/blob/master/Tools/LambdaTestTool-v2 first. | ||
|
|
||
| 1. Build the project | ||
|
|
||
| ``` | ||
| dotnet build | ||
| ``` | ||
|
|
||
| 2Install the AWS Lambda Test Tool: | ||
| ```bash | ||
| dotnet tool install -g amazon.lambda.testtool | ||
| ``` | ||
|
|
||
| 3. Start the Lambda Test Tool: | ||
|
|
||
| ```bash | ||
| dotnet lambda-test-tool start --lambda-emulator-port 5050 | ||
| ``` | ||
|
|
||
| 4. Get the test tool version: | ||
|
|
||
| ``` | ||
| dotnet lambda-test-tool info | ||
| ``` | ||
|
|
||
| 5. Run the `LambdaMessaging` project. | ||
|
|
||
| There are 2 ways to run it | ||
| 1. Visual studio (easiest way). | ||
| 1a. Update `Properties/launchSettings.json` and replace `${VERSIOMN} with the actual test tool version. | ||
| 2. Via command line | ||
|
|
||
| ``` | ||
| cd bin\Debug\net8.0 | ||
| $env:AWS_LAMBDA_RUNTIME_API = "localhost:5050/MyFunction" | ||
| $env:VERSION = "0.9.1" // Use the version returned from dotnet lambda-test-tool info | ||
|
|
||
| dotnet exec --depsfile ./LambdaMessaging.deps.json --runtimeconfig ./LambdaMessaging.runtimeconfig.json "$env:USERPROFILE\.dotnet\tools\.store\amazon.lambda.testtool\$env:VERSION\amazon.lambda.testtool\$env:VERSION\content\Amazon.Lambda.RuntimeSupport\net8.0\Amazon.Lambda.RuntimeSupport.dll" LambdaMessaging::LambdaMessaging.Function_FunctionHandler_Generated::FunctionHandler | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| 6. You should now see the `MyFunction` appear in the test tools function list drop down in the top right corner. Select `MyFunction`. | ||
|
|
||
| 7. We have provided a `HandlerSampleRequest.json` file to be used to test this function. Copy and paste this json into the test tools input window and then hit the "invoke button". | ||
|
|
||
| 8. You should see in the console window that the `ChatMessageHandler` successfully processed the message. There should be a log statement saying `Message Description: Testing!!!`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,31 @@ | ||
| using Amazon.Lambda.Annotations; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace LambdaMessaging; | ||
|
|
||
| [LambdaStartup] | ||
| public class Startup | ||
| { | ||
| public void ConfigureServices(IServiceCollection services) | ||
| public HostApplicationBuilder ConfigureHostBuilder() | ||
| { | ||
| services.AddLogging(builder => | ||
| var builder = new HostApplicationBuilder(); | ||
| builder.Services.AddLogging(b => | ||
| { | ||
| builder.SetMinimumLevel(LogLevel.Trace); | ||
| builder.AddLambdaLogger(); | ||
| b.SetMinimumLevel(LogLevel.Trace); | ||
| b.AddLambdaLogger(); | ||
| }); | ||
| services.AddAWSMessageBus(builder => | ||
| builder.Services.AddAWSMessageBus(b => | ||
| { | ||
| builder.AddMessageHandler<ChatMessageHandler, ChatMessage>("chatMessage"); | ||
| b.AddMessageHandler<ChatMessageHandler, ChatMessage>("chatMessage"); | ||
|
|
||
| builder.AddLambdaMessageProcessor(options => | ||
| b.AddLambdaMessageProcessor(options => | ||
| { | ||
| options.MaxNumberOfConcurrentMessages = 2; | ||
| }); | ||
| }); | ||
|
|
||
| return builder; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # AWS Message Processing Framework with Polly Integration | ||
|
|
||
| This sample application demonstrates how to use the AWS Message Processing Framework for .NET with Polly for resilient message processing. It showcases integration between SQS message processing and Polly's retry policies. | ||
|
|
||
| ## Overview | ||
|
|
||
| This sample demonstrates: | ||
| - Integration of AWS Message Processing Framework with Polly for resilient messaging | ||
| - Custom backoff handling for message processing | ||
| - SQS message processing with typed handlers | ||
| - Both code-based and configuration-based setup options | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - .NET 8.0 or later | ||
| - AWS Account with appropriate permissions | ||
| - Basic understanding of Amazon SQS and AWS Message Processing Framework | ||
|
|
||
| ## Setup | ||
|
|
||
| ### 1. Create an SQS Queue | ||
|
|
||
| 1. Open the AWS Management Console | ||
| 2. Navigate to Amazon SQS | ||
| 3. Click "Create Queue" | ||
| 4. Choose "Standard Queue" | ||
| 5. Enter a queue name (e.g., "MPF") | ||
| 6. Keep default settings for this demo | ||
| 7. Click "Create Queue" | ||
| 8. Copy the Queue URL - you'll need this later | ||
|
|
||
| ### 2. Configure Message Processing | ||
|
|
||
| You can choose either configuration approach: | ||
|
|
||
| #### Option A: Code-based Configuration | ||
|
|
||
| In `Program.cs`, update the queue URL and keep these lines uncommented: | ||
| ```csharp | ||
| builder.AddSQSPoller("https://sqs.us-west-2.amazonaws.com/012345678910/MPF"); // Replace with your Queue URL | ||
| builder.AddMessageHandler<ChatMessageHandler, ChatMessage>("chatMessage"); | ||
| ``` | ||
| And keep this line commented: | ||
| ``` | ||
| // builder.LoadConfigurationFromSettings(context.Configuration); | ||
| ``` | ||
|
|
||
| #### Option B: Configuration-based (appsettings.json) | ||
| 1. Comment out the code-based configuration in Program.cs: | ||
|
|
||
| ``` | ||
| // builder.AddSQSPoller("https://sqs.us-west-2.amazonaws.com/012345678910/MPF"); | ||
| // builder.AddMessageHandler<ChatMessageHandler, ChatMessage>("chatMessage"); | ||
| ``` | ||
| 2. Uncomment the configuration loading: | ||
|
|
||
| ``` | ||
| builder.LoadConfigurationFromSettings(context.Configuration); | ||
| ``` | ||
| 3. Update appsettings.json: | ||
|
|
||
| ``` | ||
| { | ||
| "AWS.Messaging": { | ||
| "SQSPollers": [ | ||
| { | ||
| "QueueUrl": "https://sqs.us-west-2.amazonaws.com/012345678910/MPF" // Replace with your Queue URL | ||
| } | ||
| ], | ||
| "MessageHandlers": [ | ||
| { | ||
| "HandlerType": "PollyIntegration.MessageHandlers.ChatMessageHandler", | ||
| "MessageType": "PollyIntegration.Models.ChatMessage", | ||
| "MessageTypeIdentifier": "chatMessage" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| Choose Option A if you: | ||
|
|
||
| - Want configuration close to the code | ||
|
|
||
| - Need dynamic runtime configuration | ||
|
|
||
| - Are prototyping or testing | ||
|
|
||
|
|
||
| Choose Option B if you: | ||
|
|
||
| - Need configuration changes without recompiling | ||
|
|
||
| - Want environment-specific settings | ||
|
|
||
| - Prefer separation of configuration from code | ||
|
|
||
| - Need to manage multiple configurations | ||
|
|
||
|
|
||
| ### 3. Configure AWS Credentials | ||
|
|
||
| Ensure you have AWS credentials configured either through: | ||
|
|
||
| - AWS CLI | ||
|
|
||
| - Environment variables | ||
|
|
||
| - AWS credentials file | ||
|
|
||
| - IAM role (if running on AWS) | ||
|
|
||
| ## Project Structure | ||
| ``` | ||
| PollyIntegration/ | ||
| ├── MessageHandlers/ | ||
| │ └── ChatMessageHandler.cs # Sample message handler | ||
| ├── Models/ | ||
| │ └── ChatMessage.cs # Message type definition | ||
| ├── Program.cs # Application entry point | ||
| ├── PollyBackoffHandler.cs # Custom Polly integration | ||
| └── appsettings.json # Application configuration | ||
| ``` | ||
|
|
||
| ## Running the Application | ||
| 1. Build the project | ||
| ``` | ||
| dotnet build | ||
| ``` | ||
| 2. Run the application | ||
| ``` | ||
| dotnet run | ||
| ``` | ||
| ## Testing | ||
|
|
||
| ### Send a Test Message | ||
|
|
||
| You can send a test message to your SQS queue using the AWS Console or AWS CLI: | ||
|
|
||
| Using AWS CLI: | ||
| ``` | ||
|
|
||
| $messageBody = "{""""type"""":""""chatMessage"""",""""id"""":""""123"""",""""source"""":""""test"""",""""specversion"""":""""1.0"""",""""time"""":""""2024-01-01T00:00:00Z"""",""""data"""":""""{\\""""messageDescription\\"""":\\""""Test message\\""""}""""}" | ||
|
|
||
| aws sqs send-message --queue-url YOUR_QUEUE_URL --message-body $messageBody | ||
|
|
||
| ``` | ||
| Replace YOUR_QUEUE_URL with your actual SQS queue URL. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was trying for an hour or so to figure out how to make this command better to create the messagebody but was having issues with the serialization. creating the string manually was the easiest way for now. will have to come back to this