As @Marcin mentioned, you need to update the actual function through your codebuild specification yaml file.
there are plenty of approaches for having CI/CD for serverless lambda functions. (Cloudformation, Terraform, CodeDeploy, using only CodeBuild and update the code there!)
what I personally do is using ECR, Docker Images to handle the CI/CD proccess, here's the flow:
- create an ECR repository
- create a docker image of your lambda function
- push the docker image to the ECR
in your buildspec.yml
file:
- try to get credentials for ECR
- and build the lambda docker image based on the artifact coming from source section in codepipeline
- push the new docker image to ECR
- update the lambda with the docker image.
here's a sample:
version: 0.2
phases:
pre_build:
commands:
- echo "Build on `date`"
- aws ecr get-login-password --region ca-central-1 | docker login --username AWS --password-stdin YOUR_ECR_ADDRESS
- npm run test
build:
commands:
- echo Building the Docker image ... | tee -a log.txt
- docker build -t YOUR_ECR_ADDRESS/your-lambda:latest . 2>&1 | tee -a log.txt
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push YOUR_ECR_ADDRESS/your-lambda:latest
- aws lambda update-function-code --function-name your-lambda --image-uri YOUR_ECR_ADDRESS/your-lambda:latest > /dev/null
You can also have cloudformation as last stage so you can change a function specifications i.e roles, timeout, ram usage, etc by changing only a template yml file.