Skip to content

Commit 42e540b

Browse files
jeastham1993jamesuk
and
jamesuk
authored
Add ASP.NET examples (awslabs#284)
* Add ASP.NET examples * Update base README to add ASP.NET * Add ASP.NET to README * Fix 404 errors and update README/SAM templates * Add example for retrieving header and lambda request context --------- Co-authored-by: jamesuk <[email protected]>
1 parent 80f585b commit 42e540b

File tree

170 files changed

+149334
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+149334
-2
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ samconfig.toml
44
/.idea/
55
.aws-sam
66

7-
.vscode
7+
.vscode
8+
9+
**/obj/*
10+
**/bin/*
11+
**/.idea/*
12+

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A tool to run web applications on AWS Lambda
44

5-
AWS Lambda Web Adapter allows developers to build web apps (http api) with familiar frameworks (e.g. Express.js, Next.js, Flask, SpringBoot, and Laravel, anything speaks HTTP 1.1/1.0) and run it on AWS Lambda.
5+
AWS Lambda Web Adapter allows developers to build web apps (http api) with familiar frameworks (e.g. Express.js, Next.js, Flask, SpringBoot, ASP.NET and Laravel, anything speaks HTTP 1.1/1.0) and run it on AWS Lambda.
66
The same docker image can run on AWS Lambda, Amazon EC2, AWS Fargate, and local computers.
77

88
![Lambda Web Adapter](docs/images/lambda-adapter-overview.png)
@@ -189,6 +189,9 @@ Please note that `sam local` starts a Lambda Runtime Interface Emulator on port
189189
- [Golang Gin in Zip](examples/gin-zip)
190190
- [Deno Oak in Zip](examples/deno-zip)
191191
- [Laravel on Lambda](https://github.com/aws-samples/lambda-laravel)
192+
- [ASP.NET MVC](examples/aspnet-mvc)
193+
- [ASP.NET MVC in Zip](examples/aspnet-mvc-zip)
194+
- [ASP.NET Web API in Zip](examples/aspnet-webapi-zip)
192195

193196
## Acknowledgement
194197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetLambdaZipWebAdapter", "src\AspNetLambdaZipWebAdapter.csproj", "{7484CB19-34BE-4ED2-BD13-9A61F8C5DB18}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(SolutionProperties) = preSolution
14+
HideSolutionNode = FALSE
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{7484CB19-34BE-4ED2-BD13-9A61F8C5DB18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{7484CB19-34BE-4ED2-BD13-9A61F8C5DB18}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{7484CB19-34BE-4ED2-BD13-9A61F8C5DB18}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{7484CB19-34BE-4ED2-BD13-9A61F8C5DB18}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal

examples/aspnet-mvc-zip/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# ASP.NET ZIP
2+
3+
This example shows how to deploy an ASP.NET application using the Lambda Web Adapter packaged as a ZIP file.
4+
5+
The `dotnet6` runtime is used.
6+
7+
```yaml
8+
Resources:
9+
AspNetOnLambdaWebAdapterFunction:
10+
Type: AWS::Serverless::Function
11+
Properties:
12+
CodeUri: src/
13+
Handler: AspNetLambdaZipWebAdapter
14+
MemorySize: 1024
15+
Environment:
16+
Variables:
17+
AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap
18+
RUST_LOG: info
19+
Layers:
20+
- !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerX86:17
21+
Events:
22+
Api:
23+
Type: HttpApi
24+
Properties:
25+
Path: /{proxy+}
26+
Method: ANY
27+
```
28+
29+
## Build & Deploy
30+
31+
Make sure .NET 6 is already installed. Run the following commands on a x86_64 machine.
32+
33+
```shell
34+
sam build --use-container
35+
sam deploy -g
36+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Content Include="..\README.md">
11+
<Link>README.md</Link>
12+
</Content>
13+
<Content Include="..\template.yaml">
14+
<Link>template.yaml</Link>
15+
</Content>
16+
</ItemGroup>
17+
18+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace AspNetLambdaZipWebAdapter.Controllers;
2+
3+
using System.Diagnostics;
4+
5+
using AspNetLambdaZipWebAdapter.Models;
6+
7+
using Microsoft.AspNetCore.Mvc;
8+
9+
public class HomeController : Controller
10+
{
11+
private readonly ILogger<HomeController> _logger;
12+
13+
public HomeController(ILogger<HomeController> logger)
14+
{
15+
this._logger = logger;
16+
}
17+
18+
public IActionResult Index()
19+
{
20+
return this.View();
21+
}
22+
23+
public IActionResult Privacy()
24+
{
25+
return this.View();
26+
}
27+
28+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
29+
public IActionResult Error()
30+
{
31+
return this.View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? this.HttpContext.TraceIdentifier });
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AspNetLambdaZipWebAdapter.Models;
2+
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(this.RequestId);
8+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddControllersWithViews();
5+
6+
var app = builder.Build();
7+
8+
// Configure the HTTP request pipeline.
9+
if (!app.Environment.IsDevelopment())
10+
{
11+
app.UseExceptionHandler("/Home/Error");
12+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
13+
app.UseHsts();
14+
}
15+
16+
app.UseHttpsRedirection();
17+
app.UseStaticFiles();
18+
19+
app.UseRouting();
20+
21+
app.UseAuthorization();
22+
23+
app.MapControllerRoute(
24+
name: "default",
25+
pattern: "{controller=Home}/{action=Index}/{id?}");
26+
27+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:40601",
8+
"sslPort": 44387
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5043",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7076;http://localhost:5043",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model AspNetLambdaZipWebAdapter.Models.ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - AspNetLambdaZipWebAdapter</title>
7+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
9+
<link rel="stylesheet" href="~/AspNetLambdaZipWebAdapter.styles.css" asp-append-version="true" />
10+
</head>
11+
<body>
12+
<header>
13+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
14+
<div class="container-fluid">
15+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">AspNetLambdaZipWebAdapter</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
17+
aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
21+
<ul class="navbar-nav flex-grow-1">
22+
<li class="nav-item">
23+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
24+
</li>
25+
<li class="nav-item">
26+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
27+
</li>
28+
</ul>
29+
</div>
30+
</div>
31+
</nav>
32+
</header>
33+
<div class="container">
34+
<main role="main" class="pb-3">
35+
@RenderBody()
36+
</main>
37+
</div>
38+
39+
<footer class="border-top footer text-muted">
40+
<div class="container">
41+
&copy; 2023 - AspNetLambdaZipWebAdapter - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
42+
</div>
43+
</footer>
44+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
45+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
46+
<script src="~/js/site.js" asp-append-version="true"></script>
47+
@await RenderSectionAsync("Scripts", required: false)
48+
</body>
49+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Please see documentation at https://learn.microsoft.com/aspnet/core/client-side/bundling-and-minification
2+
for details on configuring this project to bundle and minify static web assets. */
3+
4+
a.navbar-brand {
5+
white-space: normal;
6+
text-align: center;
7+
word-break: break-all;
8+
}
9+
10+
a {
11+
color: #0077cc;
12+
}
13+
14+
.btn-primary {
15+
color: #fff;
16+
background-color: #1b6ec2;
17+
border-color: #1861ac;
18+
}
19+
20+
.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
21+
color: #fff;
22+
background-color: #1b6ec2;
23+
border-color: #1861ac;
24+
}
25+
26+
.border-top {
27+
border-top: 1px solid #e5e5e5;
28+
}
29+
.border-bottom {
30+
border-bottom: 1px solid #e5e5e5;
31+
}
32+
33+
.box-shadow {
34+
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
35+
}
36+
37+
button.accept-policy {
38+
font-size: 1rem;
39+
line-height: inherit;
40+
}
41+
42+
.footer {
43+
position: absolute;
44+
bottom: 0;
45+
width: 100%;
46+
white-space: nowrap;
47+
line-height: 60px;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
2+
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@using AspNetLambdaZipWebAdapter
2+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*",
9+
"Kestrel": {
10+
"Endpoints": {
11+
"Http": {
12+
"Url": "http://*:8080"
13+
}
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)