Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_s3: Implement requester pays #31031

Open
2 tasks
giusedroid opened this issue Aug 6, 2024 · 3 comments
Open
2 tasks

aws_s3: Implement requester pays #31031

giusedroid opened this issue Aug 6, 2024 · 3 comments
Labels
@aws-cdk/aws-s3 Related to Amazon S3 effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed. p2

Comments

@giusedroid
Copy link

Describe the feature

Enable S3 requester pays

Enabling s3 requester pays as a property of BucketProperty would help customers managing the state of their payment options directly from code.

    const bucket = new s3.Bucket(this, 'RequesterPaysBucket', {
      //requester pays
      paymentRequests: true, // or something like that
    });

Use Case

As of now, I could not find a way to declaratively set this option on a bucket.

Proposed Solution

there's no CloudFormation equivalent option on AWS::S3::Bucket, so this may not be an easy fix. Here's a link to the docs where they show how to implement this via an HTTP request.

Potentially this could be implemented as a custom resource depending on the bucket

// lambda/set-requyester-pays.js
const { S3Client, PutBucketRequestPaymentCommand } = require('@aws-sdk/client-s3');

exports.handler = async (event) => {
    const bucketName = event.ResourceProperties.BucketName;

    if (!bucketName) {
        return {
            status: 'FAILED',
            reason: 'Bucket name is required',
        };
    }

    const s3Client = new S3Client({ region: process.env.AWS_REGION });

    const params = {
        Bucket: bucketName,
        RequestPaymentConfiguration: {
            Payer: 'Requester'
        }
    };

    try {
        await s3Client.send(new PutBucketRequestPaymentCommand(params));
        return {
            status: 'SUCCESS',
            reason: 'Requester pays enabled successfully',
        };
    } catch (error) {
        return {
            status: 'FAILED',
            reason: `Error enabling requester pays: ${error.message}`,
        };
    }
};
// within a stack
// Custom Resource Lambda to set requester pays
    const setRequesterPaysLambda = new lambda.Function(this, 'SetRequesterPaysLambda', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'set-requester-pays.handler',
      code: lambda.Code.fromAsset(path.join(__dirname, '../lambda'))
    });

    // Custom Resource to trigger the lambda
    const customResourceProvider = new Provider(this, 'CustomResourceProvider', {
      onEventHandler: setRequesterPaysLambda,
    });

    new CustomResource(this, 'SetRequesterPaysCustomResource', {
      serviceToken: customResourceProvider.serviceToken,
      properties: {
        BucketName: bucket.bucketName,
      },
    });

Other Information

No response

Acknowledgements

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

CDK version used

2.151.0

Environment details (OS name and version, etc.)

mac os

@giusedroid giusedroid added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 6, 2024
@github-actions github-actions bot added the @aws-cdk/aws-s3 Related to Amazon S3 label Aug 6, 2024
@giusedroid
Copy link
Author

@giusedroid
Copy link
Author

this solution is even better: aws-cloudformation/cloudformation-coverage-roadmap#123 (comment)

@ashishdhingra ashishdhingra self-assigned this Aug 6, 2024
@ashishdhingra ashishdhingra added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Aug 6, 2024
@ashishdhingra
Copy link
Contributor

@giusedroid Good morning. As rightly pointed out by you, there is an open issue aws-cloudformation/cloudformation-coverage-roadmap#123 in CloudFormation repository. The constructs in CDK are limited by the functionality provided by CloudFormation. Once CloudFormation specification supports RequesterPays property, it would be available as part of CDK CfnBucket L1 construct. The workaround to use custom resource as suggested in aws-cloudformation/cloudformation-coverage-roadmap#123 (comment) is the way to go forward as of now.

Thanks,
Ashish

@ashishdhingra ashishdhingra added p2 effort/small Small work item – less than a day of effort needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Aug 6, 2024
@ashishdhingra ashishdhingra removed their assignment Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-s3 Related to Amazon S3 effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed. p2
Projects
None yet
Development

No branches or pull requests

2 participants