对于 SAM,有什么方法可以在通过 make 启动时查看 shell 脚本的输出

Is there any way to see the output of a shell script when launched through make, for SAM

我正在尝试构建一个多层 Python SAM(AWS 无服务器应用程序模型)应用程序。

没什么特别的,但它有一些依赖项,使用 pipenv 进行管理。

SAM 有一个 sam build 命令,它在幕后使用 make。 我有一个 Makefile 看起来像这样:

# Empty build target for "sam build". Why?
# 1. "sam build Layers" works
# 2. "sam build" for the main function doesn't (it tries to use pip and crashes)
# With this empty target the main build is skipped and only the layers are built, which is enough.
build-Function: ;

build-Layers:
    bash pip-install.sh "$(ARTIFACTS_DIR)"

pip-install.sh 看起来像这样:

#!/bin/bash
ARTIFACTS_DIR=""

set -eu

# Detect Cygwin: 
uname="$(uname -s)"
if [[ "${uname}" =~ "Cygwin" ]]
then
    # AWS SAM for Windows passes a Windows path Cygwin tools (make, etc.) don't understand.
    ARTIFACTS_DIR="$(cygpath -u "${ARTIFACTS_DIR}")"
fi

PROJECT_PATH="$(echo "${ARTIFACTS_DIR}" | sed 's@/.aws-sam.*@@')"
PIPFILE_PATH="$(echo "${ARTIFACTS_DIR}" | sed 's@/.aws-sam.*@/../Pipfile.lock@' | xargs readlink -f)"

mkdir -p "${ARTIFACTS_DIR}/python"
# We're using pipenv, which does not use requirements.txt.
# requirements.txt is used by SAM to automatically package and deploy layers (dependencies).
# To avoid dependency/version duplication, we extract the info from the pipenv info.

jq -r '.default | to_entries[] | .key + .value.version' "${PIPFILE_PATH}" > requirements.txt

# For debugging purposes.
ls -lha

python -m pip install -r "${PROJECT_PATH}/requirements.txt" -t "${ARTIFACTS_DIR}/python"

重要的是这个项目使用pipenv来管理依赖关系,而不是pip,SAM使用make作为构建工具,但在那之后,它只有一个pip 扩展名。这个脚本基本上创建了一个 pip 理解的依赖列表,然后 pip 下载一个文件夹中的所有依赖,然后将其打包为一个大层。

主要问题是调试。

我看到出于某些(恕我直言)原因 make 隐藏了它启动的 shell 脚本的输出。据说可以欺骗 make 来显示它,用这个:See output of shell script in Makefile

我试过了,还是不行。我 运行 sam build Layers 并且我只看到 sam 输出。我试过 --debug,同样的事情。

有没有办法显示该输出?我希望能够在 make/sam build 上下文中调试脚本,因为我不理解我遇到的一些错误

在 re:Invent 2020 年,AWS 宣布 Lambda 现在可以使用 Docker 个图像。

无需混合使用 SAM 提供的 Make 和专有设置来构建 Lambda 层,只需使用标准 Docker 文件即可节省大量时间

隐藏输出的不是make,而是sam (issue #2006)。当 make 失败时(返回任何不同于 0 的退出代码),它只打印 STDERR。您可以使用 shell 重定向将 STDOUT 重定向到 STDERR:

my-rule:
    npm run build-my-lambda 1>&2