Skip to content

Docs: CDK example improvement with best practices #520

Closed
@niko-achilles

Description

@niko-achilles

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:

// infrastructure code applies the mechanisms as part of the infrastructure stack surface 
// by designating the construction of the function as stack resource with `tracingActive: true`

new ExampleFunction(this, 'MyFunction', {
      functionName: 'MyFunction',
      tracingActive: true,
    });

The application code on the other hand:

// uses the utilities of power tools
// imports the utilities like logger, tracer and metrics and uses them in application code

import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
import { Logger } from '@aws-lambda-powertools/logger';
import { Tracer } from '@aws-lambda-powertools/tracer';
....

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 library aws-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 website
Look for notice by following this link

The 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 code

Related 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.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippeddocumentationImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions