Skip to content

Instantly share code, notes, and snippets.

@CalvinAllen
Last active May 14, 2020 15:19
Show Gist options
  • Save CalvinAllen/1e88b7430ff34d46306cc489e3eaac46 to your computer and use it in GitHub Desktop.
Save CalvinAllen/1e88b7430ff34d46306cc489e3eaac46 to your computer and use it in GitHub Desktop.
{
"Information" : [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile" : "<snip>",
"region" : "us-east-2",
"configuration" : "Release",
"framework" : "netcoreapp3.1",
"function-runtime" : "dotnetcore3.1",
"function-memory-size" : 512,
"function-timeout" : 15,
"function-handler" : "bootstrap::AWSLambda1.Function::FunctionHandler",
"msbuild-parameters" : "--self-contained true",
"function-name" : "Calvins-Test-Cron-Lambda",
"function-description" : "",
"function-role" : "<snip>",
"tracing-mode" : "PassThrough",
"environment-variables" : ""
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<AWSProjectType>Lambda</AWSProjectType>
<AssemblyName>bootstrap</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.CloudWatchLogsEvents" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.1.1" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.0.0" />
</ItemGroup>
</Project>
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using System;
using System.Threading.Tasks;
using Amazon.Lambda.CloudWatchLogsEvents;
namespace AWSLambda1
{
public class Function
{
/// <summary>
/// The main entry point for the custom runtime.
/// </summary>
/// <param name="args"></param>
private static async Task Main(string[] args)
{
Func<CloudWatchLogsEvent, ILambdaContext, string> func = FunctionHandler;
using(var handlerWrapper = HandlerWrapper.GetHandlerWrapper(func, new DefaultLambdaJsonSerializer()))
using(var bootstrap = new LambdaBootstrap(handlerWrapper))
{
await bootstrap.RunAsync();
}
}
/// <summary>
/// A simple function that takes a string and does a ToUpper
///
/// To use this handler to respond to an AWS event, reference the appropriate package from
/// https://github.com/aws/aws-lambda-dotnet#events
/// and change the string input parameter to the desired event type.
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public static string FunctionHandler(CloudWatchLogsEvent input, ILambdaContext context)
{
return input?.Awslogs?.DecodeData();
}
}
}
{
"errorType": "LambdaException",
"errorMessage": "Unable to load type 'AWSLambda1.Function' from assembly 'bootstrap'."
}
START RequestId: 5f974297-4f05-432e-a002-d3fba58ea269 Version: $LATEST
Unable to load type 'AWSLambda1.Function' from assembly 'bootstrap'.: LambdaException
14 May 2020 14:33:58,989 [WARN] ([email protected]:297 errno: Address family not supported by protocol) run_dotnet(dotnet_path, &args) failed
END RequestId: 5f974297-4f05-432e-a002-d3fba58ea269
REPORT RequestId: 5f974297-4f05-432e-a002-d3fba58ea269 Duration: 931.38 ms Billed Duration: 1000 ms Memory Size: 512 MB Max Memory Used: 43 MB
Unknown application error occurred
@BradKnowles
Copy link

You're just writing a function right?

@CalvinAllen
Copy link
Author

Yeah, just a dirt simple function that will respond to a cron (CloudWatchLogsEvent). I can't get that part working, let alone what I will actually want the function to do in response to it.

@BradKnowles
Copy link

Does your function need to know any details of how it was triggered? Are you hard coding parameters in the CloudWatchEvent?

@CalvinAllen
Copy link
Author

No, I don't care about any params, just need it to get triggered.

@BradKnowles
Copy link

BradKnowles commented May 14, 2020

This is our project file. We used the template from the AWS Toolkit for Visual Studio. We are not using a custom runtime, since AWS now supports 3.1 natively.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <AWSProjectType>Lambda</AWSProjectType>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="1.0.0" />
  </ItemGroup>
</Project>

@BradKnowles
Copy link

Here is our trimmed down function. You may not need all the usings, I didn't delete what we had in place for our actual work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

using Amazon.Lambda.Core;



// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]

namespace BradTest
{
    public class Function
    {
            
        public async Task<string> Handler(ILambdaContext context)
        {
			// do stuff here
        }

    }
}

@BradKnowles
Copy link

Did you connect the event to the Lambda in the console or with CloudFormation?

image

@CalvinAllen
Copy link
Author

Did you connect the event to the Lambda in the console or with CloudFormation?

image

Did this one in the console

@CalvinAllen
Copy link
Author

Here is our trimmed down function. You may not need all the usings, I didn't delete what we had in place for our actual work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

using Amazon.Lambda.Core;



// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]

namespace BradTest
{
    public class Function
    {
            
        public async Task<string> Handler(ILambdaContext context)
        {
			// do stuff here
        }

    }
}

The code that was generated from the blueprint in the VS extension looks NOTHING like this...

@BradKnowles
Copy link

Which blueprint did you use? I don't even have one for CloudWatch so we may have chosen Empty.

image

@CalvinAllen
Copy link
Author

I think it was Custom Runtime Function - which is probably the problem. It had comments in it indicating what to change to work against CloudWatchLogsEvent triggers, which I thought it was I needed.

Looking at what is generated for "Empty", its much closer to what you have.

I switched the configuration/settings/code over to match yours, and got it working!

@BradKnowles
Copy link

Excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment