【发布时间】:2020-09-27 04:51:59
【问题描述】:
我将 aws 处理程序脚本部署为 zip 文件在 S3 存储桶中。现在,我想在 lambda 函数中使用这个部署的压缩文件。我正在通过 gitlab CI 完成所有这些工作。
我有以下 CI:
image: ubuntu:18.04
variables:
GIT_SUBMODULE_STRATEGY: recursive
AWS_DEFAULT_REGION: eu-central-1
S3_BUCKET: $BUCKET_TRIAL
stages:
- deploy
.before_script_template: &before_script_definition
stage: 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
.after_script_template: &after_script_definition
after_script:
# Upload package to S3
# Install AWS CLI
- pip install awscli --upgrade # --user
- export PATH=$PATH:~/.local/bin # Add to PATH
# Configure AWS connection
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set default.region $AWS_DEFAULT_REGION
- aws sts get-caller-identity --output text --query 'Account'
- aws s3 cp ~/forlambda/archive.zip $BUCKET_TRIAL/${LAMBDA_NAME}-deployment.zip
- aws lambda create-function --function-name "${LAMBDA_NAME}-2" --runtime python3.7 --role arn:aws:iam::xxxxxxxxxxxx:role/abc_scripts --handler ${HANDLER_SCRIPT_NAME}.${HANDLER_FUNCTION} --memory-size 1024 --zip-file "fileb://$BUCKET_TRIAL/${LAMBDA_NAME}-deployment.zip"
my_job:
variables:
LAMBDA_NAME: my_lambda
HANDLER_SCRIPT_NAME: my_aws_handler
HANDLER_FUNCTION: my_handler_function
<<: *before_script_definition
script:
# - move scripts around and install requirements and zip the file for lambda deployment
<<: *after_script_definition
对于 CI,我添加了环境变量 $BUCKET_TRIAL,格式为 s3://my-folder
CI文件运行时,最后抛出如下错误:
解析参数“--zip-file”时出错:无法加载参数文件 fileb://s3://my-folder/my_lambda-deployment.zip: [Errno 2] 没有 文件或目录:'s3://my-folder/my_lambda-deployment.zip'
我还尝试将after_script最后一行中的--zip-file更改为:
- aws lambda create-function --function-name "${LAMBDA_NAME}-2" --runtime python3.7 --role arn:aws:iam::xxxxxxxxxxxx:role/abc_scripts --handler ${HANDLER_SCRIPT_NAME}.${HANDLER_FUNCTION} --memory-size 1024 --zip-file "fileb:///my-folder/${LAMBDA_NAME}-deployment.zip"
但它仍然会抛出同样的错误。
我错过了什么吗?
【问题讨论】:
-
如果您的代码是
~/forlambda/archive.zip,为什么还要使用fileb://$BUCKET_TRIAL?不应该只是--zip-file "fileb://~/forlambda/archive.zip吗?不确定~是否会在此处扩展。可以尝试使用完整路径。 -
@Marcin 嘿,我这样做是因为压缩后的部署文件是
$BUCKET_TRIAL(S3 存储桶中的一个文件夹) -
但
file://或fileb://仅适用于本地路径,不适用于 s3。如果你想要 s3,那么在我看来,我应该是 `--zip-file "$BUCKET_TRIAL/....`。 -
@Marcin 我试过你的建议
--zip-file "$BUCKET_TRIAL/${LAMBDA_NAME}-deployment.zip",但它没有显示--zip-file must be a zip file with the fileb:// prefix -
你试过我的第一个建议了吗?如果需要
fileb,那么它必须是本地文件,而不是 s3 文件上的远程文件?
标签: amazon-web-services amazon-s3 aws-lambda gitlab-ci aws-cli