Skip to content

CloudFrontToS3 additionalBehaviors default origin to s3BucketInterface #1331

@RecuencoJones

Description

@RecuencoJones

Currently we can add a default behavior which implicitly is attached to the S3 bucket origin

const dist = new CloudFrontToS3(this, 'my-distribution', {
  cloudFrontDistributionProps: {
    defaultBehavior: {
      allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
      viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
    }
  }
});

If we want to add an additional behavior for that same origin, we can't simply do:

const dist = new CloudFrontToS3(this, 'my-distribution', {
  cloudFrontDistributionProps: {
    defaultBehavior: {
      allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
      viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
    },
    additionalBehaviors: {
      'some-path/*': {
        allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
        viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
      }
    }
  }
});

As it will fail with TypeError: Cannot read properties of undefined (reading 'bind')

Instead, we have to do the following:

const dist = new CloudFrontToS3(this, 'my-distribution', {
  cloudFrontDistributionProps: {
    defaultBehavior: {
      allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
      viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
    }
  }
});

dist.cloudFrontWebDistribution.addBehavior(
  'some-path/*',
  S3BucketOrigin.withOriginAccessControl(dist.s3BucketInterface),
  {
    allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
    viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
  }
);

Use Case

I think it's generally useful to be able to add any extra behaviors on the distribution in-place rather than assigning and having to create an extra origin with S3BucketOrigin.withOriginAccessControl(dist.s3BucketInterface)

Proposed Solution

Make it optional in BehaviorOptions and default to s3BucketInterface, users may override with different origins if they need to.

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestA feature should be added or improvedneeds-triageThe issue or PR still needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions