简体   繁体   中英

How do i deploy a simple AWS lambda function using AWS code deploy without using SAM?

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]()

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.

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:

  1. create an ECR repository
  2. create a docker image of your lambda function
  3. push the docker image to the ECR

in your buildspec.yml file:

  1. try to get credentials for ECR
  2. and build the lambda docker image based on the artifact coming from source section in codepipeline
  3. push the new docker image to ECR
  4. 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 ie roles, timeout, ram usage, etc by changing only a template yml file.

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