Skip to content

Commit dc0bf7f

Browse files
authored
AWS Lambda ASP.NET Core Sample (#582)
1 parent 9681ffb commit dc0bf7f

File tree

10 files changed

+141
-0
lines changed

10 files changed

+141
-0
lines changed

Sentry.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.AspNetCore.Grpc.Test
114114
EndProject
115115
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.AspNetCore.Grpc", "samples\Sentry.Samples.AspNetCore.Grpc\Sentry.Samples.AspNetCore.Grpc.csproj", "{21599C29-C3D4-4DAC-A2D6-6C194600478F}"
116116
EndProject
117+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Aws.Lambda.AspNetCoreServer", "samples\Sentry.Samples.Aws.Lambda.AspNetCoreServer\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj", "{274CEDC2-2129-469D-B269-649EFA2EF5E0}"
118+
EndProject
117119
Global
118120
GlobalSection(SolutionConfigurationPlatforms) = preSolution
119121
Debug|Any CPU = Debug|Any CPU
@@ -240,6 +242,10 @@ Global
240242
{21599C29-C3D4-4DAC-A2D6-6C194600478F}.Debug|Any CPU.Build.0 = Debug|Any CPU
241243
{21599C29-C3D4-4DAC-A2D6-6C194600478F}.Release|Any CPU.ActiveCfg = Release|Any CPU
242244
{21599C29-C3D4-4DAC-A2D6-6C194600478F}.Release|Any CPU.Build.0 = Release|Any CPU
245+
{274CEDC2-2129-469D-B269-649EFA2EF5E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
246+
{274CEDC2-2129-469D-B269-649EFA2EF5E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
247+
{274CEDC2-2129-469D-B269-649EFA2EF5E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
248+
{274CEDC2-2129-469D-B269-649EFA2EF5E0}.Release|Any CPU.Build.0 = Release|Any CPU
243249
EndGlobalSection
244250
GlobalSection(SolutionProperties) = preSolution
245251
HideSolutionNode = FALSE
@@ -278,6 +284,7 @@ Global
278284
{720811C8-29C7-4368-86F2-D61DF415AC2A} = {AF6AF4C7-8AA2-4D59-8064-2D79560904EB}
279285
{3818FA5C-8649-427D-8E68-0C44558CA0DD} = {83263231-1A2A-4733-B759-EEFF14E8C5D5}
280286
{21599C29-C3D4-4DAC-A2D6-6C194600478F} = {77454495-55EE-4B40-A089-71B9E8F82E89}
287+
{274CEDC2-2129-469D-B269-649EFA2EF5E0} = {77454495-55EE-4B40-A089-71B9E8F82E89}
281288
EndGlobalSection
282289
GlobalSection(ExtensibilityGlobals) = postSolution
283290
SolutionGuid = {0C652B1A-DF72-4EE5-A98B-194FE2C054F6}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace Sentry.Samples.Aws.Lambda.AspNetCoreServer
4+
{
5+
[Route("api/[controller]")]
6+
public class BadController
7+
{
8+
[HttpGet]
9+
public string Get() => throw null;
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
3+
// The entrypoint used when the function is deployed to AWS.
4+
public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
5+
{
6+
protected override void Init(IWebHostBuilder builder)
7+
{
8+
builder
9+
// Add Sentry (configuration was done via appsettings.json but could be done programatically):
10+
.UseSentry()
11+
.UseStartup<Startup>();
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
// Only used for testing locally: dotnet run
5+
public static class LocalEntryPoint
6+
{
7+
public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
8+
9+
private static IHostBuilder CreateHostBuilder(string[] args) =>
10+
Host.CreateDefaultBuilder(args)
11+
.ConfigureWebHostDefaults(webBuilder =>
12+
{
13+
webBuilder.UseStartup<Startup>()
14+
// Add Sentry (configuration was done via appsettings.json but could be done programatically):
15+
.UseSentry();
16+
});
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Sample AWS Lambda ASP.NET Core Server app
2+
3+
To test it locally:
4+
5+
1. Add your own [DSN](https://docs.sentry.io/product/sentry-basics/dsn-explainer/) to [appsettings.json](appsettings.json).
6+
2. Run the local server: `dotnet run`
7+
* You can also publish to AWS to test it
8+
3. Hit the bad endpoint:
9+
10+
```
11+
$ curl -X GET -I http://localhost:5000/api/bad
12+
13+
HTTP/1.1 500 Internal Server Error
14+
Date: Mon, 09 Nov 2020 23:10:06 GMT
15+
Server: Kestrel
16+
Content-Length: 0
17+
18+
```
19+
20+
An event will now show up in Sentry within a couple of seconds, like below.
21+
22+
Note the event shows only the application frames by default. You can click _Full_ to see the complete stack trace.
23+
There's also a list of logs for that one request. Logs are taken via the ASP.NET Core logging integration.
24+
By default calls to `LogInfo` and higher are stored as breadcrumbs. `LogError` is sent to Sentry as an event.
25+
Events will include `HTTP` headers, and other relevant information helpful to identify the source of the issue.
26+
27+
![Sample event in Sentry](aws-lambda-aspnet-server.png)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
5+
<AWSProjectType>Lambda</AWSProjectType>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
9+
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="5.1.1" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<Folder Include="Controllers" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\Sentry.AspNetCore\Sentry.AspNetCore.csproj" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
public class Startup
7+
{
8+
public Startup(IConfiguration configuration) => Configuration = configuration;
9+
10+
public static IConfiguration Configuration { get; private set; }
11+
12+
public void ConfigureServices(IServiceCollection services) => services.AddControllers();
13+
14+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
15+
{
16+
app.UseRouting();
17+
app.UseEndpoints(e => e.MapControllers());
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information"
5+
}
6+
},
7+
"AWS": {
8+
"Region": ""
9+
},
10+
"Sentry": {
11+
"NOTE1": "Add your own DSN below see the sample event in YOUR Sentry dashboard.",
12+
"Dsn": "https://[email protected]/5428537",
13+
14+
"NOTE2": "Many Sentry ASP.NET Core options are demonstrated in the other samples in this repository.",
15+
"NOTE3": "The most relevant one for serverless is to tell Sentry to flush out events after each lambda request.",
16+
"NOTE4": "This makes sure events are sent to Sentry once requests complete and nothing is lost.",
17+
"FlushOnCompletedRequest": true
18+
}
19+
}
529 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Information": [
3+
],
4+
"profile": "",
5+
"region": "",
6+
"configuration": "Release",
7+
"framework": "netcoreapp3.1",
8+
"s3-prefix": "aws-aspnet/",
9+
"template": "serverless.template",
10+
"template-parameters": "ShouldCreateBucket=true;BucketName="
11+
}

0 commit comments

Comments
 (0)