Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,33 @@ With this Lambda Function you will be able to add decoding features to your appl

#### Step 2, Generate code artifacts and dependencies
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.
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:
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:

* 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.
* Login into you AWS Account and access AWS Cloud9 by navigating to https://console.aws.amazon.com/cloud9control/home#/

![CloudShell](src/img/step-0_1.png)
* Once CloudShell has initiallized, clone this repo
* `git clone https://github.com/aws-samples/Barcode-QR-Decoder-Lambda.git`
* Click on "Create environment"

* 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

![Cloud9Setup1](src/img/cloud9_step-1.png)

* 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"

![Cloud9Setup2](src/img/cloud9_step-2.png)

* Once your Cloud9 environment is created, open it and create a new terminal

![Cloud9Setup3](src/img/cloud9_step-3.png)

* Now clone this repo
* `git clone https://github.com/aws-samples/barcode-qr-decoder-lambda.git`


* 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.
* `sh Barcode-QR-Decoder-Lambda/src/code/setup.sh -b <BUCKET_NAME>`
* `sh barcode-qr-decoder-lambda/src/code/setup.sh -b <BUCKET_NAME>`


* 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
* 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

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

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

You have now created the Lambda function!

#### Step 4, Add Layers to your Lambda function
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!
Follow these steps to create your layers:
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!
Follow these steps to create your layer:
- Open the Layers page of the Lambda console.
- Choose Create layer.
- Under Layer configuration, for Name, enter a name for your layer.
- (Optional) For Description, enter a description for your layer.
- 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.
- For Compatible architectures, choose x86_64.
- For Compatible runtimes, choose Python 3.7.
- For Compatible runtimes, choose Python 3.9.
- Choose Create.

Repeat these steps for both artifacts created in S3.

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.
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.

#### Step 5, Configure the permissions needed
Head over to IAM and add permissions to your associated role to access your S3 Bucket.
Expand Down
35 changes: 24 additions & 11 deletions src/code/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,47 @@ if [ -z "$BUCKET_NAME" ]
exit 0
fi

#download python 3.9
wget https://www.python.org/ftp/python/3.9.18/Python-3.9.18.tgz
tar xvf Python-3.9.18.tgz && cd Python-3.9.18
./configure --enable-optimizations && sudo make altinstall && cd ..

#create and activate python venv
python3.9 -m venv .venv && source .venv/bin/activate

#set variables. Replace <BUCKET_NAME> with the name of your S3 bucket
LAYER_FOLDER_TREE=python/lib/python3.7/site-packages
LAYER_FOLDER_TREE=python/lib/python3.9/site-packages

#download and zip pillow layer
mkdir -p $LAYER_FOLDER_TREE
pip3 install pillow -t $LAYER_FOLDER_TREE
zip -r pillow_layer.zip python && rm -r python
pip install pillow -t $LAYER_FOLDER_TREE
#zip -r pillow_layer.zip python && rm -r python

#download pyzbar layer
mkdir -p $LAYER_FOLDER_TREE
pip3 install pyzbar -t $LAYER_FOLDER_TREE
#mkdir -p $LAYER_FOLDER_TREE
pip install pyzbar -t $LAYER_FOLDER_TREE

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

#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
cp zbar/zbar/.libs/libzbar.so.0.3.0 $LAYER_FOLDER_TREE/pyzbar/libzbar.so
sed -i "s/find_library('zbar')/('\/opt\/python\/lib\/python3.7\/site-packages\/pyzbar\/libzbar.so')/g" $LAYER_FOLDER_TREE/pyzbar/zbar_library.py
zip -r pyzbar_layer.zip python && rm -rf python && rm -rf zbar
sed -i "s/find_library('zbar')/('\/opt\/python\/lib\/python3.9\/site-packages\/pyzbar\/libzbar.so')/g" $LAYER_FOLDER_TREE/pyzbar/zbar_library.py
zip -r barcode_layer_py39.zip python && rm -rf python && rm -rf zbar

#package lambda function code in a .zip
zip -r lambda_function.zip Barcode-QR-Decoder-Lambda/src/code/lambda_function.zip
aws s3 sync . s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets --exclude="*" --include="*layer.zip" --include="lambda_function.zip"
zip -r lambda_function.zip barcode-qr-decoder-lambda/src/code/lambda_function.py
#aws s3 sync . s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets --exclude="*" --exclude=".c9*" --include="*layer.zip" --include="lambda_function.zip"
#aws s3 sync . s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets --include="*.zip"

aws s3 cp barcode_layer_py39.zip s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets/
aws s3 cp lambda_function.zip s3://$BUCKET_NAME/BarcodeQRDecoder/qr-reader/assets/


#delete generated lambda layers after uploaded to S3 to clean curent directory
rm pillow_layer.zip pyzbar_layer.zip lambda_function.zip
#rm pillow_layer.zip pyzbar_layer.zip lambda_function.zip
Binary file added src/img/cloud9_step-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/img/cloud9_step-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/img/cloud9_step-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/img/step-0_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.