class BucketPolicy (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.S3.BucketPolicy |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awss3#BucketPolicy |
Java | software.amazon.awscdk.services.s3.BucketPolicy |
Python | aws_cdk.aws_s3.BucketPolicy |
TypeScript (source) | aws-cdk-lib » aws_s3 » BucketPolicy |
Implements
IConstruct
, IDependable
, IResource
The bucket policy for an Amazon S3 bucket.
Policies define the operations that are allowed on this resource.
You almost never need to define this construct directly.
All AWS resources that support resource policies have a method called
addToResourcePolicy()
, which will automatically create a new resource
policy if one doesn't exist yet, otherwise it will add to the existing
policy.
The bucket policy method is implemented differently than addToResourcePolicy()
as BucketPolicy()
creates a new policy without knowing one earlier existed.
e.g. if during Bucket creation, if autoDeleteObject:true
, these policies are
added to the bucket policy:
["s3:DeleteObject", "s3:GetBucket", "s3:List*", "s3:PutBucketPolicy"],
and when you add a new BucketPolicy with ["s3:GetObject", "s3:ListBucket"] on
this existing bucket, invoking BucketPolicy()
will create a new Policy
without knowing one earlier exists already, so it creates a new one.
In this case, the custom resource handler will not have access to
s3:GetBucketTagging
action which will cause failure during deletion of stack.
Hence its strongly recommended to use addToResourcePolicy()
method to add
new permissions to existing policy.
Example
const bucketName = "amzn-s3-demo-bucket";
const accessLogsBucket = new s3.Bucket(this, 'AccessLogsBucket', {
objectOwnership: s3.ObjectOwnership.BUCKET_OWNER_ENFORCED,
bucketName,
});
const bucketPolicy = new s3.CfnBucketPolicy(this, "BucketPolicy", {
bucket: bucketName,
policyDocument: {
Statement: [
{
Action: 's3:*',
Effect: 'Deny',
Principal: {
AWS: '*',
},
Resource: [
accessLogsBucket.bucketArn,
`${accessLogsBucket.bucketArn}/*`
],
},
],
Version: '2012-10-17',
},
});
// Wrap L1 Construct with L2 Bucket Policy Construct. Subsequent
// generated bucket policy to allow access log delivery would append
// to the current policy.
s3.BucketPolicy.fromCfnBucketPolicy(bucketPolicy);
const bucket = new s3.Bucket(this, 'MyBucket', {
serverAccessLogsBucket: accessLogsBucket,
serverAccessLogsPrefix: 'logs',
});
Initializer
new BucketPolicy(scope: Construct, id: string, props: BucketPolicyProps)
Parameters
- scope
Construct
- id
string
- props
Bucket
Policy Props
Construct Props
Name | Type | Description |
---|---|---|
bucket | IBucket | The Amazon S3 bucket that the policy applies to. |
removal | Removal | Policy to apply when the policy is removed from this stack. |
bucket
Type:
IBucket
The Amazon S3 bucket that the policy applies to.
removalPolicy?
Type:
Removal
(optional, default: RemovalPolicy.DESTROY.)
Policy to apply when the policy is removed from this stack.
Properties
Name | Type | Description |
---|---|---|
bucket | IBucket | The Bucket this Policy applies to. |
document | Policy | A policy document containing permissions to add to the specified bucket. |
env | Resource | The environment this resource belongs to. |
node | Node | The tree node. |
stack | Stack | The stack in which this resource is defined. |
bucket
Type:
IBucket
The Bucket this Policy applies to.
document
Type:
Policy
A policy document containing permissions to add to the specified bucket.
For more information, see Access Policy Language Overview in the Amazon Simple Storage Service Developer Guide.
env
Type:
Resource
The environment this resource belongs to.
For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.
node
Type:
Node
The tree node.
stack
Type:
Stack
The stack in which this resource is defined.
Methods
Name | Description |
---|---|
apply | Sets the removal policy for the BucketPolicy. |
to | Returns a string representation of this construct. |
static from | Create a mutable BucketPolicy from a CfnBucketPolicy . |
RemovalPolicy(removalPolicy)
applypublic applyRemovalPolicy(removalPolicy: RemovalPolicy): void
Parameters
- removalPolicy
Removal
— the RemovalPolicy to set.Policy
Sets the removal policy for the BucketPolicy.
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.
CfnBucketPolicy(cfnBucketPolicy)
static frompublic static fromCfnBucketPolicy(cfnBucketPolicy: CfnBucketPolicy): BucketPolicy
Parameters
- cfnBucketPolicy
Cfn
Bucket Policy
Returns
Create a mutable BucketPolicy
from a CfnBucketPolicy
.