aws / aws/aws-cdk

aws_s3: Implement requester pays

Open
#31,031 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-s3 effort/small feature-request needs-cfn p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### 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.

```javascript
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](https://docs.aws.amazon.com/AmazonS3/latest/userguide/RequesterPaysExamples.html) 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

Contributor guide

Open the contributing guide

Research direction

Start by locating BucketProperty in the S3 module and compare the requested property with the AWS requester-pays API described in the linked docs. Review the proposed lambda/set-requyester-pays.js and custom-resource entry points to determine whether a custom resource is needed; done means a declarative bucket option reliably enables requester pays.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.