aws-samples / image-optimization

Simple, performant and cost efficient solution for optimizing images using Amazon CloudFront, Amazon S3 and AWS Lambda
MIT No Attribution
172 stars 106 forks source link

Does it support multiple s3 buckets? #27

Open forwells opened 6 months ago

forwells commented 6 months ago

I have multiple s3 buckets, When I run cdk bootstrap, a cdk s3 bucket is generated. After that, I can cdk deploy -c bucket_name .., but it is only configured for a single bucket at this time. If I want to configure multiple buckets, can I run it multiple times? npx deploy -c

achrafsouk commented 6 months ago

Hello, it depends what do you want to achieve. Why do you need multiple S3 buckets?

alecsvaldez commented 5 months ago

That would be useful, in our case we have several buckets for each one of our websites. The need for a bucket-per-site its because in our bussiness every site corresponds to different areas, and every area has its own cost-center and pay the bills with different budgets (maybe not the best solution, but that implementation was made by our TI team who manage AWS accounts time ago). By now, we try making multiple deployments for every site/bucket...

achrafsouk commented 5 months ago

If every site has a different ownership/ content / domain name, then it makes sense to deploy the solution separately for each website. Note that I haven't tested multiple deployments in the same AWS Account, it might require some code change to make it work.

YashAtwal commented 1 week ago

@alecsvaldez What all would we need to change? I am trying to deploy the solution for two different buckets for a similar use case. I changed the name of urlRewrite function, responseHeadersPolicyName, OAC and the stack.

Both buckets have public access blocked so I added permissions allowing the cloudfront distribution to access the s3 bucket objects and the optimisation works fine for the first bucket.

But after deploying the new stack it's not able to access the images from 2nd the s3 bucket, on checking cloudwatch logs it throwing access denied error.

Error for reference -

2024-07-09T11:25:48.031Z    " " INFO    AccessDenied: Access Denied
    at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)
    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5
    at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-s3/dist-cjs/index.js:4756:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20
    at async /var/runtime/node_modules/@aws-sdk/middleware-signing/dist-cjs/index.js:225:18
    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
    at async /var/runtime/node_modules/@aws-sdk/middleware-flexible-checksums/dist-cjs/index.js:173:18
    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:97:20
    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:120:14 {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 403,
    requestId: '',
    extendedRequestId: '',
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Code: 'AccessDenied',
  RequestId: '',
  HostId: ''
}
mahersalhani commented 1 day ago

You can go to /bin/image-optimization.ts file and add new stack

#!/usr/bin/env node
import "source-map-support/register";
import * as cdk from "aws-cdk-lib";
import { ImageOptimizationStack } from "../lib/image-optimization-stack";

const app = new cdk.App();

// Production stack
new ImageOptimizationStack(app, "ImgTransformationStackProd", {});

// Dev stack
new ImageOptimizationStack(app, "ImgTransformationStackDev", {});

now you can deploy the stack you want

cdk deploy ImgTransformationStackProd