Official Lumigo construct for AWS CDK 2
The AWS Cloud Development Kit (CDK) enables you to define your cloud application resources using familiar programming languages, mixing an imperative programming style with the declarative nature of the underpinning AWS CloudFormation.
Today, we are launching @lumigo/cdk-constructs-v2: 1st-party, officially supported constructs to add Lumigo autotracing for your Lambda functions in CDK applications!
What is supported today?
The Lumigo construct can add Lumigo autotracing to all your Node.js and Python Lambda functions in a CDK app:
import { Lumigo } from '@lumigo/cdk-constructs-v2'; import { App, SecretValue } from 'aws-cdk-lib'; const app = new App(); // Add here stacks and constructs new Lumigo({lumigoToken:SecretValue.secretsManager('LumigoToken')}).traceEverything(app); app.synth();
Tracing can also be added to all Lambda functions in a stack:
import { Lumigo } from '@lumigo/cdk-constructs-v2'; import { App, SecretValue } from 'aws-cdk-lib'; export class NodejsStack extends Stack { constructor(scope: Construct, id: string, props: StackProps = {}) { super(scope, id, props); new Function(this, 'MyLambda', { code: new InlineCode('foo'), handler: 'index.handler', runtime: Runtime.NODEJS_14_X, }); } } const app = new App(); const stack = new NodejsStack(app, 'NodejsTestStack', { env: { region: 'eu-central-1', } }); new Lumigo({lumigoToken:SecretValue.secretsManager('LumigoToken')}).traceEverything(stack); app.synth();
You can also add tracing to Lambda functions one by one:
import { Lumigo } from '@lumigo/cdk-constructs-v2'; import { App, SecretValue } from 'aws-cdk-lib'; interface NodejsStackProps extends StackProps { readonly lumigo: Lumigo; } export class NodejsStack extends Stack { constructor(scope: Construct, id: string, props: NodejsStackProps = {}) { super(scope, id, props); const handler = new Function(this, 'MyLambda', { code: new InlineCode('foo'), handler: 'index.handler', runtime: Runtime.NODEJS_14_X, }); props.lumigo.traceLambda(handler); } } const app = new App(); const lumigo = new Lumigo({lumigoToken:SecretValue.secretsManager('LumigoToken')}); const stack = new NodejsStack(app, 'NodejsTestStack', { env: { region: 'eu-central-1', }, lumigo, }); app.synth();
Autotracing can be added to all Lambda functions declared as a Function from the aws-cdk-lib/aws-lambda package with a Node.js or Python runtime, NodejsFunction from the aws-cdk-lib/aws-lambda-nodejs and PythonFunction from the @aws-cdk/aws-lambda-python-alpha package.
Which version of the tracer will be used?
Each version of the @lumigo/cdk-constructs-v2 package comes with a built-in list of latest Lambda layers as of the time of publishing. We regularly (in a matter of hours) publish new versions of the @lumigo/cdk-constructs-v2 package when we release a new layer. Reproducibility is very important for AWS CDK apps, and that is why the same version of the @lumigo/cdk-constructs-v2 will always generate the same CloudFormation. (Moreover, it is bad practice to invoke APIs during the "synth" phase in CDK.) So, to get the newest tracers deployed, update the version of the @lumigo/cdk-constructs-v2 package and run "cdk deploy" anew.
It also possible for you to manually specify which version of the layer to use:
import { Lumigo } from '@lumigo/cdk-constructs-v2'; import { App, SecretValue } from 'aws-cdk-lib'; const app = new App(); // Add here stacks and constructs new Lumigo({lumigoToken:SecretValue.secretsManager('LumigoToken')}).traceEverything(app, { lambdaNodejsLayerVersion: 42, // All Lambda functions with a supported Node.js runtime will use the layer v42 lambdaPythonLayerVersion: 1, // All Lambda functions with a supported Python runtime will use the layer v1 }); app.synth();
Why using a CDK construct when Lumigo has autotracing in the UI?
You can indeed ask Lumigo to autotrace functions you manage with any of the tools out there that rely on CloudFormation (CDK itself, the Serverless framework, the AWS Serverless Application Model), but some may find that having Lumigo change infrastructure after deployment is not ideal in Infrastructure as Code (IaC) approaches. So, we are making it straightforward to add Lumigo tracing as an integral part of your IaC specification. This is why we built the Lumigo Serverless plugin before, and we are releasing the Lumigo CDK constructs today!
Isn't 'Lumigo.traceEverything' a little bold as an API name? After all, it's only Lambda, no?
Well, we are just getting started. There is some really interesting stuff happening with Lumigo in and beyond Serverless in the next few months ;-)
Credits
Special thanks to Thorsten Höger of Taimos, AWS DevTools Hero and author of "The CDK Book", for his invaluable feedback during the ideation and first implementation of the @lumigo/cdk-constructs-v2 package.
Thorsten offers various service packages to help you level up your CDK usage, offers reviews of your CDK apps, and runs the Cloud Automation Weekly podcast, where I had the pleasure to be his first guest, discussing how I use daily AWS CDK in my Product Management work at Lumigo.