Skip to content

Instantly share code, notes, and snippets.

@scionwest
Last active March 17, 2022 22:23
Show Gist options
  • Save scionwest/36e6a3ea90b6a349a1f9da1a0e99cc73 to your computer and use it in GitHub Desktop.
Save scionwest/36e6a3ea90b6a349a1f9da1a0e99cc73 to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PublishReadyToRun>true</PublishReadyToRun>
<TrimMode>true</TrimMode>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.AspNetCoreServer.Hosting" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.2.0" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.0.28" />
</ItemGroup>
</Project>
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Amazon.Lambda.Core;
using System;
using Amazon.DynamoDBv2;
using Amazon;
using Amazon.DynamoDBv2.Model;
using System.Collections.Generic;
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
var builder = WebApplication.CreateBuilder(args);
string region = Environment.GetEnvironmentVariable("AWS_REGION") ?? RegionEndpoint.USWest1.SystemName;
string dynamoTable = Environment.GetEnvironmentVariable("SAMPLE_TABLE");
builder.Services.AddSingleton<IAmazonDynamoDB>(new AmazonDynamoDBClient(RegionEndpoint.GetBySystemName(region)) );
builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);
var app = builder.Build();
app.MapGet("/", () => new List<Guid> {
Guid.Parse("410a546f-6cb5-4dbd-a334-44507a37a015"),
Guid.Parse("9f8df297-1998-49f1-823f-de5659d1e3be"),
Guid.Parse("c30b5b1c-cc27-4d1e-9325-9d6d2e40e931"),
Guid.Parse("db08fb5d-dead-45eb-9009-b67eafad16a7"),
Guid.Parse("c2a9f164-3012-4f35-9237-e1602762a8c4")
});
app.MapGet("/{id}", async (IAmazonDynamoDB context, string id) => {
var results = await context.GetItemAsync(
new GetItemRequest
{
TableName = "helloworldBookCatalog",
Key = new Dictionary<string, AttributeValue>() {{ "Id", new AttributeValue { S = id }}},
ProjectionExpression = "Id, ISBN, Title, Authors",
ConsistentRead = false
}
);
return new {
Title = results.Item["Title"].S,
ISBN = results.Item["ISBN"].N,
Id = results.Item["Id"].S,
};
});
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment