4
votes

I have several AWS Lambda Functions created using AWS Console and every function has some dependent APIs.Now I want to integrate the same with AWS CI/CD using CodeBuild, CodePipeline, and CloudFormation. But the problem is I'm unable to update the existing lambda functions that are not created using the CloudFormation Stack
Error Message ABC_Lambda already exists

Please note that if I create a new lambda using the CI/CD flow and then try to update it, that's working fine.

So my questions are

  1. How do I integrate the existing Lambda functions with AWS CI/CD workflow, without deleting and re-creating them?

  2. Is there any way or SAM_template config param that force the CloudFormation to update the existing lambda(created using AWS console/AWS Commandline tool).

4

4 Answers

2
votes

As noted by @omuthu, if you have not created the Lambdas with Cloudformation, you cannot use Cloudformation to update them.

What you could do, is, in your CodeBuild step, use the update-function-code and update-function-configuration APIs to perform the operations you need. This though does not allow you to integrate with SAM due to the limitation mentioned above.

1
votes

AWS CloudFormation can update only resources created by the same stack.

The only possibility I can think of is to have CloudFormation Custom Resource.

The custom resource lambda should take care of handling any resources that are not created by the stack.

Learn more about it here (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources-lambda.html)

Hoe this helps !!!

0
votes

I would treat the Lambda functions you created in the console as prototypes and incrementally build a CloudFormation stack to replace the prototype. The trick to avoid the error message (ABC_Lambda already exists) is to only specify a logical name in CloudFormation and let it assign the physical name. If you call the Lambda functions by name or an API Gateway endpoint, you'll need to update the caller when you're ready to cut over to the new stack.

0
votes

If you really needed to keep the old lambda functions around for legacy reasons you could create your new functions in Cloudformation, and update your old function manually to "proxy" and execute your new lambda code.

However, you will incur additional cost for the second lambda - and possibly hit your concurrent lambda request limit.

Over time you can update your old config / code to call just the new function, and eventually remove the old one entirely.