CodeBuild - ResourceConflictException 问题
CodeBuild - ResourceConflictException issue
我有以下 buildspec.yml 文件,我用它来使用 AWS CodeBuild 构建我的应用程序:
version: 0.2
env:
variables:
function_name: ${lambda_function_name}
alias: ${lambda_alias_name}
files: ${app_files}
phases:
build:
commands:
# Zipping all files from source stage
- zip -r index.zip $files
# Updating the lambda function code
- aws lambda update-function-code --function-name $function_name --zip-file fileb://index.zip
# Getting the current version being used by the alias
- export current_version=$(aws lambda get-alias --function-name $function_name --name $alias | grep FunctionVersion | awk -F'"' '{print }')
# Publishing the new lambda version and storing its version number
#- sleep 2
- export target_version=$(aws lambda publish-version --function-name $function_name | grep Version | awk -F'"' '{print }')
# Creating the appspec.json file for CodeDeploy
- echo '{"Resources":[{"myLambdaFunction":{"Properties":{"Alias":"'${lambda_alias_name}'","CurrentVersion":"'$current_version'","Name":"'$lambda_function_name'","TargetVersion":"'$target_version'"},"Type":"AWS::Lambda::Function"}}],"version":0}' >> appspec.json
# Deployment without CodeDeploy
#- aws lambda update-alias --function-name $function_name --name $alias --function-version $version
执行returns出现如下错误:
An error occurred (ResourceConflictException) when calling the PublishVersion operation: The operation cannot be performed at this time. An update is in progress for resource: arn:aws:lambda:...
我认为错误是因为 update-function-code 在我调用 publish-version[=24= 时还没有准备好].
我发现的一个解决方法是在代码中设置 2 秒的休眠时间,但我不喜欢这个解决方案,因为它可能需要更长的时间,或者我在浪费金钱来支付不必要的休眠时间。
如何更新此脚本以便在代码更新完成后立即发布 Lambda 函数?
您可以使用此脚本更新 lambda 函数的状态
#!/bin/bash
set -xeuo pipefail
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
distribution_id=""
function_name=""
region=""
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
while [[ "$STATE" == "InProgress" ]]
do
echo "sleep 5sec ...."
sleep 5s
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
echo $STATE
done
幸运的是,我开发了 CodeBuild 来将我的函数部署到 Lambda@Edge,并且昨天刚刚完成。
@Ash Blake 的回答有效,但是我做了一些小的调整,因为我不希望 CodeBuild 挂起数小时以防发生变化或出错...
#!/bin/bash
# Control variables
COUNTER=0
TRIES=10
# Awaiting for the Lambda to be updated
echo 'Awaiting for the code to be updated...'
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
while [[ $STATE != "Successful" ]] && [[ $COUNTER -le $TRIES ]]
do
sleep 0.5s
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
COUNTER=$COUNTER+1
echo $STATE
done
我有以下 buildspec.yml 文件,我用它来使用 AWS CodeBuild 构建我的应用程序:
version: 0.2
env:
variables:
function_name: ${lambda_function_name}
alias: ${lambda_alias_name}
files: ${app_files}
phases:
build:
commands:
# Zipping all files from source stage
- zip -r index.zip $files
# Updating the lambda function code
- aws lambda update-function-code --function-name $function_name --zip-file fileb://index.zip
# Getting the current version being used by the alias
- export current_version=$(aws lambda get-alias --function-name $function_name --name $alias | grep FunctionVersion | awk -F'"' '{print }')
# Publishing the new lambda version and storing its version number
#- sleep 2
- export target_version=$(aws lambda publish-version --function-name $function_name | grep Version | awk -F'"' '{print }')
# Creating the appspec.json file for CodeDeploy
- echo '{"Resources":[{"myLambdaFunction":{"Properties":{"Alias":"'${lambda_alias_name}'","CurrentVersion":"'$current_version'","Name":"'$lambda_function_name'","TargetVersion":"'$target_version'"},"Type":"AWS::Lambda::Function"}}],"version":0}' >> appspec.json
# Deployment without CodeDeploy
#- aws lambda update-alias --function-name $function_name --name $alias --function-version $version
执行returns出现如下错误:
An error occurred (ResourceConflictException) when calling the PublishVersion operation: The operation cannot be performed at this time. An update is in progress for resource: arn:aws:lambda:...
我认为错误是因为 update-function-code 在我调用 publish-version[=24= 时还没有准备好].
我发现的一个解决方法是在代码中设置 2 秒的休眠时间,但我不喜欢这个解决方案,因为它可能需要更长的时间,或者我在浪费金钱来支付不必要的休眠时间。
如何更新此脚本以便在代码更新完成后立即发布 Lambda 函数?
您可以使用此脚本更新 lambda 函数的状态
#!/bin/bash
set -xeuo pipefail
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
distribution_id=""
function_name=""
region=""
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
while [[ "$STATE" == "InProgress" ]]
do
echo "sleep 5sec ...."
sleep 5s
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
echo $STATE
done
幸运的是,我开发了 CodeBuild 来将我的函数部署到 Lambda@Edge,并且昨天刚刚完成。
@Ash Blake 的回答有效,但是我做了一些小的调整,因为我不希望 CodeBuild 挂起数小时以防发生变化或出错...
#!/bin/bash
# Control variables
COUNTER=0
TRIES=10
# Awaiting for the Lambda to be updated
echo 'Awaiting for the code to be updated...'
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
while [[ $STATE != "Successful" ]] && [[ $COUNTER -le $TRIES ]]
do
sleep 0.5s
STATE=$(aws lambda get-function --function-name "$function_name" --region $region --query 'Configuration.LastUpdateStatus' --output text)
COUNTER=$COUNTER+1
echo $STATE
done