Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS dotnet Lambda example #453

Merged
merged 6 commits into from
Nov 8, 2019
Merged

AWS dotnet Lambda example #453

merged 6 commits into from
Nov 8, 2019

Conversation

EvanBoyle
Copy link
Contributor

Simple example that does the toUpper of a string input. This is currently two separate project that require separate build steps. Happy to make updates if someone has knowledge on how to combine them.

Copy link
Member

@mikhailshilkov mikhailshilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, great to see this working! LGTM, left several comments inline.

1. Build and publish the lambda function, making the output available to our Pulumi program.

```bash
cd dotnetLambda/src/dotnetLambda/ && dotnet restore && dotnet build && dotnet publish && cd ../../../
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You don't have to cd back and forth, you can pass the folder as a parameter to dotnet x

1. Build and publish the lambda function, making the output available to our Pulumi program.

```bash
cd dotnetLambda/src/dotnetLambda/ && dotnet restore && dotnet build && dotnet publish && cd ../../../
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call restore and build: publish will do both implicitly

--payload '"foo"' \
output.json

cat output.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cat won't work on Windows, which might be important for a .NET guide. We use curl in many examples though, so I'm not sure if it's a problem and what an alternative should be.

{
public class Function
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove the empty line


class LambdaUtil
{
public static Pulumi.AssetArchive buildArchive(string lambdaArchivePath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method names should be in PascalCase

{
var immutableDictBuilder = System.Collections.Immutable.ImmutableDictionary.CreateBuilder<string, AssetOrArchive>();
immutableDictBuilder.Add(".", new FileArchive(lambdaArchivePath));
return new Pulumi.AssetArchive(immutableDictBuilder.ToImmutable());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulumi. is redundant here

var lambda = new Function("basicLambda", new FunctionArgs
{
Runtime = "dotnetcore2.1",
Code = LambdaUtil.buildArchive("../dotnetLambda/src/dotnetLambda//bin/Debug/netcoreapp2.1/publish"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pass FileArchive directly to Code, so I don't think the buildArchive function is needed at all

}
}

class Program
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put the Program class on top so that people would see it first. Usual C# practice is to have one class per file, but that's not required here. You could also merge two classes into one with two methods.

var lambda = new Function("basicLambda", new FunctionArgs
{
Runtime = "dotnetcore2.1",
Code = LambdaUtil.buildArchive("../dotnetLambda/src/dotnetLambda//bin/Debug/netcoreapp2.1/publish"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Code = LambdaUtil.buildArchive("../dotnetLambda/src/dotnetLambda//bin/Debug/netcoreapp2.1/publish"),
Code = LambdaUtil.buildArchive("../dotnetLambda/src/dotnetLambda/bin/Debug/netcoreapp2.1/publish"),


using Pulumi;
using Pulumi.Aws.Lambda;
using Pulumi.Aws.Iam;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: alphabetize the usings within each group

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

namespace dotnetLambda
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespaces shoudl be PascalCased in C#.

public string FunctionHandler(string input, ILambdaContext context)
{
return input?.ToUpper();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider:

public string FunctionHandler(string input, ILambdaContext context)
    => input?.ToUpper();

Role = LambdaUtil.createLambdaRole().Arn
});

return new Dictionary<string, object>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this all one line, or put the { on the next line. consider also just running an explicit format on all your docs.

{
var immutableDictBuilder = System.Collections.Immutable.ImmutableDictionary.CreateBuilder<string, AssetOrArchive>();
immutableDictBuilder.Add(".", new FileArchive(lambdaArchivePath));
return new Pulumi.AssetArchive(immutableDictBuilder.ToImmutable());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll make htis better so you don't need the ImmutableDict stuff either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct that AssetArchive is not needed at all in this particular case? Just pass FileArchive to Code. Or is there a difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was able to update this as suggested by @mikhailshilkov

@EvanBoyle
Copy link
Contributor Author

Thanks @CyrusNajmabadi and @mikhailshilkov for helping me get this into shape!

@EvanBoyle EvanBoyle merged commit 7c28f4c into master Nov 8, 2019
@pulumi-bot pulumi-bot deleted the evan/aws-cs-lambda branch November 8, 2019 21:49
dixler pushed a commit that referenced this pull request Jan 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants