Skip to content

Commit 8cdc2d6

Browse files
authored
chore(lambda-layer-awscli): install awscli with pip and requirements.txt (#18800)
We are currently bundling the AWS CLI in the Lambda Layer and need a mechanism to automatically upgrade when new versions are released. This changes the way we bundle the AWS CLI, from using the install script to using pip to install from a `requirements.txt` file. This will enable dependabot to auto upgrade the version of the awscli specified in requirements.txt Also added integration tests to ensure that the AWS CLI is executable from within various Python runtimes. The `assetHash` has been updated to fingerprint the entire `layer` directory since we want to deploy a new version anytime anything in that directory changes, not just the Dockerfile. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent bb8d6f6 commit 8cdc2d6

File tree

11 files changed

+832
-48
lines changed

11 files changed

+832
-48
lines changed

packages/@aws-cdk/lambda-layer-awscli/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
This module exports a single class called `AwsCliLayer` which is a `lambda.Layer` that bundles the AWS CLI.
1414

15+
Any Lambda Function that uses this layer must use a Python 3.x runtime.
16+
1517
Usage:
1618

1719
```ts

packages/@aws-cdk/lambda-layer-awscli/awscli.version

-1
This file was deleted.

packages/@aws-cdk/lambda-layer-awscli/build-tools/bump-awscli-version.sh

-19
This file was deleted.

packages/@aws-cdk/lambda-layer-awscli/layer/Dockerfile

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM public.ecr.aws/lambda/provided:latest
1+
FROM public.ecr.aws/sam/build-python3.7
22

33
USER root
44
RUN mkdir -p /opt
@@ -9,27 +9,19 @@ WORKDIR /tmp
99
#
1010

1111
RUN yum update -y \
12-
&& yum install -y zip unzip wget tar gzip python3
12+
&& yum install -y zip unzip wget tar gzip
1313

1414
#
1515
# aws cli
1616
#
1717

18-
ARG AWSCLI_VERSION=0.0.0
19-
20-
RUN curl https://s3.amazonaws.com/aws-cli/awscli-bundle-${AWSCLI_VERSION}.zip -o awscli-bundle.zip
21-
RUN unzip awscli-bundle.zip
22-
RUN python3 ./awscli-bundle/install -i /opt/awscli -b /opt/awscli/aws
18+
COPY requirements.txt ./
19+
RUN python -m pip install -r requirements.txt -t /opt/awscli
2320

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

3124
# cleanup
32-
RUN rm -fr /opt/awscli.tmp
3325
RUN rm -rf \
3426
/opt/awscli/pip* \
3527
/opt/awscli/setuptools* \

packages/@aws-cdk/lambda-layer-awscli/layer/build.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ set -euo pipefail
33

44
cd $(dirname $0)
55

6-
version=$(cat ../awscli.version)
7-
8-
echo ">> Building AWS Lambda layer inside a docker image for CLI version ${version}..."
6+
echo ">> Building AWS Lambda layer inside a docker image..."
97

108
TAG='aws-lambda-layer'
119

12-
docker build -t ${TAG} . --build-arg AWSCLI_VERSION=${version}
10+
docker build -t ${TAG} .
1311

1412
echo ">> Extrating layer.zip from the build container..."
1513
CONTAINER=$(docker run -d ${TAG} false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
awscli==1.22.46
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as crypto from 'crypto';
2-
import * as fs from 'fs';
31
import * as path from 'path';
42
import * as lambda from '@aws-cdk/aws-lambda';
3+
import { FileSystem } from '@aws-cdk/core';
54
import { Construct } from 'constructs';
65

76
/**
@@ -11,17 +10,10 @@ export class AwsCliLayer extends lambda.LayerVersion {
1110
constructor(scope: Construct, id: string) {
1211
super(scope, id, {
1312
code: lambda.Code.fromAsset(path.join(__dirname, 'layer.zip'), {
14-
// we hash the Dockerfile (it contains the tools versions) because hashing the zip is non-deterministic
15-
assetHash: hashFile(path.join(__dirname, '..', 'layer', 'Dockerfile')),
13+
// we hash the layer directory (it contains the tools versions and Dockerfile) because hashing the zip is non-deterministic
14+
assetHash: FileSystem.fingerprint(path.join(__dirname, '../layer')),
1615
}),
1716
description: '/opt/awscli/aws',
1817
});
1918
}
2019
}
21-
22-
function hashFile(fileName: string) {
23-
return crypto
24-
.createHash('sha256')
25-
.update(fs.readFileSync(fileName))
26-
.digest('hex');
27-
}

packages/@aws-cdk/lambda-layer-awscli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"license": "Apache-2.0",
7575
"devDependencies": {
7676
"@aws-cdk/assertions": "0.0.0",
77+
"@aws-cdk/custom-resources": "0.0.0",
7778
"@aws-cdk/cdk-build-tools": "0.0.0",
7879
"@aws-cdk/cdk-integ-tools": "0.0.0",
7980
"@aws-cdk/pkglint": "0.0.0",

0 commit comments

Comments
 (0)