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

时间:2020-04-28 08:46:36

标签: amazon-web-services aws-lambda gitlab gitlab-ci zappa

我正在尝试通过gitlab CI通过zappa在aws lambda上部署flask应用程序。由于无法通过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文件运行后,出现以下错误:

enter image description here

任何建议都值得赞赏

1 个答案:

答案 0 :(得分:3)

zappa_settings.json被提交到仓库,而不是即时创建的。动态创建的是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报告了其他错误。