简体   繁体   中英

AWS CodePipline (CI/CD) to update the code of an existing AWS Lambda (not created with CloudFormation or SAM)

I need to build a CI/CD pipeline that updates the code of an existing Lambda Function that was created using the console. Creating a new one is not an option!

I was successful deploying the code using CloudFormation in the Deploy Stage but, it can only update the code for Lambda functions that are created with CloudFormation/SAM. When I specify the name of and existing Lambda in the template.yaml file it tries to create it and tells me that it already exists!

The 2nd way i tried is deploying using CodeDeploy and i get this error: - "BundleType must be either YAML or JSON"

This thread gives a workaround this problem and i could not apply it to my case: CodePipeline: CodeDeploy reports "BundleType must be either YAML or JSON"

Is there any other way to make a proper CI/CD pipeline that only updates the code for a Lambda Function ?

One way of updating Lambda code from CloudFormation would be to store your Lambda code in S3 with versioning enabled.

Here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html

Changes to a deployment package in Amazon S3 are not detected automatically during stack updates. To update the function code, change the object key or version in the template.

So, the pipeline could be something like:

  • 1) Checkout Lambda code from a repository. Or
  • 2) Upload a new zip file to a versioned S3 bucket.
  • if 1), then zip the contents and upload it to a versioned S3 bucket.
  • Once done, you will have a new versioned object to call CloudFormation on (you will need to update your template.yml file with this new version, which can be done retrieving this new object version using CLI) and an UPDATE can happen as described in the documentation.

Hope that helps.

I also ran into similar issue. My requirement was bit different. I will explain the issue and solution we used so that this might be helpful for someone else. I started with approach explained by @marianogg9.

In our case, i was using github as source trying to deploy a stackset which contains a lambda function. So i had a lambda python file and template yml file in github. To deploy, i was using below steps which ran into issue that lambda function is not getting updated even after deploying the stackset everytime.

  1. get files from github
  2. zip lambda python file and upload to an s3 bucket (existing bucket).
  3. yml template contains this bucket path and s3key predefined. So everytime we were overwriting the s3 lambda file after we execute code pipeline. But as per aws documentation

Changes to a deployment package in Amazon S3 are not detected automatically during stack updates. To update the function code, change the object key or version in the template.

which means you need to either use a new bucket or use a new version everytime. So for this, first i enabled versioning for s3 bucket. Then i created a parameter versionId in the template file. So my idea was to upload zip file to s3 and then use cli command to get the latest version id and then override the versionid parameter using parameter override property of create stack instances cli command. Since i had to create a stackset and code pipeline by default wont support parameter override option if action provider is cloudformation stackset. So instead of that i used a code build stage again and then used cli commands to create stackset and then create stack instances. Using CLI commands for stack instances allowed me to use the parameter override property and thus i was able to update the version id parameter used in the template which will eventually update the lambda function after its deployed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM