如何在 gitlab CI/CD 中使用 zappa 将应用程序部署到 AWS Lambda?

How to use zappa in gitlab CI/CD to deploy app to AWS Lambda?

我正在尝试通过 gitlab CI 通过 zappa 在 aws lambda 上部署一个烧瓶应用程序。由于无法通过 gitlab CI 进行内联编辑,我在我的远程计算机上生成了 zappa_settings.json 文件,我正尝试使用它来执行 zappa deploy dev.

我的 zappa_settings.json 文件:

{
    "dev": {
        "app_function": "main.app",
        "aws_region": "eu-central-1",
        "profile_name": "default",
        "project_name": "prices-service-",
        "runtime": "python3.7",
        "s3_bucket": -MY_BUCKET_NAME-
    }
}

我的 .gitlab-ci.yml 文件:

image: ubuntu:18.04

stages:
  - deploy

before_script:
  - apt-get -y update
  - apt-get -y install python3-pip python3.7 zip
  - python3.7 -m pip install --upgrade pip
  - python3.7 -V
  - pip3.7 install virtualenv zappa

deploy_job:
  stage: deploy
  script:
    - mv requirements.txt ~
    - mv zappa_settings.json ~
    - mkdir ~/forlambda
    - cd ~/forlambda
    - virtualenv -p python3 venv
    - source venv/bin/activate
    - pip3.7 install -r ~/requirements.txt -t ~/forlambda/venv/lib/python3.7/site-packages/
    - zappa deploy dev

CI 文件在 运行 上出现以下错误:

如有任何建议,我们将不胜感激

zappa_settings.json 提交到 repo,而不是动态创建。即时创建的是 AWS 凭证文件。所需的值正在从项目的 Web UI 中设置的 Gitlab env vars 中读取。

zappa_settings.json

{
    "prod": {
        "lambda_handler": "main.handler",
        "aws_region": "eu-central-1",
        "profile_name": "default",
        "project_name": "dummy-name",
        "s3_bucket": "dummy-name",
        "aws_environment_variables": {
            "STAGE": "prod",
            "PROJECT": "dummy-name"
        }
    },
    "dev": {
        "extends": "prod",
        "debug": true,
        "aws_environment_variables": {
            "STAGE": "dev",
            "PROJECT": "dummy-name"
        }
    }
}

.gitlab-ci.yml

image:
  python:3.6

stages:
  - test
  - deploy

variables:
  AWS_DEFAULT_REGION: "eu-central-1"
  # variables set in gitlab's web gui:
  #   AWS_ACCESS_KEY_ID
  #   AWS_SECRET_ACCESS_KEY

before_script:
  # adding pip cache
  - export PIP_CACHE_DIR="/home/gitlabci/cache/pip-cache"

.zappa_virtualenv_setup_template: &zappa_virtualenv_setup
  # `before_script` should not be overriden in the job that uses this template
  before_script:
    # creating virtualenv because zappa MUST have it and activating it
    - pip install virtualenv
    - virtualenv ~/zappa
    - source ~/zappa/bin/activate

    # installing requirements in virtualenv
    - pip install -r requirements.txt

test code:
  stage: test
  before_script:
    # installing testing requirements
    - pip install -r requirements_testing.txt
  script:
    - py.test

test package:
  <<: *zappa_virtualenv_setup
  variables:
    ZAPPA_STAGE: prod
  stage: test
  script:
    - zappa package $ZAPPA_STAGE

deploy to production:
  <<: *zappa_virtualenv_setup
  variables:
    ZAPPA_STAGE: prod
  stage: deploy
  environment:
    name: production
  script:
    # creating aws credentials file
    - mkdir -p ~/.aws
    - echo "[default]" >> ~/.aws/credentials
    - echo "aws_access_key_id = "$AWS_ACCESS_KEY_ID >> ~/.aws/credentials
    - echo "aws_secret_access_key = "$AWS_SECRET_ACCESS_KEY >> ~/.aws/credentials

    # try to update, if the command fails (probably not even deployed) do the initial deploy
    - zappa update $ZAPPA_STAGE || zappa deploy $ZAPPA_STAGE
  after_script:
    - rm ~/.aws/credentials
  only:
    - master

我有一段时间没用过 zappa,但我记得很多错误都是由错误的 AWS 凭据引起的,但 zappa 报告了其他内容。