how to deploy to aws using ci/cd for zappa(python)

Viewed 2360

I'm using zappa to deploy on aws. And I wanted to implement CI/CD on AWS. So, I created a pipeline and successfully did Aws COMMIT and AWS BUILD.

I'm unable to deploy the same using AWS CODE DEPLOY.

The Buildspec.yaml looks like this:

version: 0.2


    phases:
      install:
        commands:
          - echo Setting up virtualenv
          - python -m venv venv
          - source venv/bin/activate
          - echo Installing requirements from file
          - pip install -r requirements.txt
      build:
        commands:
          - echo Build started on `date`
          - echo Building and running tests
          - python tests.py
          - flask db upgrade
      post_build:
        commands:
          - echo Build completed on `date`
          - echo Starting deployment
          - zappa update dev
          - echo Deployment completed

How should I execute zappa deploy or zappa update on AWS?

I'm not sure how to add create appspec.yaml file.

Please HELP! Stuck!!

2 Answers

Here's a buildspec.yml file that I use. You could adjust this to suit your needs (for example, including the DB upgrade command).

version: 0.2

phases:
  install:
    commands:
      - mkdir /tmp/src/
      - mv $CODEBUILD_SRC_DIR/* /tmp/src/
      - cd /tmp/src/
      - python3 -m venv docker_env && source docker_env/bin/activate && pip install --upgrade pip==9.0.3 && pip install -r requirements.txt && zappa update production && deactivate && rm -rf docker_env
  post_build:
    commands:
      - cd $CODEBUILD_SRC_DIR
      - rm -rf /tmp/src/
      - echo Build completed on `date`

Note that this is using the Docker image danielwhatmuff/zappa:python3.6 in CodeBuild. I use this image as it's based on AWS Lambda and has been tuned for Zappa.

Zappa update to Code Deploy: Your Buildspec.yaml looks fair good but there is one important point to consider.

Postbuild will always run regardless of success/failure. Debug information can be pulled from a failed build.

Either check the reason for failure from build log, or modify your yml to look like below (caution: this is only draft change, test before using in systems):

version: 0.2
  phases:
    install:
      commands:
        - yum -y groupinstall development
        - yum -y install zlib-devel
        - yum -y install openssl-devel
        - wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz
        - tar xJf Python-3.6.0.tar.xz
        - cd Python-3.6.0
        - ./configure
        - make
        - make install 
        - ln -s /usr/local/bin/python3.6 /usr/bin/python3
        - curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
        - python3 get-pip.py
        - pip3 install virtualenv
        - virtualenv -p /usr/bin/python3 venv
        - source venv/bin/activate
        - pip3 install -r requirements.txt
    build:
      commands:
        - echo Build started on `date`
        - echo Building and running tests
        - python3 tests.py
        - flask db upgrade
    post_build:
      commands:
        - if [ $CODEBUILD_BUILD_SUCCEEDING = 1 ]; then echo Build completed on `date`; echo Starting deployment; zappa update dev; else echo Build failed ignoring deployment; fi
        - echo Deployment completed

Hope it answers.

Zappa update to AWS

Below are the steps to do Zappa update on AWS

  1. Configure AWS with IAM user
  2. Configure AWS cli in the local host using command a. pip install awscli b. aws configure
  3. Call "Zappa init", it will generate zappa_settings.json based on details provided
  4. Zappa deploy <name provided for environment in step3>

Now your application will be deployed to AWS. Whenever you need to update call

Zappa update <name provided for environment in step3>

Related