Skip to content

Commit

Permalink
#169 | Remove unused S3BulkThumbnailCreator script and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
himeshr committed Feb 7, 2024
1 parent 3e5f802 commit 335a1c6
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 108 deletions.
9 changes: 2 additions & 7 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@

- AWS CLI should be installed on the user's machine.

There are two lambda functions inside the scripts directory.
There ~~are two~~ is a lambda function inside the scripts directory.

1. S3ThumbnailCreator
2. ~~S3BulkThumbnailCreator~~ (Un-used)

#### 1. S3ThumbnailCreator

This lambda function generates the thumbnail instantly when an image is uploaded to the S3 bucket.

#### 2. ~~S3BulkThumbnailCreator~~(Un-used)

~~This lambda function creates thumbnails of existing images . When deployed this function runs on nightly to create thumbnails~~

Both the lambda functions will be deployed in a single go. Run the following command:
The lambda functions will be deployed in a single go. Run the following command:

```
./deploy-lambda-functions.sh bucket-name
Expand Down
51 changes: 0 additions & 51 deletions scripts/S3BulkThumbnailCreator/index.mjs

This file was deleted.

50 changes: 0 additions & 50 deletions scripts/deploy-lambda-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ ROLE_NAME="${ENV}-thumbnail-lambda-execution-role"
ROLE_ARN="arn:aws:iam::${ACCOUNT_ID}:role/${ROLE_NAME}"

FUNCTION_NAME="${ENV}-generate-thumbnails-instantly"
FUNCTION_NAME_BULK="${ENV}-generate-thumbnails-bulk"
FUNCTION_ARN="arn:aws:lambda:${AVNI_MEDIA_AWS_REGION}:${ACCOUNT_ID}:function:${FUNCTION_NAME}"
HANDLER="index.handler"
MEMORY_SIZE="1024"
TIMEOUT="20"
TIMEOUT_BULK="300"
STATEMENT_ID="${ENV}-lambda-for-thumbnails"

S3_KEY="${ENV}-generate-thumbnails.zip"
S3_KEY_BULK="${ENV}-generate-thumbnails-bulk.zip"

role_status=""

Expand All @@ -45,17 +42,13 @@ npm install || { echo "npm install failed"; exit 1; }
npm run install-sharp || { echo "npm run install-sharp failed"; exit 1; }

rm -f "${FUNCTION_NAME}.zip"
rm -f "${FUNCTION_NAME_BULK}.zip"

zip -r "${FUNCTION_NAME}.zip" ./S3ThumbnailCreator/index.mjs -j 'S3ThumbnailCreator/*' || { echo "Failed to zip the function code for ${FUNCTION_NAME}"; exit 1; }
zip -r "${FUNCTION_NAME}.zip" node_modules/ || { echo "Failed to zip the node_modules for ${FUNCTION_NAME}"; exit 1; }

zip -r "${FUNCTION_NAME_BULK}.zip" ./S3BulkThumbnailCreator/index.mjs -j 'S3BulkThumbnailCreator/*' || { echo "Failed to zip the function code for ${FUNCTION_NAME_BULK}"; exit 1; }
zip -r "${FUNCTION_NAME_BULK}.zip" node_modules/ || { echo "Failed to zip the nod_modules for ${FUNCTION_NAME_BULK}"; exit 1; }

echo "Uploading the function code to S3"
aws s3 cp "${FUNCTION_NAME}.zip" "s3://${CODE_DEPLOY_BUCKET_NAME}/${S3_KEY}" || { echo "Failed to upload the function code to S3"; exit 1; }
aws s3 cp "${FUNCTION_NAME_BULK}.zip" "s3://${CODE_DEPLOY_BUCKET_NAME}/${S3_KEY_BULK}" || { echo "Failed to upload the bulk function code to S3"; exit 1; }

echo "Creating an IAM role for your Lambda function"
if aws iam get-role --role-name "${ROLE_NAME}" >/dev/null 2>&1; then
Expand Down Expand Up @@ -129,49 +122,6 @@ if ! aws lambda update-function-code \
exit 1
fi

if aws lambda get-function --function-name "${FUNCTION_NAME_BULK}" >/dev/null 2>&1; then
echo "Function ${FUNCTION_NAME_BULK} already exists."
elif ! aws lambda create-function \
--function-name "${FUNCTION_NAME_BULK}" \
--runtime nodejs18.x \
--handler "${HANDLER}" \
--memory-size "${MEMORY_SIZE}" \
--timeout "${TIMEOUT_BULK}" \
--role "${ROLE_ARN}" \
--environment Variables="{BUCKET_NAME=${BUCKET_NAME}}" \
--code "S3Bucket=${CODE_DEPLOY_BUCKET_NAME},S3Key=${S3_KEY_BULK}"; then
echo "Error creating the Lambda function for bulk thumbnail creation."
exit 1
fi

echo "Updating the function code..."
if ! aws lambda update-function-code \
--function-name "${FUNCTION_NAME_BULK}" \
--s3-bucket "${CODE_DEPLOY_BUCKET_NAME}" \
--s3-key "${S3_KEY_BULK}"; then
echo "Error updating the function code for bulk thumbnail creation."
exit 1
fi

###### Disabling scheduled run of bulk lambda for now
#echo "Creating the rule..."
#if ! aws events put-rule \
# --name "daily-thumbnails" \
# --description "Triggers the ${FUNCTION_NAME_BULK} function once a day" \
# --schedule-expression "cron(0 18 * * ? *)" \
# --state "ENABLED"; then
# echo "Error creating the rule."
# exit 1
#fi
#
#echo "Adding a target to the rule..."
#if ! aws events put-targets \
# --rule "daily-thumbnails" \
# --targets "Id"="1","Arn"="arn:aws:lambda:${AVNI_MEDIA_AWS_REGION}:${ACCOUNT_ID}:function:${FUNCTION_NAME_BULK}"; then
# echo "Error adding a target to the rule."
# exit 1
#fi

EXISTING_PERMISSION=$(aws lambda get-policy --function-name $FUNCTION_NAME --output text --query 'Policy' 2>/dev/null | grep -q $STATEMENT_ID && echo "yes" || echo "no")

if [[ $EXISTING_PERMISSION == "yes" ]]; then
Expand Down

0 comments on commit 335a1c6

Please sign in to comment.