Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
Add dotnetcore2.1 runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mhart committed Jul 9, 2018
1 parent a4a89a5 commit 8610f88
Show file tree
Hide file tree
Showing 20 changed files with 481 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -13,3 +13,4 @@ go1.x/run/aws-lambda-mock
go1.x/run/vendor
examples/go1.x/handler
examples/dotnetcore2.0/pub
examples/dotnetcore2.1/pub
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -63,6 +63,9 @@ docker run --rm -v "$PWD":/var/task lambci/lambda:java8 org.myorg.MyHandler
# a class named Function with a FunctionHandler method, and a custom event
docker run --rm -v "$PWD":/var/task lambci/lambda:dotnetcore2.0 test::test.Function::FunctionHandler '{"some": "event"}'

# Test on .NET Core 2.1 in the same way
docker run --rm -v "$PWD":/var/task lambci/lambda:dotnetcore2.1 test::test.Function::FunctionHandler '{"some": "event"}'

# Run custom commands
docker run --rm --entrypoint node lambci/lambda:nodejs8.10 -v

Expand Down Expand Up @@ -182,6 +185,7 @@ Docker tags (follow the Lambda runtime names):
- `java8`
- `go1.x`
- `dotnetcore2.0`
- `dotnetcore2.1`
- `build-nodejs4.3`
- `build-nodejs6.10`
- `build-nodejs8.10`
Expand All @@ -190,6 +194,7 @@ Docker tags (follow the Lambda runtime names):
- `build-java8`
- `build-go1.x`
- `build-dotnetcore2.0`
- `build-dotnetcore2.1`

Env vars:
- `AWS_LAMBDA_FUNCTION_NAME`
Expand Down
2 changes: 1 addition & 1 deletion base/diff.sh
@@ -1,6 +1,6 @@
#!/bin/bash

RUNTIMES="nodejs4.3 nodejs6.10 nodejs8.10 python2.7 python3.6 java8 go1.x dotnetcore2.0"
RUNTIMES="nodejs4.3 nodejs6.10 nodejs8.10 python2.7 python3.6 java8 go1.x dotnetcore2.0 dotnetcore2.1"

rm -rf diff
mkdir -p diff
Expand Down
78 changes: 78 additions & 0 deletions base/dump-dotnetcore21/Function.cs
@@ -0,0 +1,78 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

using Amazon.Lambda.Core;
using Amazon.S3;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace dump_dotnetcore21
{
public class Function
{
/// <summary>
/// Lambda function to dump the container directories /var/lang
/// and /var/runtime and upload the resulting archive to S3
/// </summary>
/// <returns></returns>
public async Task<string> FunctionHandler(object invokeEvent, ILambdaContext context)
{
string filename = "dotnetcore2.1.tgz";
string cmd = $"tar -cpzf /tmp/{filename} --numeric-owner --ignore-failed-read /var/runtime /var/lang";

Console.WriteLine($"invokeEvent: {invokeEvent}");
Console.WriteLine($"context.RemainingTime: {context.RemainingTime}");

Console.WriteLine("Parent cmdline:");
Console.WriteLine(File.ReadAllText("/proc/1/cmdline").Replace("\0", " "));

Console.WriteLine("Parent env:");
RunShell("xargs --null --max-args=1 < /proc/1/environ");

Console.WriteLine("This cmdline:");
Console.WriteLine(File.ReadAllText($"/proc/{Process.GetCurrentProcess().Id}/cmdline").Replace("\0", " "));

Console.WriteLine("This env:");
RunShell($"xargs --null --max-args=1 < /proc/{Process.GetCurrentProcess().Id}/environ");

Console.WriteLine($"Current working directory: {Directory.GetCurrentDirectory()}");

RunShell(cmd);

Console.WriteLine("Zipping done! Uploading...");

var s3Client = new AmazonS3Client();
var response = await s3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = "lambci",
Key = $"fs/{filename}",
FilePath = $"/tmp/{filename}",
CannedACL = S3CannedACL.PublicRead,
});

Console.WriteLine("Uploading done!");

return response.HttpStatusCode.ToString();
}

private static Process RunShell(string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/sh",
Arguments = $"-c \"{escapedArgs}\"",
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
process.WaitForExit();
return process;
}
}
}
34 changes: 34 additions & 0 deletions base/dump-dotnetcore21/Readme.md
@@ -0,0 +1,34 @@
# AWS Lambda Dump Runtime Project
This functions dumps the runtime and uploads it to s3.


## Here are some steps to follow from Visual Studio:

To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*.

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.

To perform testing against your deployed function use the Test Invoke tab in the opened Function View window.

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.

To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window.

To view execution logs of invocations of your function use the Logs tab in the opened Function View window.

## Here are some steps to follow to get started from the command line:

Restore dependencies
```shell
dotnet restore
```

Deploy function to AWS Lambda
```shell
dotnet lambda deploy-function [--profile <profile>] [--region <region>] dump-dotnetcore21
```

Invoke function
```shell
dotnet lambda invoke-function [--profile <profile>] [--region <region>] dump-dotnetcore21
```
8 changes: 8 additions & 0 deletions base/dump-dotnetcore21/aws-lambda-tools-defaults.json
@@ -0,0 +1,8 @@
{
"configuration": "Release",
"framework": "netcoreapp2.1",
"function-runtime": "dotnetcore2.1",
"function-memory-size": 1536,
"function-timeout": 300,
"function-handler": "dump_dotnetcore21::dump_dotnetcore21.Function::FunctionHandler"
}
19 changes: 19 additions & 0 deletions base/dump-dotnetcore21/dump-dotnetcore21.csproj
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AssemblyName>dump_dotnetcore21</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.2.0" />
<PackageReference Include="AWSSDK.S3" Version="3.3.18.6" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.2.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion base/dump.sh
@@ -1,6 +1,6 @@
#!/bin/bash

RUNTIMES="node43 node610 node810 python27 python36 java8 go1x dotnetcore20"
RUNTIMES="node43 node610 node810 python27 python36 java8 go1x dotnetcore20 dotnetcore21"

for RUNTIME in $RUNTIMES; do
echo $RUNTIME
Expand Down
20 changes: 20 additions & 0 deletions dotnetcore2.1/build/Dockerfile
@@ -0,0 +1,20 @@
FROM lambci/lambda-base:build

ENV PATH=/var/lang/bin:/usr/local/bin:/usr/bin/:/bin \
LD_LIBRARY_PATH=/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib \
AWS_EXECUTION_ENV=AWS_Lambda_dotnetcore2.1 \
DOTNET_SDK_VERSION=2.1.301 \
DOTNET_CLI_TELEMETRY_OPTOUT=1 \
NUGET_XMLDOC_MODE=skip

RUN rm -rf /var/runtime /var/lang && \
curl https://lambci.s3.amazonaws.com/fs/dotnetcore2.1.tgz | tar -zx -C / && \
yum install -y libunwind && \
curl https://dot.net/v1/dotnet-install.sh | bash -s -- -v $DOTNET_SDK_VERSION -i /var/lang/bin && \
mkdir /tmp/warmup && \
cd /tmp/warmup && \
dotnet new && \
cd / && \
rm -rf /tmp/warmup /tmp/NuGetScratch

CMD ["dotnet", "build"]
24 changes: 24 additions & 0 deletions dotnetcore2.1/run/Dockerfile
@@ -0,0 +1,24 @@
FROM microsoft/dotnet:2.1-sdk
WORKDIR /source

# cache restore result
COPY MockBootstraps/*.csproj .
RUN dotnet restore

# copy the rest of the code
COPY MockBootstraps/ .
RUN dotnet publish --output /app/ --configuration Release


FROM lambci/lambda-base

ENV PATH=/var/lang/bin:/usr/local/bin:/usr/bin/:/bin \
LD_LIBRARY_PATH=/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib \
AWS_EXECUTION_ENV=AWS_Lambda_dotnetcore2.1

RUN rm -rf /var/runtime /var/lang && \
curl https://lambci.s3.amazonaws.com/fs/dotnetcore2.1.tgz | tar -zx -C /

COPY --from=0 /app/MockBootstraps.* /var/runtime/

ENTRYPOINT ["/var/lang/bin/dotnet", "/var/runtime/MockBootstraps.dll"]
17 changes: 17 additions & 0 deletions dotnetcore2.1/run/MockBootstraps/MockBootstraps.csproj
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>MockLambdaRuntime</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Reference Include="Bootstrap">
<HintPath>lib\Bootstrap.dll</HintPath>
</Reference>
<Reference Include="Amazon.Lambda.Core">
<HintPath>lib\Amazon.Lambda.Core.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions dotnetcore2.1/run/MockBootstraps/MockBootstraps.sln
@@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2017
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockBootstraps", "MockBootstraps.csproj", "{8AD9356B-231B-4B42-B168-D06497B769AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8AD9356B-231B-4B42-B168-D06497B769AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8AD9356B-231B-4B42-B168-D06497B769AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8AD9356B-231B-4B42-B168-D06497B769AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8AD9356B-231B-4B42-B168-D06497B769AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
84 changes: 84 additions & 0 deletions dotnetcore2.1/run/MockBootstraps/MockLambdaContext.cs
@@ -0,0 +1,84 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace MockLambdaRuntime
{
public class MockLambdaContext
{
static readonly Random random = new Random();

/// Creates a mock context from a given Lambda handler and event
public MockLambdaContext(string handler, string eventBody)
{
RequestId = Guid.NewGuid().ToString();
StartTime = DateTime.Now;
InputStream = new MemoryStream();
OutputStream = new MemoryStream();

var eventData = Encoding.UTF8.GetBytes(eventBody);
InputStream.Write(eventData, 0, eventData.Length);
InputStream.Position = 0;

Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", FunctionName);
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", FunctionVersion);
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", MemorySize.ToString());
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_GROUP_NAME", LogGroup);
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_STREAM_NAME", LogStream);
Environment.SetEnvironmentVariable("AWS_REGION", Region);
Environment.SetEnvironmentVariable("AWS_DEFAULT_REGION", Region);
Environment.SetEnvironmentVariable("_HANDLER", handler);
}

/// Calculates the remaining time using current time and timeout
public TimeSpan RemainingTime()
{
return StartTime + TimeSpan.FromSeconds(Timeout) - DateTime.Now;
}

public long Duration => (long)(DateTime.Now - StartTime).TotalMilliseconds;
public long BilledDuration => (long)(Math.Ceiling((DateTime.Now - StartTime).TotalMilliseconds / 100)) * 100;

public long MemoryUsed => Process.GetCurrentProcess().WorkingSet64;

public Stream InputStream { get; }

public Stream OutputStream { get; }

public string OutputText
{
get
{
OutputStream.Position = 0;
using (TextReader reader = new StreamReader(OutputStream))
{
return reader.ReadToEnd();
}
}
}

public string RequestId { get; }
public DateTime StartTime { get; }

public int Timeout => Convert.ToInt32(EnvHelper.GetOrDefault("AWS_LAMBDA_FUNCTION_TIMEOUT", "300"));

public int MemorySize => Convert.ToInt32(EnvHelper.GetOrDefault("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "1536"));

public string FunctionName => EnvHelper.GetOrDefault("AWS_LAMBDA_FUNCTION_NAME", "test");

public string FunctionVersion => EnvHelper.GetOrDefault("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST");

public string LogGroup => EnvHelper.GetOrDefault("AWS_LAMBDA_LOG_GROUP_NAME", $"/aws/lambda/{FunctionName}");

public string LogStream => EnvHelper.GetOrDefault("AWS_LAMBDA_LOG_STREAM_NAME", RandomLogStreamName);

public string Region => EnvHelper.GetOrDefault("AWS_REGION", EnvHelper.GetOrDefault("AWS_DEFAULT_REGION", "us-east-1"));

public string AccountId => EnvHelper.GetOrDefault("AWS_ACCOUNT_ID", "000000000000");

public string Arn => EnvHelper.GetOrDefault("AWS_LAMBDA_FUNCTION_INVOKED_ARN", $"arn:aws:lambda:{Region}:{AccountId}:function:{FunctionName}");

string RandomLogStreamName => $"{DateTime.Now.ToString("yyyy/MM/dd")}/[{FunctionVersion}]{random.Next().ToString("x") + random.Next().ToString("x")}";
}
}

0 comments on commit 8610f88

Please sign in to comment.