How to deploy CDK app via Lambda
See original GitHub issueHi,
I think this is an unsupported use case for CDK. I am trying to deploy the CDK app via Lambda. The goal is for the Lambda function to call cdk deploy
and get the application, included together with the Lambda code, deployed.
Currently, calling cdk deploy
via the Node’s exec
command fails due to missing AWS credentials. Ideally, the same role that is used to execute the Lambda function should be reused. In my case this function has all the permissions to deploy a CFN template that the underlying CDK generates.
I tried to extract the deployment related code out of the aws-cdk
package and call it directly, but found out that it depends on the credential provider which tries to find the credentials either in the env variables or config files.
Is there a way to bypass this credentials check and let it just call the APIs to do the job?
Thanks.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:13
- Comments:11 (2 by maintainers)
Top GitHub Comments
AFAIK, Lambda doesn’t provide SDK credentials. The recommended way is to add proper permissions to the Lambda IAM role https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-lambda.html. Setting credentials via env variables is a security concern.
Considering the above, this issue is more of a limitation of CDK, not a question. So closing at will seems not caring about your customer which is one of your leadership principles.
Having said that, I figured out how to deploy a CDK app via Lambda. And, no, CDK will not work out of the box in Lambda. The following are the changes I had made locally and deployed to Lambda.
Overrode
SDK
class so that it doesn’t requirecredentials
for CloudFormation and S3 in https://github.com/awslabs/aws-cdk/blob/master/packages/aws-cdk/lib/api/util/sdk.ts#L95.Here is how I used
LambdaSDK
in the Lambda handler:Copied over the same command line parsing function from
cdk
into the lambda handler script and removed all commands exceptdeploy
.Note the last
parse()
command I added:That’s to make sure which app to use as well as override the staging directory since Lambda allows to write only to
/tmp
. Also, disablerequire-approval
since the execution is not supervised.Finally had to fix this line https://github.com/awslabs/aws-cdk/blob/master/packages/aws-cdk/lib/api/util/sdk.ts#L71:
When running from Lambda
require.main
is Lambda, meaning the above code will fail with:The fixed code is:
As a reminder, because I didn’t appreciate your response, this was not a question but a report that a certain functionality is not supported. If you reopen, I will, perhaps, create a PR to make CDK work from within Lambda.
@kadishmal can you please provide an example how to provision CDK App from Lambda?