diff --git a/scripts/README.md b/scripts/README.md index d48e335..b31a0be 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -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 diff --git a/scripts/S3BulkThumbnailCreator/index.mjs b/scripts/S3BulkThumbnailCreator/index.mjs deleted file mode 100644 index d7dd009..0000000 --- a/scripts/S3BulkThumbnailCreator/index.mjs +++ /dev/null @@ -1,51 +0,0 @@ -import AWS from 'aws-sdk'; -import sharp from 'sharp'; - -const s3 = new AWS.S3(); - -export const handler = async (event, context) => { - const bucketName = process.env.BUCKET_NAME; - - try { - const batchSize = 100; - let continuationToken = null; - - do { - const response = await s3.listObjectsV2({ - Bucket: bucketName, - ContinuationToken: continuationToken, - MaxKeys: batchSize - }).promise(); - - const objects = response.Contents; - for (const object of objects) { - if (object.Key.match(/\.(jpe?g|png)$/i)) { - let thumbnailKey; - try { - const [organisationName, fileName] = object.Key.split('/'); - const s3Object = await s3.getObject({ Bucket: bucketName, Key: decodeURIComponent(object.Key.replace(/\+/g, " ")) }).promise(); - - thumbnailKey = `${decodeURIComponent(organisationName.replace(/\+/g, " "))}/thumbnails/${decodeURIComponent(fileName.replace(/\+/g, " "))}`; - - if (!fileName.includes('thumbnails')) { - const thumbnail = await sharp(s3Object.Body).resize(200, 200).toBuffer(); - const thumbnailParams = { - Bucket: bucketName, - Key: thumbnailKey, - Body: thumbnail, - ContentType: 'image/jpeg', - }; - await s3.putObject(thumbnailParams).promise(); - console.log(`Thumbnail created: ${thumbnailKey}`); - } - } catch (err) { - console.error(`Thumbnail error: ${thumbnailKey} ${err}`); - } - } - } - continuationToken = response.NextContinuationToken; - } while (continuationToken); - } catch (err) { - console.log(err); - } -}; diff --git a/scripts/deploy-lambda-functions.sh b/scripts/deploy-lambda-functions.sh index 64d76ad..7cf0068 100755 --- a/scripts/deploy-lambda-functions.sh +++ b/scripts/deploy-lambda-functions.sh @@ -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="" @@ -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 @@ -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