Skip to content

Commit

Permalink
chore(lambda-layer-awscli): add update mechanism for AWS CLI
Browse files Browse the repository at this point in the history
We bundle the AWS CLI. This adds a mechanism to keep up with new updates
of the CLI. It adds a script that should be executed periodically to
create a PR with a version update.

We had to install and bump to Python v3 because newer versions of the
AWS CLI have ended support for Python 2.7.
  • Loading branch information
rix0rrr committed Feb 2, 2022
1 parent 17dbe5f commit e295db2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/lambda-layer-awscli/awscli.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.22.46
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -eu
scriptdir=$(cd $(dirname $0) && pwd)

package=awscli
tmpfile=_pip.json

curl -LsSf https://pypi.org/pypi/awscli/json > $tmpfile
trap "rm $tmpfile" EXIT
version=$(node -p "require('./${tmpfile}').info.version")

if [[ $version != 1.* ]]; then
echo "Expected version 1.*, got ${version}" >&2
exit 1
fi

echo "AWS CLI is currently at ${version}"

echo $version > $scriptdir/../awscli.version
31 changes: 17 additions & 14 deletions packages/@aws-cdk/lambda-layer-awscli/layer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
FROM public.ecr.aws/lambda/provided:latest

#
# versions
#

# This is the last version that still supports Python 2.7
ARG AWSCLI_VERSION=1.19.112

USER root
RUN mkdir -p /opt
WORKDIR /tmp
Expand All @@ -16,21 +9,24 @@ WORKDIR /tmp
#

RUN yum update -y \
&& yum install -y zip unzip wget tar gzip
&& yum install -y zip unzip wget tar gzip python3

#
# aws cli
#

ARG AWSCLI_VERSION=0.0.0

RUN curl https://s3.amazonaws.com/aws-cli/awscli-bundle-${AWSCLI_VERSION}.zip -o awscli-bundle.zip
RUN unzip awscli-bundle.zip
RUN ./awscli-bundle/install -i /opt/awscli -b /opt/awscli/aws
RUN python3 ./awscli-bundle/install -i /opt/awscli -b /opt/awscli/aws

# organize for self-contained usage
RUN mv /opt/awscli /opt/awscli.tmp
RUN mv /opt/awscli.tmp/lib/python2.7/site-packages /opt/awscli
RUN mv /opt/awscli.tmp/bin /opt/awscli/bin
RUN mv /opt/awscli/bin/aws /opt/awscli
RUN mv /opt/awscli /opt/awscli.tmp
RUN pyver=$(python3 -c 'import sys; v = sys.version_info; print(f"{v[0]}.{v[1]}")') && \
mv /opt/awscli.tmp/lib/python${pyver}/site-packages /opt/awscli
RUN mv /opt/awscli.tmp/bin /opt/awscli/bin
RUN mv /opt/awscli/bin/aws /opt/awscli

# cleanup
RUN rm -fr /opt/awscli.tmp
Expand All @@ -39,6 +35,13 @@ RUN rm -rf \
/opt/awscli/setuptools* \
/opt/awscli/awscli/examples

#
# Test that the CLI works
#

RUN yum install -y groff
RUN /opt/awscli/aws help

#
# create the bundle
#
Expand All @@ -49,4 +52,4 @@ RUN cd /opt \
&& ls -alh /layer.zip;

WORKDIR /
ENTRYPOINT [ "/bin/bash" ]
ENTRYPOINT [ "/bin/bash" ]
6 changes: 4 additions & 2 deletions packages/@aws-cdk/lambda-layer-awscli/layer/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ set -euo pipefail

cd $(dirname $0)

echo ">> Building AWS Lambda layer inside a docker image..."
version=$(cat ../awscli.version)

echo ">> Building AWS Lambda layer inside a docker image for CLI version ${version}..."

TAG='aws-lambda-layer'

docker build -t ${TAG} .
docker build -t ${TAG} . --build-arg AWSCLI_VERSION=${version}

echo ">> Extrating layer.zip from the build container..."
CONTAINER=$(docker run -d ${TAG} false)
Expand Down

0 comments on commit e295db2

Please sign in to comment.