Skip to content

Commit

Permalink
Merge pull request #117 from litmus/amazon-linux-docker
Browse files Browse the repository at this point in the history
Add Github Action to validate nuget package runs on Lambda
  • Loading branch information
brianfeucht committed Nov 21, 2023
2 parents b6ae7fe + 1b37457 commit b8fccfd
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/sample-app-docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Validate Nuget Package Docker Build
on: [pull_request]

jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.x
- run: dotnet tool install -g Amazon.Lambda.Tools
- name: Build and package nuget
uses: cake-build/cake-action@v1
with:
cake-version: 1.3.0
- name: Build and package lambda
working-directory: ./sample/SampleLambda
run: dotnet lambda package SampleLambda --profile default --region us-west-1
- name: Run docker
run: docker run --detach -p 9000:8080 samplelambda:latest
- name: Test function
run: curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
1 change: 1 addition & 0 deletions build.cake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#addin nuget:?package=SharpZipLib&version=1.3.1
#addin nuget:?package=Cake.Compression&version=0.2.6
#addin nuget:?package=Newtonsoft.Json&version=13.0.3

using System.IO;
using Newtonsoft.Json;
Expand Down
11 changes: 11 additions & 0 deletions sample/SampleLambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# You can also pull these images from DockerHub amazon/aws-lambda-dotnet:7
FROM public.ecr.aws/lambda/dotnet:7

# Set the image's internal work directory
WORKDIR /var/task

# Copy function code
COPY "bin/Release/lambda-publish" .

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "SampleLambda::SampleLambda.HelloWorldHandler::Handle" ]
25 changes: 25 additions & 0 deletions sample/SampleLambda/HelloWorldHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Amazon.Lambda.Core;
using HeadlessChromium.Puppeteer.Lambda.Dotnet;
using Microsoft.Extensions.Logging;

namespace SampleLambda
{
public class HelloWorldHandler
{
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public async Task<byte[]> Handle(ILambdaContext context)
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var browserLauncher = new HeadlessChromiumPuppeteerLauncher(loggerFactory);

await using (var browser = await browserLauncher.LaunchAsync())
await using (var page = await browser.NewPageAsync())
{
await page.GoToAsync("https://www.google.com");
await page.ScreenshotAsync("./google.png");
}

return await File.ReadAllBytesAsync("./google.png");
}
}
}
27 changes: 27 additions & 0 deletions sample/SampleLambda/SampleLambda.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="aws-lambda-tools-defaults.json" />
</ItemGroup>

<ItemGroup>
<Content Include="aws-lambda-tools-defaults.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.1" />
<PackageReference Include="HeadlessChromium.Puppeteer.Lambda.Dotnet" Version="1.1.0-dev" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="PuppeteerSharp" Version="11.0.2" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions sample/SampleLambda/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"configuration": "Release",
"framework": "net7.0",
"function-runtime": "dotnet7",
"function-memory-size": 1536,
"function-timeout": 60,
"function-handler": "SampleLambda::SampleLambda.HelloWorldHandler::Handle",
"package-type": "image",
"docker-host-build-output-dir": "./bin/Release/lambda-publish"
}
6 changes: 6 additions & 0 deletions sample/SampleLambda/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyLocalSharedSource" value="..\..\artifacts\" />
</packageSources>
</configuration>

0 comments on commit b8fccfd

Please sign in to comment.