Skip to content

Commit 2a77894

Browse files
authored
Merge pull request #5 from davidecoccia/main
Fixes zbar library compiling after CloudShell migration to Amazon Linux 2023
2 parents 5898292 + 9f615d3 commit 2a77894

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

README.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,33 @@ With this Lambda Function you will be able to add decoding features to your appl
1313

1414
#### Step 2, Generate code artifacts and dependencies
1515
To read QR/Barcodes, we are going to be using the [Zbar library](https://github.com/mchehab/zbar), an open source software suite for reading bar codes. We are going to include Zbar and other necessary packages into Lambda Layers for our Lambda function to work.
16-
But don't worry, we have already automated this process for you, in a simple script you can run in your AWS Cloudshell! Here are the steps you have to follow:
16+
But don't worry, we have already automated this process for you, in a simple script you can run in your AWS Cloud9! Here are the steps you have to follow:
1717

18-
* Access AWS CloudShell, a browser-based shell inside the AWS console. You can click the terminal icon next to the search bar or looking for _Cloudshell_ in the service search bar.
18+
* Login into you AWS Account and access AWS Cloud9 by navigating to https://console.aws.amazon.com/cloud9control/home#/
1919

20-
![CloudShell](src/img/step-0_1.png)
21-
* Once CloudShell has initiallized, clone this repo
22-
* `git clone https://github.com/aws-samples/Barcode-QR-Decoder-Lambda.git`
20+
* Click on "Create environment"
21+
22+
* Provide a name for your environment, select an instance from the t2 or t3 family and make sure to choose "Amazon Linux 2" as platform. This guarantees the correct installation of the zbar library
23+
24+
![Cloud9Setup1](src/img/cloud9_step-1.png)
25+
26+
* We recommend to choose "AWS Systems Manager(SSM)" in the network settings as it won't require you to open any inbound port to the EC2 instance. Do not change setting in the "VPC Settings" sections unless you need to. Finally, create the environment by clicking "Create"
27+
28+
![Cloud9Setup2](src/img/cloud9_step-2.png)
29+
30+
* Once your Cloud9 environment is created, open it and create a new terminal
31+
32+
![Cloud9Setup3](src/img/cloud9_step-3.png)
33+
34+
* Now clone this repo
35+
* `git clone https://github.com/aws-samples/barcode-qr-decoder-lambda.git`
2336

2437

2538
* Run the `setup.sh` script in order to generate the needed lambda layers and code package. You must specify the bucket where you want to upload this artifacts replacing <BUCKET_NAME> with the S3 bucket name you created.
26-
* `sh Barcode-QR-Decoder-Lambda/src/code/setup.sh -b <BUCKET_NAME>`
39+
* `sh barcode-qr-decoder-lambda/src/code/setup.sh -b <BUCKET_NAME>`
2740

2841

29-
* Once the script finishes, you should see 3 new files in your S3 bucket under `BarcodeQRDecoder/qr-reader/assets/` path, the two Lambda layers containing the libraries needed (Pillow and Pyzbar) and the lambda code packaged in a .zip file
42+
* Once the script finishes, you should see 2 new files in your S3 bucket under `BarcodeQRDecoder/qr-reader/assets/` path, the Lambda layer containing the libraries needed (Pillow and Pyzbar) and the lambda code packaged in a .zip file
3043

3144
![S3Files](src/img/step-0_2.png)
3245

@@ -35,28 +48,26 @@ But don't worry, we have already automated this process for you, in a simple scr
3548
* Create a new Lambda Function.
3649
* Select Author from scratch.
3750
* Input a new name for your function
38-
* Select Python 3.7 as runtime
51+
* Select Python 3.9 as runtime
3952
* Select x86_64 as architecture
4053
* Create a new role with basic Lambda permissions
41-
* Replace the code with Python code inside `code/lambda.py`
54+
* Replace the code with Python code [available in this repository](src/code/lambda_function.py)
4255

4356
You have now created the Lambda function!
4457

4558
#### Step 4, Add Layers to your Lambda function
46-
As we mentioned before, your function needs some packages to run correctly. If you completed step 2, you should have the layers artifacts ready in your bucket!
47-
Follow these steps to create your layers:
59+
As we mentioned before, your function needs some packages to run correctly. If you completed step 2, you should have the layer artifact ready in your bucket!
60+
Follow these steps to create your layer:
4861
- Open the Layers page of the Lambda console.
4962
- Choose Create layer.
5063
- Under Layer configuration, for Name, enter a name for your layer.
5164
- (Optional) For Description, enter a description for your layer.
5265
- To upload a file from Amazon S3, choose Upload a file from Amazon S3. Then, for Amazon S3 link URL, enter the S3 URI of the artifact.
5366
- For Compatible architectures, choose x86_64.
54-
- For Compatible runtimes, choose Python 3.7.
67+
- For Compatible runtimes, choose Python 3.9.
5568
- Choose Create.
5669

57-
Repeat these steps for both artifacts created in S3.
58-
59-
Next, go to the Lambda function and in your layers section, select Add Layer. Select your layers which will be available at the Custom AWS layers dropdown.
70+
Next, go to the Lambda function and in your layers section, select Add Layer. Select your layer which will be available at the Custom AWS layers dropdown.
6071

6172
#### Step 5, Configure the permissions needed
6273
Head over to IAM and add permissions to your associated role to access your S3 Bucket.

src/code/setup.sh

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,47 @@ if [ -z "$BUCKET_NAME" ]
1414
exit 0
1515
fi
1616

17+
#download python 3.9
18+
wget https://www.python.org/ftp/python/3.9.18/Python-3.9.18.tgz
19+
tar xvf Python-3.9.18.tgz && cd Python-3.9.18
20+
./configure --enable-optimizations && sudo make altinstall && cd ..
21+
22+
#create and activate python venv
23+
python3.9 -m venv .venv && source .venv/bin/activate
24+
1725
#set variables. Replace <BUCKET_NAME> with the name of your S3 bucket
18-
LAYER_FOLDER_TREE=python/lib/python3.7/site-packages
26+
LAYER_FOLDER_TREE=python/lib/python3.9/site-packages
1927

2028
#download and zip pillow layer
2129
mkdir -p $LAYER_FOLDER_TREE
22-
pip3 install pillow -t $LAYER_FOLDER_TREE
23-
zip -r pillow_layer.zip python && rm -r python
30+
pip install pillow -t $LAYER_FOLDER_TREE
31+
#zip -r pillow_layer.zip python && rm -r python
2432

2533
#download pyzbar layer
26-
mkdir -p $LAYER_FOLDER_TREE
27-
pip3 install pyzbar -t $LAYER_FOLDER_TREE
34+
#mkdir -p $LAYER_FOLDER_TREE
35+
pip install pyzbar -t $LAYER_FOLDER_TREE
2836

2937
#get shared library (libzbar.so) needed for pyzbar to work properly within the Lambda function
3038
#compiling zbar to obtain libzbar.so
3139
sudo yum install -y autoconf autopoint gettext-devel automake pkgconfig libtool
3240
git clone https://github.com/mchehab/zbar.git
3341
cd zbar/
3442
autoreconf -vfi
35-
./configure && make && cd
43+
./configure --with-gtk=auto --with-python=auto && make && cd ..
3644

3745
#copy library to layer folder and replace libzbar.so path inside zbar_library.py to correctly load the library. Lambda layers (.zips) will be uploaded to S3
3846
cp zbar/zbar/.libs/libzbar.so.0.3.0 $LAYER_FOLDER_TREE/pyzbar/libzbar.so
39-
sed -i "s/find_library('zbar')/('\/opt\/python\/lib\/python3.7\/site-packages\/pyzbar\/libzbar.so')/g" $LAYER_FOLDER_TREE/pyzbar/zbar_library.py
40-
zip -r pyzbar_layer.zip python && rm -rf python && rm -rf zbar
47+
sed -i "s/find_library('zbar')/('\/opt\/python\/lib\/python3.9\/site-packages\/pyzbar\/libzbar.so')/g" $LAYER_FOLDER_TREE/pyzbar/zbar_library.py
48+
zip -r barcode_layer_py39.zip python && rm -rf python && rm -rf zbar
4149

4250
#package lambda function code in a .zip
43-
zip -r lambda_function.zip Barcode-QR-Decoder-Lambda/src/code/lambda_function.zip
44-
aws s3 sync . s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets --exclude="*" --include="*layer.zip" --include="lambda_function.zip"
51+
zip -r lambda_function.zip barcode-qr-decoder-lambda/src/code/lambda_function.py
52+
#aws s3 sync . s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets --exclude="*" --exclude=".c9*" --include="*layer.zip" --include="lambda_function.zip"
53+
#aws s3 sync . s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets --include="*.zip"
54+
55+
aws s3 cp barcode_layer_py39.zip s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets/
56+
aws s3 cp lambda_function.zip s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets/
57+
4558

4659
#delete generated lambda layers after uploaded to S3 to clean curent directory
47-
rm pillow_layer.zip pyzbar_layer.zip lambda_function.zip
60+
#rm pillow_layer.zip pyzbar_layer.zip lambda_function.zip

src/img/cloud9_step-1.png

175 KB
Loading

src/img/cloud9_step-2.png

172 KB
Loading

src/img/cloud9_step-3.png

307 KB
Loading

src/img/step-0_2.png

75.7 KB
Loading

0 commit comments

Comments
 (0)