-
Notifications
You must be signed in to change notification settings - Fork 153
Docs: CDK example improvement with best practices #520
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
Comments
Thanks for this feedback and proposal, indeed we can do better here. We will look into it |
@niko-achilles Let me make sure that I understand correctly. You suggest to separate files that contain Lambda code into into a separated folder. The example is for
Is that correct? ps. I will exclude bundle splitting or other optimization out of scope as it depends on the tool. In the future, we may add an example for other tools like SAM, Terraform, cdktf, or Serverless Framework. But the main purpose of example is just to illustrate how to integrate Powertools with the framework. |
To offer some additional context around the topic, the project structure used in these examples is in line with the reference project architecture suggested in the CDK v2 docs of the .
├── lib
│ ├── my-construct.api.ts # Lambda handler for API
│ ├── my-construct.auth.ts # Lambda handler for Auth
│ └── my-construct.ts # CDK construct with two Lambda functions
├── package-lock.json # single lock file
├── package.json # CDK and runtime dependencies defined in a single package.json
└── tsconfig.json Example copied example from page linked above Breaking out each function in its own folder is suggested in those cases when functions don't share dependencies or when they are more complex than one single file. In this case the recommended project structure becomes:
Example copied example from page linked above For the examples in this repo we have opted for the flat structure because all functions share most of the dependencies are are contained in a single file. Additionally, keeping this structure has allowed us to keep the CDK structure short since we are using the construct's defaults: const fn = new NodejsFunction(this, functionName, {
functionName: 'MyFunction',
tracing: Tracing.ACTIVE,
}); For those who don't want to use this folder structure the new lambda.NodejsFunction(this, 'MyFunction', {
entry: '/path/to/my/file.ts', // accepts .js, .jsx, .ts, .tsx and .mjs files
handler: 'myExportedFunc', // defaults to 'handler'
}); Notably, the With that said I'm open with clarifying the project structure choices in the README file of the CDK examples folder. |
@ijemmy exactly. As you wrote above:
But why use the word The cdk project should reference the For transpilation of application code , use a simple method. Is application code that uses I think the node14 is a good candidate: link Is application code that uses |
A structure example in shake of simplicity.
|
Could you please elaborate a bit more on what would be the benefit of this change and how it would benefit you and other customers using Powertools (& these examples)? I'm not sure I'm seeing the whole picture. |
the reasons of this issue / wish / improvement is described on my text when I opened this issue. |
Because the official tutorial uses that word. And I don't know if there is a global standard on naming best practices yet. |
This is clear to me now. Thanks! |
Let me summarise this so we have actionable items Reason for this change
Reason of the current code structure
Given that the maintainers need to focus on getting the tools production ready as soon as possible. This will be addressed after production release (Unless anyone wants to contribute!, please let me know). Let's tackle this with issue #385 so that the new structure we decide on is future-proof for other tools (as the way they build application code are different). |
One more thing, I prefer that we don't keep transpiled JS files in Git repo. |
the transpiled files not. |
I agree with what @ijemmy said here. Thanks @niko-achilles for opening this issue. |
i also can derive from my code base, some examples that use powertools with javascript (with JsDoc magic) and typescript. When you decide on the structure i can contribute , ping me in case you need some support , respecting the time to release a production ready lib of powertools. // cc. @ijemmy , @dreamorosi @saragerion |
Looking at this issue , i have pushed in my repository an example of Lambda written in Javascript with Typescript support in VSCode. The lambda function uses the tracer and commons module (see side note) . Given my motivation for this issue was to learn from examples of lambda functions and decide which tool to use , e.g SAM or CDK or terraform , will the structure of examples in the repository of powertools change, so that i can leverage the ability to choose tools for aws infrastructure ? side note: Link Intellisense support of my example: https://github.com/niko-achilles/lambda-powertools-js-example/blob/main/app/src/types/%40aws-lambda-powertools/commons/index.d.ts // cc. @dreamorosi |
@dreamorosi have updated my example with points of interest. Given my motivation for this issue was to learn from examples of lambda functions and decide which tool to use , e.g SAM or CDK or terraform , will the structure of examples in the repository of powertools change, so that i can leverage the ability to choose tools for aws infrastructure ? As in the example provided in my personal repo i have a project structure which is different than powertools. I mean the current project structure of examples in powertools can be improved. In case aware of improvement this Issue can be closed . @ijemmy @flochaz @saragerion |
Hi @niko-achilles, thanks for sharing your sample repo and sorry for not getting back to you on the previous comment. Updating the contents of the examples folder is still on our backlog, and while I'm not yet sure about how it will look like exactly, we are going to separate the infrastructure code from the functions one - similar to your example. At the moment we have another contributor updating the SAM examples (#1140) and the functions code. Once that activity is complete we'll do the same with the CDK ones and re-use the same code in both examples. I'd like to keep this issue open as a reminder of updating the CDK examples. |
|
Description of the improvement
Summary of the proposal
Provide examples that are structured in such a way that
the application code (lambda handlers with aws power tools utilities)
is separated from the stack deployment code.
Developers in order to organize their dependencies
tend to compile application code with utilities as a process in the pipeline
that executes on an other timepoint than the infrastructure relative code.
What is given ?
Given examples for cdk demonstrate the usage of
AWS Lambda Powertools in TypeScript as utilities for Lambda functions
and the examples guide the deployment of these Functions as part of cdk.
But the Functions rely on cdk for compilation.
Relative code in examples , as part of the stack code:
The application code on the other hand:
How, where did you look for information
Missing or unclear documentation
The examples make use of un-compiled Lambda functions , in pure typescript
.ts
files .and the examples use the
NodejsFunction
from libraryaws-cdk-lib/aws-lambda-nodejs
.The missing information is how to use the AWS Lambda Powertools
as utilities for already
compiled/transpiled
Lambda function in.js
files.Improvement
use/choose from already created Lambda functions from the cdk examples
which use the Lambda power tools utilities,
create src directories for each of the chosen lambda functions with the typescript application code,
compile application code and include in
dist
directories.In addition, the application code, with src, dist directories that contain the application code
lambda handlers with imported
aws power tools utilities
,should not be in the same directory as the cdk stack code.
This can / should be a good practice variant.
From a working project, I had a use case where my pipeline architecture
is constructed in flexible way with stages
and came to conclusion that the application code is kept separate from my infrastructure code.
Related existing documentation
yes , actually there is related notice that
I found on
terraform with cdk
websiteLook for notice
by following this linkThe good part in those examples above is
that both kind of good practices are provided.
One with precompiled typescript functions and
the other one with pure typescript.
The naming convention
fully integrated
for the examples with pure typescript is given.Hence the deployment stack takes the responsibility not only to deploy the application code , but also compile / transpile it.
Link compiled application code , separate from code infrastructure:
Link uncompiled application code,
fully integrated
with infrastructure codeRelated issues, RFCs
Side note:
From the web development practices developers compile in separate and in parallel
utility specific code/dependencies .
This is called in web development
bundle splitting
.Can have positive outcomes like performance in web,
but in case of power tools and lambda i am not sure how those practices should be materialized or
apply.
I use case can be applicable with lambda layers,
where the layer is compiled separately and
contains the utilities that can be used by application code.
The text was updated successfully, but these errors were encountered: