如何在gitlab CI/CD中使用zappa将应用程序部署到AWS Lambda? [英] How to use zappa in gitlab CI/CD to deploy app to AWS Lambda?

查看:148
本文介绍了如何在gitlab CI/CD中使用zappa将应用程序部署到AWS Lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过gitlab CI通过zappa在aws lambda上部署flask应用程序.由于无法通过gitlab CI进行内联编辑,因此我在远程计算机上生成了zappa_settings.json文件,并且尝试使用此文件来执行zappa deploy dev.

I am trying to deploy a flask application on aws lambda via zappa through gitlab CI. Since inline editing isn't possible via gitlab CI, I generated the zappa_settings.json file on my remote computer and I am trying to use this to do 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文件运行后,出现以下错误:

The CI file, upon running, gives me the following error:

任何建议都值得赞赏

推荐答案

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

zappa_settings.json is commited to the repo and not created on the fly. What is created on the fly is AWS credentials file. Values required are being read from Gitlab env vars set in the web UI of the project.

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报告了其他错误.

I haven't used zappa in a while, but I remember that a lot of errors that were caused by bad AWS credentials, but zappa reporting something else.

这篇关于如何在gitlab CI/CD中使用zappa将应用程序部署到AWS Lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆