├── .gitignore ├── README.md └── src └── Sample.LambdaFunction ├── Function.cs ├── Readme.md ├── Sample.LambdaFunction.csproj └── aws-lambda-tools-defaults.json /.gitignore: -------------------------------------------------------------------------------- 1 | build_output/* 2 | build_artifacts/* 3 | build_temp/* 4 | *.suo 5 | *.user 6 | packages 7 | *.dotCover 8 | 9 | *.ncrunch* 10 | .vs 11 | 12 | .fake 13 | 14 | src/logs/* 15 | 16 | **/*.sln* 17 | bin 18 | obj 19 | _ReSharper* 20 | 21 | *.csproj.user 22 | *.resharper.user 23 | *.resharper 24 | *.ReSharper 25 | *.cache 26 | *~ 27 | *.swp 28 | *.bak 29 | *.orig 30 | 31 | NuGet.exe 32 | packages 33 | 34 | # Tests 35 | TestResult.xml 36 | submit.xml 37 | SolutionVersion.cs 38 | src/SolutionVersion.cs 39 | doc/build/* 40 | *.[lm]df 41 | *.runsettings 42 | 43 | # osx noise 44 | .DS_Store 45 | *.DotSettings 46 | /src/MassTransit.SimpleInjectorIntegration/MassTransit.SimpleInjectorIntegration.csproj.nuspec 47 | *.DS_Store 48 | 49 | docs/.vuepress/dist 50 | _book 51 | /node_modules 52 | .vscode 53 | **/.idea/ 54 | appsettings.Development.json 55 | package-lock.json 56 | 57 | 58 | # Cake 59 | tools/** 60 | !tools/packages.config 61 | 62 | # Artifacts 63 | artifacts/** 64 | artifacts_old/** 65 | /src/.ionide 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MassTransit Lambda Demo 2 | 3 | This repository shows a functioning lambda project. 4 | 5 | ## Requirements 6 | 7 | The amazon lambda tools. Tested with 5.1.2 8 | 9 | ```sh 10 | dotnet tool install -g Amazon.Lambda.Tools 11 | ``` 12 | 13 | ## Instructions 14 | 15 | 1. Load your AWS keys into your environment 16 | 2. `dotnet lambda deploy-function -pl src/amzn` 17 | 18 | 19 | ## Valid Message Body 20 | 21 | ```json 22 | { 23 | "Records": [ 24 | { 25 | "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78", 26 | "receiptHandle": "MessageReceiptHandle", 27 | "body": "{}", 28 | "attributes": { 29 | "ApproximateReceiveCount": "1", 30 | "SentTimestamp": "1523232000000", 31 | "SenderId": "123456789012", 32 | "ApproximateFirstReceiveTimestamp": "1523232000001" 33 | }, 34 | "messageAttributes": {}, 35 | "md5OfBody": "{{{md5_of_body}}}", 36 | "eventSource": "aws:sqs", 37 | "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue", 38 | "awsRegion": "us-east-1" 39 | } 40 | ] 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /src/Sample.LambdaFunction/Function.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Amazon.Lambda.Core; 7 | using Amazon.Lambda.SQSEvents; 8 | using MassTransit; 9 | using MassTransit.Transports; 10 | using Microsoft.Extensions.DependencyInjection; 11 | 12 | // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. 13 | [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] 14 | 15 | namespace Sample.LambdaFunction 16 | { 17 | public class Function 18 | { 19 | private ServiceProvider _provider; 20 | 21 | // Build up the Host once 22 | public Function() 23 | { 24 | var services = new ServiceCollection(); 25 | services.AddMassTransit(x => 26 | { 27 | x.UsingInMemory(); 28 | 29 | x.AddConsumer(); 30 | 31 | x.AddConfigureEndpointsCallback((_, cfg) => 32 | { 33 | cfg.UseRawJsonSerializer(); 34 | }); 35 | }); 36 | _provider = services.BuildServiceProvider(true); 37 | 38 | } 39 | 40 | /// 41 | /// A simple function that takes a string and does a ToUpper 42 | /// 43 | /// 44 | /// 45 | /// 46 | public async Task FunctionHandler(SQSEvent input, ILambdaContext context) 47 | { 48 | using var cts = new CancellationTokenSource(context.RemainingTime); 49 | using var scope = _provider.CreateScope(); 50 | 51 | var ep = scope.ServiceProvider.GetRequiredService>(); 52 | 53 | var headers = new Dictionary(); 54 | 55 | // foreach (var key in context!.ClientContext!.Environment!.Keys) 56 | // headers[key] = context.ClientContext.Environment[key]; 57 | // 58 | // foreach(var key in context!.ClientContext!.Custom!.Keys) 59 | // headers[key] = context.ClientContext.Custom[key]; 60 | 61 | foreach (var record in input.Records) 62 | { 63 | foreach (var key in record!.Attributes!.Keys) 64 | headers[key] = record.Attributes[key]; 65 | 66 | foreach (var key in record!.MessageAttributes!.Keys) 67 | headers[key] = record.MessageAttributes[key]; 68 | 69 | var body = Encoding.UTF8.GetBytes(record.Body); 70 | 71 | await ep.Dispatch(body, headers, cts.Token); 72 | } 73 | } 74 | } 75 | 76 | public class SubmitOrderConsumer : IConsumer 77 | { 78 | public Task Consume(ConsumeContext context) 79 | { 80 | Console.WriteLine("YUS"); 81 | // TODO: write something to S3 82 | return Task.CompletedTask; 83 | } 84 | } 85 | 86 | public class SubmitOrder 87 | { 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/Sample.LambdaFunction/Readme.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Empty Function Project 2 | 3 | This starter project consists of: 4 | * Function.cs - class file containing a class with a single function handler method 5 | * aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS 6 | 7 | You may also have a test project depending on the options selected. 8 | 9 | The generated function handler is a simple method accepting a string argument that returns the uppercase equivalent of the input string. Replace the body of this method, and parameters, to suit your needs. 10 | 11 | ## Here are some steps to follow from Visual Studio: 12 | 13 | To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*. 14 | 15 | To view your deployed function open its Function View window by double-clicking the function name shown beneath the AWS Lambda node in the AWS Explorer tree. 16 | 17 | To perform testing against your deployed function use the Test Invoke tab in the opened Function View window. 18 | 19 | To configure event sources for your deployed function, for example to have your function invoked when an object is created in an Amazon S3 bucket, use the Event Sources tab in the opened Function View window. 20 | 21 | To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window. 22 | 23 | To view execution logs of invocations of your function use the Logs tab in the opened Function View window. 24 | 25 | ## Here are some steps to follow to get started from the command line: 26 | 27 | Once you have edited your template and code you can deploy your application using the [Amazon.Lambda.Tools Global Tool](https://github.com/aws/aws-extensions-for-dotnet-cli#aws-lambda-amazonlambdatools) from the command line. 28 | 29 | Install Amazon.Lambda.Tools Global Tools if not already installed. 30 | ``` 31 | dotnet tool install -g Amazon.Lambda.Tools 32 | ``` 33 | 34 | If already installed check if new version is available. 35 | ``` 36 | dotnet tool update -g Amazon.Lambda.Tools 37 | ``` 38 | 39 | Execute unit tests 40 | ``` 41 | cd "BlueprintBaseName/test/BlueprintBaseName.Tests" 42 | dotnet test 43 | ``` 44 | 45 | Deploy function to AWS Lambda 46 | ``` 47 | cd "BlueprintBaseName/src/BlueprintBaseName" 48 | dotnet lambda deploy-function 49 | ``` 50 | -------------------------------------------------------------------------------- /src/Sample.LambdaFunction/Sample.LambdaFunction.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net6.0 4 | true 5 | Lambda 6 | 7 | true 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/Sample.LambdaFunction/aws-lambda-tools-defaults.json: -------------------------------------------------------------------------------- 1 | { 2 | "Information": [ 3 | "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", 4 | "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", 5 | "dotnet lambda help", 6 | "All the command line options for the Lambda command can be specified in this file." 7 | ], 8 | "profile": "default", 9 | "region": "us-east-1", 10 | "configuration": "Release", 11 | "framework": "netcoreapp3.1", 12 | "function-runtime": "dotnetcore3.1", 13 | "function-memory-size": 256, 14 | "function-timeout": 30, 15 | "function-handler": "Sample.LambdaFunction::Sample.LambdaFunction.Function::FunctionHandler" 16 | } 17 | --------------------------------------------------------------------------------