0

I'm trying to create an infrastructure with AWS CDK. When creating a lambda, it forces me to specify the code that's going in it.

However, that'll be the responsibility of the release pipeline.

Is there a way to create a lambda without specifying the code?

1
  • Just plug some dummy code in, or literally an empty string / file. That will cause the lambda itself to fail if invoked but that would be expected I guess. Commented Jan 3, 2023 at 8:39

1 Answer 1

2

No. code is a required prop in the CDK Lambda Function construct*. Use the InlineCode class as a minimal placeholder:

new lambda.Function(this, "Lambda", {
    code: new lambda.InlineCode(
        "exports.handler = async (event) => console.log(event)"
    ),
    runtime: lambda.Runtime.NODEJS_18_X,
    handler: "index.handler",
});

* It's also required for the CDK L1 CfnFunction. For what it's worth, Code is also a required input in the CreateFunction API and SDK commands.

Sign up to request clarification or add additional context in comments.

6 Comments

Makes sense. Thanks for sharing. But won't this overwrite the deployed function after we run the cdk deploy command next time?
Yes, of course. The CDK app (via CloudFormation) is meant to own the Lambda's runtime code and infrastructure code. It's a CDK best practice.
May I suggest you open a *new question* that addresses your use case? To add detail to the part you say "that'll be the responsibility of the release pipeline". I don't understand the proposed setup. I'm glad to help, but it's really a separate question.
I would not expect this to change the code during the next CDK deployment because CloudFormation is incapable of fixing drifted resources itself. During a stack update it should not even see that the code of the lambda changed in the meantime. Obviously still a bad idea since you rely on a bad CF behaviour and this would break if you were to ever change the dummy code at which point all lambdas would be overwritten.
@luk2302 Your "won't overwrite" expectation holds only if: (1) OP never modifies the CDK Lambda code and (2) OP never changes the function name (which will cause a replace-redeploy).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.