The docs say I should specify a application revision in either .yaml or .json format when calling codedeploy. But how do I deploy the actual function code through code deploy?
There is no option to provide a zip with the code inside it. Only json and yaml files are accepted. How do I actually update/add the lambda function code through codedeploy? The below images shows even through aws console, I can only specify a yaml or json appspec file.
How do I actually deploy my function code here? ![Text]()
CodePudding user response:
You have to deploy it before your CodeDeploy (CD) stage. You can use CodeBuild (CB) for that. So in your CI/CD pipeline, your CD stage would be executed after CB stage. The CB stage would deploy and create a version of your function, which then would be used as an input to CD.
CodePudding user response:
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.