Skip to content

Commit

Permalink
Re-use base image's entrypoint, refactor bootstrap script
Browse files Browse the repository at this point in the history
  • Loading branch information
carlzogh committed Nov 11, 2021
1 parent 29bd948 commit 9668ee8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 36 deletions.
9 changes: 3 additions & 6 deletions LambdaRuntimeDockerfiles/Images/net5/amd64/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ ENV \

COPY --from=publish /app/publish /var/runtime

COPY --from=publish /app/publish/bootstrap.sh /
RUN rm -f /var/runtime/bootstrap.sh && \
chmod +x /bootstrap.sh && \
# Keep the legacy name lambda-entrypoint.sh for the bootstrap script for existing CloudFormation templates referencing it
cp /bootstrap.sh /lambda-entrypoint.sh
COPY --from=publish /app/publish/bootstrap.sh /var/runtime/bootstrap
RUN chmod +x /var/runtime/bootstrap

ENTRYPOINT ["/bootstrap.sh"]
# Entrypoint is inherited from public.ecr.aws/lambda/provided
2 changes: 1 addition & 1 deletion LambdaRuntimeDockerfiles/Images/net6/amd64/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ COPY --from=publish /app/publish /var/runtime
RUN mv /var/runtime/bootstrap.sh /var/runtime/bootstrap && \
chmod +x /var/runtime/bootstrap

ENTRYPOINT ["/var/runtime/bootstrap"]
# Entrypoint is inherited from public.ecr.aws/lambda/provided
2 changes: 1 addition & 1 deletion LambdaRuntimeDockerfiles/Images/net6/arm64/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ COPY --from=publish /app/publish /var/runtime
RUN mv /var/runtime/bootstrap.sh /var/runtime/bootstrap && \
chmod +x /var/runtime/bootstrap

ENTRYPOINT ["/var/runtime/bootstrap"]
# Entrypoint is inherited from public.ecr.aws/lambda/provided
83 changes: 55 additions & 28 deletions Libraries/src/Amazon.Lambda.RuntimeSupport/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,77 @@
# These files are used to add the end-user assembly into context and make the code reachable to the dotnet process
# Since the file names are not known in advance, we use this shell script to find the files and pass them to the dotnet process as parameters
# You can improve cold-start performance by setting the LAMBDA_DOTNET_MAIN_ASSEMBLY environment variable and specifying the assembly name
USER_LAMBDA_BINARIES_DIR="/var/task/"
if [ ! -d "$USER_LAMBDA_BINARIES_DIR" ]; then
echo "Error: .NET binaries for Lambda function are not correctly installed in the $USER_LAMBDA_BINARIES_DIR directory of the image when the image was built. The $USER_LAMBDA_BINARIES_DIR directory is missing." 1>&2
exit 1
USER_LAMBDA_BINARIES_DIR="/var/task"
if [ ! -d "${USER_LAMBDA_BINARIES_DIR}" ]; then
echo "Error: .NET binaries for Lambda function are not correctly installed in the ${USER_LAMBDA_BINARIES_DIR} directory of the image when the image was built. The ${USER_LAMBDA_BINARIES_DIR} directory is missing." 1>&2
exit 1
fi

if [[ `expr index "$1" ":"` == 0 ]]; then
EXECUTABLE_ASSEMBLY=$1
if [[ "$EXECUTABLE_ASSEMBLY" != *.dll ]]; then
DOTNET_BIN="/var/lang/bin/dotnet"
DOTNET_EXEC="exec"
DOTNET_ARGS=()

LAMBDA_HANDLER=""
# Command-line parameter has precedence over "_HANDLER" environment variable
if [ ! -z "${1}" ]; then
LAMBDA_HANDLER="${1}"
elif [ ! -z "${_HANDLER}" ]; then
LAMBDA_HANDLER="${_HANDLER}"
else
echo "Error: No Lambda Handler function was specified." 1>&2
exit 1
fi

if [[ `expr index "${LAMBDA_HANDLER}" ":"` == 0 ]]; then
EXECUTABLE_ASSEMBLY="${USER_LAMBDA_BINARIES_DIR}"/"${LAMBDA_HANDLER}"
if [[ "${EXECUTABLE_ASSEMBLY}" != *.dll ]]; then
EXECUTABLE_ASSEMBLY="${EXECUTABLE_ASSEMBLY}.dll"
fi

if [ ! -f "${USER_LAMBDA_BINARIES_DIR}/${EXECUTABLE_ASSEMBLY}" ]; then
echo "Error: executable assembly $EXECUTABLE_ASSEMBLY was not found." 1>&2
exit 1
fi
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
exec /usr/local/bin/aws-lambda-rie /var/lang/bin/dotnet exec "${USER_LAMBDA_BINARIES_DIR}/${EXECUTABLE_ASSEMBLY}"
else
/var/lang/bin/dotnet exec "${USER_LAMBDA_BINARIES_DIR}/${EXECUTABLE_ASSEMBLY}"
if [ ! -f "${EXECUTABLE_ASSEMBLY}" ]; then
echo "Error: executable assembly ${EXECUTABLE_ASSEMBLY} was not found." 1>&2
exit 1
fi

DOTNET_ARGS+=("${EXECUTABLE_ASSEMBLY}")
else
ASSEMBLY_NAME="${LAMBDA_DOTNET_MAIN_ASSEMBLY}"
if [ -z "$ASSEMBLY_NAME" ]; then
if [ -z "${ASSEMBLY_NAME}" ]; then
DEPS_FILE=`find "${USER_LAMBDA_BINARIES_DIR}" -name \*.deps.json -print`
if [ -z "$DEPS_FILE" ]; then
echo "Error: .NET binaries for Lambda function are not correctly installed in the $USER_LAMBDA_BINARIES_DIR directory of the image when the image was built. The $USER_LAMBDA_BINARIES_DIR directory is missing the required .deps.json file." 1>&2
if [ -z "${DEPS_FILE}" ]; then
echo "Error: .NET binaries for Lambda function are not correctly installed in the ${USER_LAMBDA_BINARIES_DIR} directory of the image when the image was built. The ${USER_LAMBDA_BINARIES_DIR} directory is missing the required .deps.json file." 1>&2
exit 1
fi
RUNTIMECONFIG_FILE=`find "${USER_LAMBDA_BINARIES_DIR}" -name \*.runtimeconfig.json -print`
if [ -z "$RUNTIMECONFIG_FILE" ]; then
echo "Error: .NET binaries for Lambda function are not correctly installed in the $USER_LAMBDA_BINARIES_DIR directory of the image when the image was built. The $USER_LAMBDA_BINARIES_DIR directory is missing the required .runtimeconfig.json file." 1>&2
if [ -z "${RUNTIMECONFIG_FILE}" ]; then
echo "Error: .NET binaries for Lambda function are not correctly installed in the ${USER_LAMBDA_BINARIES_DIR} directory of the image when the image was built. The ${USER_LAMBDA_BINARIES_DIR} directory is missing the required .runtimeconfig.json file." 1>&2
exit 1
fi
else
if [[ "$ASSEMBLY_NAME" == *.dll ]]; then
if [[ "${ASSEMBLY_NAME}" == *.dll ]]; then
ASSEMBLY_NAME="${ASSEMBLY_NAME::-4}"
fi
DEPS_FILE="${USER_LAMBDA_BINARIES_DIR}${ASSEMBLY_NAME}.deps.json"
RUNTIMECONFIG_FILE="${USER_LAMBDA_BINARIES_DIR}${ASSEMBLY_NAME}.runtimeconfig.json"
DEPS_FILE="${USER_LAMBDA_BINARIES_DIR}/${ASSEMBLY_NAME}.deps.json"
RUNTIMECONFIG_FILE="${USER_LAMBDA_BINARIES_DIR}/${ASSEMBLY_NAME}.runtimeconfig.json"
fi
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
exec /usr/local/bin/aws-lambda-rie /var/lang/bin/dotnet exec --depsfile $DEPS_FILE --runtimeconfig $RUNTIMECONFIG_FILE /var/runtime/Amazon.Lambda.RuntimeSupport.dll $1
else
/var/lang/bin/dotnet exec --depsfile $DEPS_FILE --runtimeconfig $RUNTIMECONFIG_FILE /var/runtime/Amazon.Lambda.RuntimeSupport.dll $1

DOTNET_ARGS+=("--depsfile" "${DEPS_FILE}"
"--runtimeconfig" "${RUNTIMECONFIG_FILE}"
"/var/runtime/Amazon.Lambda.RuntimeSupport.dll" "${LAMBDA_HANDLER}")
fi

# To support runtime wrapper scripts
# https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper
if [ -z "${AWS_LAMBDA_EXEC_WRAPPER}" ]; then
exec "${DOTNET_BIN}" "${DOTNET_EXEC}" "${DOTNET_ARGS[@]}"
else
if [ ! -f "${AWS_LAMBDA_EXEC_WRAPPER}" ]; then
echo "${AWS_LAMBDA_EXEC_WRAPPER}: does not exist"
exit 127
fi
if [ ! -x "${AWS_LAMBDA_EXEC_WRAPPER}" ]; then
echo "${AWS_LAMBDA_EXEC_WRAPPER}: is not an executable"
exit 126
fi
fi
exec -- "${AWS_LAMBDA_EXEC_WRAPPER}" "${DOTNET_BIN}" "${DOTNET_EXEC}" "${DOTNET_ARGS[@]}"
fi

0 comments on commit 9668ee8

Please sign in to comment.