File tree Expand file tree Collapse file tree 4 files changed +87
-0
lines changed
Lambda/Working-with-Lambda-Layers Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Original file line number Diff line number Diff line change 1+ Pipfile
2+ Pipfile.lock
3+ requirements.txt
4+ * .zip
Original file line number Diff line number Diff line change 1+ # Working with Lambda Layers
2+
3+ Create a new virtual environment using ` pipenv ` and install the required libraries:
4+
5+ ``` sh
6+ pipenv --python 3.7
7+ pipenv shell
8+ pipenv install requests
9+ ```
10+
11+ Note: If you are on macOS, you can install ` pipenv ` using Homebrew:
12+
13+ ``` sh
14+ brew install pipenv
15+ ```
16+
17+ on Amazon Linux, or another environment, you can install using ` pip ` :
18+
19+ ``` sh
20+ pip3 install pipenv --user
21+ ```
22+
23+ Create the ZIP deployment package:
24+
25+ ``` sh
26+ PY_DIR=' build/python/lib/python3.7/site-packages'
27+ # Create temporary build directory
28+ mkdir -p $PY_DIR
29+ # Generate requirements file
30+ pipenv lock -r > requirements.txt
31+ # Install packages into the target directory
32+ pip install -r requirements.txt --no-deps -t $PY_DIR
33+ cd build
34+ # Zip files
35+ zip -r ../requests_layer.zip .
36+ cd ..
37+ # Remove temporary directory
38+ rm -r build
39+ ```
40+
41+ Create the Lambda Layer
42+
43+ ``` sh
44+ aws lambda publish-layer-version \
45+ --layer-name requests \
46+ --compatible-runtimes python3.7 \
47+ --zip-file fileb://requests_layer.zip
48+ ```
49+
50+ Note the ` LayerArn ` in the output.
Original file line number Diff line number Diff line change 1+ import pkg_resources
2+
3+
4+ def lambda_handler (event , context ):
5+ pkgs = pkg_resources .working_set
6+ pkgs_list = sorted ([f'{ i .key } =={ i .version } ' for i in pkgs ])
7+ print ("\n " .join (pkgs_list ))
8+
9+ # Log output will resemble:
10+
11+ # START RequestId: 430a0232-8c13-4788-90e0-c24e7ac3d7c4 Version: $LATEST
12+ # boto3==1.9.42
13+ # botocore==1.12.42
14+ # docutils==0.14
15+ # jmespath==0.9.3
16+ # pip==18.1
17+ # python-dateutil==2.7.5
18+ # s3transfer==0.1.13
19+ # setuptools==40.6.2
20+ # six==1.11.0
21+ # urllib3==1.24.1
22+ # END RequestId: 430a0232-8c13-4788-90e0-c24e7ac3d7c4
23+ # REPORT RequestId: 430a0232-8c13-4788-90e0-c24e7ac3d7c4 Duration: 39.76 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 70 MB
Original file line number Diff line number Diff line change 1+ import requests
2+
3+
4+ def lambda_handler (event , context ):
5+
6+ response = requests .get ('https://linuxacademy.com' )
7+ if response :
8+ print ('Success!' )
9+ else :
10+ print ('An error has occurred.' )
You can’t perform that action at this time.
0 commit comments