gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:53:55 24 4
gpt4 key购买 nike

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

我的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

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

最佳答案

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

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 报告了其他内容。

关于amazon-web-services - 如何在 gitlab CI/CD 中使用 zappa 将应用程序部署到 AWS Lambda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61476137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com