forked from lambci/docker-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
481 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")}"; | ||
} | ||
} |
Oops, something went wrong.