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

Improvements to Lambda Development Experience #223

Open
1 of 2 tasks
aripalo opened this issue Aug 8, 2020 · 3 comments
Open
1 of 2 tasks

Improvements to Lambda Development Experience #223

aripalo opened this issue Aug 8, 2020 · 3 comments
Labels

Comments

@aripalo
Copy link

aripalo commented Aug 8, 2020

Now that AWS CDK can build Lambda functions within Docker containers by allowing to define BundlingOptions, it would be cool to have also the possibility to locally test, run arbitrary commands and locally invoke the Lambda function inside Docker as well.

Use Case

Currently one has to come up with a separate toolchain to handle the local testing and locally invoking the Lambda function within Docker. I feel that these are critical steps in developing Lambda functions. Also having the possibility to run arbitrary commands within the build Docker container could be useful at times.

Proposed Solution

Currently one can define the build options like this:

new lambda.Function(this, "MyFunc", {
  runtime: lambda.Runtime.GO_1_X,
  handler: "bin/main",
  code: lambda.Code.fromAsset("path/to/my-func", {
    bundling: {
      // Speficy Docker image:
      image: lambda.Runtime.GO_1_X.bundlingDockerImage,
      // The build command to be executed within the Docker container:
      command: [
        'bash', '-c', [
          `cd /asset-input`,
          `go build -o bin/main`,
          `cp /asset-input/bin/main /asset-output/bin`,
        ].join(' && ')
      ],
      user: 'root',
    },
    // Glob(s) to exclude from deployment package:
    // In most cases with Go, only the binary is needed (unless you depend on external files)
    exclude: ["!bin"],
  }),
});

It would be awesome if one could also specify the following (or something like it):

// ⚠️ IMPORTANT NOTE ⚠️
// This is NOT a working code example!
// Just something I imagined and think would be cool to have in CDK:
new lambda.Function(this, "MyFunc", {
  runtime: lambda.Runtime.GO_1_X,
  handler: "bin/main",
  code: lambda.Code.fromAsset("path/to/my-func", {
    // Removed the "building" config shown above for brevity
    testing: {
      // Speficy Docker image:
      image: lambda.Runtime.GO_1_X.bundlingDockerImage, // Could probably use the same from "building" if none specified? Maybe also support providing own Dockerfile?
      // The test command to be executed within the Docker container:
      command: [
        'bash', '-c', [
          `cd /asset-input`,
          `go test`,
        ].join(' && ')
      ],
      user: 'root',
    },
  }),
});

And then I could also imagine a CLI commands to be available after synth something like:

  • cdk lambda test to run tests for all functions
  • cdk lambda test MyFunc to run tests for specific function
  • cdk lambda invoke MyFunc --event some-payload.json to build and then locally invoke the function code

Of course the above example assumes all CDK construct ID's regarding Lambda functions would be unique, which may not be the case. Then maybe one could use the Construct "path": SomeConstruct/MyFunc and AnotherConstruct/MyFunc.

Reasoning

Since CDK already has a lot functionality regarding running things inside the Docker container, it feels "unnecessary" to define totally separate toolchain for the testing/invocation; Having those essential development flows available via CDK would make CDK super powerful tool for building serverless projects.

  • 👋 I may be able to implement this feature request (really strong emphasis on the word "may" 😅)
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@aripalo
Copy link
Author

aripalo commented Aug 10, 2020

Here's also a link to blog post that also reasons about having this kind of feature in CDK directly: https://aripalo.com/blog/2020/building-lambda-functions-inside-docker-containers-with-cdk/

@nija-at
Copy link
Contributor

nija-at commented Aug 10, 2020

Thanks for filing this issue @aripalo. This is a very interesting idea, indeed. Building bett
Currently, the AWS CDK is responsible for preparing assets and artifacts for deployment into AWS. However, we've been toying around with ideas to build a better developer experience for our customers when working on the CDK.

To implement your proposal or something similar, we'll need to think a little deeper into the customer experience of the CDK and what changes will be necessary across our toolchain (such as the CLI as you've noted in your blog post).

I'm transferring this issue to our RFC repo, where we track changes and proposals that need deeper consideration.

The RFC repo has a detailed entry on its process and I'm happy to guide you along the way, if you're interested in writing one.

@nija-at nija-at transferred this issue from aws/aws-cdk Aug 10, 2020
@nija-at nija-at changed the title [aws-lambda] Support testing, arbitrary commands and local invocation within Docker Better experience when developing lambda functions in the cdk Aug 10, 2020
@nija-at nija-at removed their assignment Aug 10, 2020
@aripalo
Copy link
Author

aripalo commented Aug 10, 2020

@nija-at Great to hear! This is definitely something that should not be implemented without thoughtful consideration first on how it should actually work; I just wanted to at least through the idea out there! I'll get in touch, if I'll hopefully get a change to think about this more soon and if I'd be able to contribute on the RFC more!

@eladb eladb changed the title Better experience when developing lambda functions in the cdk Improvements to Lambda Development Experience Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants