在 AWS Code Build 中将 jq 1.3 升级到 1.6

Chalice and Upgrading jq 1.3 to 1.6 in AWS Code Build

由于需要使用 --argjson 参数,我一直在努力弄清楚如何将 AWS CodeBuild 预安装的 jq 从 1.3 更新到 1.6。一个简单的 apt-get 行不通。

我一直在尝试弄清楚如何将 AWS CodeBuild 预安装的 jq 从 1.3 更新到 1.6,以便我可以使用我的 DevOps 所需的一些额外功能。我确实弄明白了,所以这更像是一个答案而不是一个问题,但我想我会分享,因为我在网上找不到任何专门针对 CodeBuild 的内容。

# !/bin/bash
pip install --upgrade awscli
aws --version

# Upgrade JQ so JSON substitution can work for layers
wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-1.6.tar.gz
tar -xzf jq-1.6.tar.gz
cd jq-1.6
autoreconf -fi && ./configure --disable-maintainer-mode && make && make install || exit 1
jq --version

现在这个解决方案的原因是我可以用我的 CodeBuild 生成的新 lambda 层更新我的 Chalice 配置文件。这是 CodeBuild for Chalice 的完整解决方案:

# !/bin/bash
pip install --upgrade awscli
aws --version
# Upgrade JQ
cd ..

# Upgrade JQ so JSON substitution can work for layers
wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-1.6.tar.gz
tar -xzf jq-1.6.tar.gz
cd jq-1.6
autoreconf -fi && ./configure --disable-maintainer-mode && make && make install || exit 1
jq --version
cd ../
cd src

# Create virtual environment and install requirements
pip install virtualenv
virtualenv /tmp/venv
. /tmp/venv/bin/activate
pip install -r requirements.txt
pip install snowflake-sqlalchemy

cd ../
## Layer update
mkdir -p lambda_layers/python/lib/python3.7/site-packages
# 1) install the dependencies in the desired folder
pip3 install  --upgrade snowflake-sqlalchemy -t lambda_layers/python/lib/python3.7/site-packages/.
# 2) Zip the lambda_layers folder
cd lambda_layers
zip -r snowflake_lambda_layer.zip *
# 3) publish layer
layerResults=$(
    echo aws lambda publish-layer-version \
        --layer-name fl-snowflake-lambda-layer \
        --compatible-runtimes python3.7 \
        --zip-file fileb://snowflake_lambda_layer.zip
)
echo "layer response: $layerResults"
layerARN=$(${layerResults} | jq '.LayerVersionArn')
echo "LayerARN: $layerARN"
cd ../
cd src

## Update Config
contents="$(jq --argjson arn ${layerARN} '.layers = [$arn]' .chalice/config.json)" && echo ${contents} && echo ${contents} > .chalice/config.json

# Unit tests
pip install -r requirements-test.txt
pip install chalice
export PYTHONPATH=.
pytest ./tests/ || exit 1

# Run Migrations
alembic -x stage=${stage} upgrade heads || exit 1

# Load Fixtures
echo ${stage}
python chalicelib/Fixtures/loadFixtures.py ${stage} || exit 1

# Package and deploy Chalice
# chalice deploy --stage ${stage}
chalice package --stage ${stage} /tmp/packaged
aws cloudformation package --template-file /tmp/packaged/sam.json --s3-bucket "${APP_S3_BUCKET}" --output-template-file transformed.yaml