BucketPolicy L2 construct
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',
});
bucket_name = "amzn-s3-demo-bucket"
access_logs_bucket = s3.Bucket(self, "AccessLogsBucket",
object_ownership=s3.ObjectOwnership.BUCKET_OWNER_ENFORCED,
bucket_name=bucket_name
)
bucket_policy = s3.CfnBucketPolicy(self, "BucketPolicy",
bucket=bucket_name,
policy_document={
"Statement": [{
"Action": "s3:*",
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Resource": [access_logs_bucket.bucket_arn, f"{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.from_cfn_bucket_policy(bucket_policy)
bucket = s3.Bucket(self, "MyBucket",
server_access_logs_bucket=access_logs_bucket,
server_access_logs_prefix="logs"
)
String bucketName = "amzn-s3-demo-bucket";
Bucket accessLogsBucket = Bucket.Builder.create(this, "AccessLogsBucket")
.objectOwnership(ObjectOwnership.BUCKET_OWNER_ENFORCED)
.bucketName(bucketName)
.build();
CfnBucketPolicy bucketPolicy = CfnBucketPolicy.Builder.create(this, "BucketPolicy")
.bucket(bucketName)
.policyDocument(Map.of(
"Statement", List.of(Map.of(
"Action", "s3:*",
"Effect", "Deny",
"Principal", Map.of(
"AWS", "*"),
"Resource", List.of(accessLogsBucket.getBucketArn(), String.format("%s/*", accessLogsBucket.getBucketArn())))),
"Version", "2012-10-17"))
.build();
// Wrap L1 Construct with L2 Bucket Policy Construct. Subsequent
// generated bucket policy to allow access log delivery would append
// to the current policy.
BucketPolicy.fromCfnBucketPolicy(bucketPolicy);
Bucket bucket = Bucket.Builder.create(this, "MyBucket")
.serverAccessLogsBucket(accessLogsBucket)
.serverAccessLogsPrefix("logs")
.build();
// Install: dotnet add package Amazon.CDK.Lib
var bucketName = "amzn-s3-demo-bucket";
var accessLogsBucket = new Bucket(this, "AccessLogsBucket", new BucketProps {
ObjectOwnership = ObjectOwnership.BUCKET_OWNER_ENFORCED,
BucketName = bucketName
});
var bucketPolicy = new CfnBucketPolicy(this, "BucketPolicy", new CfnBucketPolicyProps {
Bucket = bucketName,
PolicyDocument = new Dictionary<string, object> {
{ "Statement", new [] { new Struct {
Action = "s3:*",
Effect = "Deny",
Principal = new Struct {
AWS = "*"
},
Resource = new [] { 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.
BucketPolicy.FromCfnBucketPolicy(bucketPolicy);
var bucket = new Bucket(this, "MyBucket", new BucketProps {
ServerAccessLogsBucket = accessLogsBucket,
ServerAccessLogsPrefix = "logs"
});
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
bucketName := "amzn-s3-demo-bucket"
accessLogsBucket := s3.NewBucket(this, jsii.String("AccessLogsBucket"), &BucketProps{
ObjectOwnership: s3.ObjectOwnership_BUCKET_OWNER_ENFORCED,
BucketName: jsii.String(BucketName),
})
bucketPolicy := s3.NewCfnBucketPolicy(this, jsii.String("BucketPolicy"), &CfnBucketPolicyProps{
Bucket: bucketName,
PolicyDocument: map[string]interface{}{
"Statement": []map[string]interface{}{
map[string]interface{}{
"Action": jsii.String("s3:*"),
"Effect": jsii.String("Deny"),
"Principal": map[string]*string{
"AWS": jsii.String("*"),
},
"Resource": []*string{
accessLogsBucket.bucketArn,
fmt.Sprintf("%v/*", accessLogsBucket.bucketArn),
},
},
},
"Version": jsii.String("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)
bucket := s3.NewBucket(this, jsii.String("MyBucket"), &BucketProps{
ServerAccessLogsBucket: accessLogsBucket,
ServerAccessLogsPrefix: jsii.String("logs"),
})
Initializer
new BucketPolicy(scope: Construct, id: string, props: BucketPolicyProps)
BucketPolicy(scope: Construct, id: str, props: BucketPolicyProps)
new BucketPolicy(Construct scope, String id, BucketPolicyProps props)
new BucketPolicy(Construct scope, string id, BucketPolicyProps props)
NewBucketPolicy(Scope Construct, Id *string, Props BucketPolicyProps)
Construct Props
documentoptionalPolicyDocumentdocumentoptionalPolicyDocumentdocumentoptionalPolicyDocumentDocumentoptionalPolicyDocumentDocumentoptionalPolicyDocument#Default: A new empty PolicyDocument will be created.
removalPolicyoptionalRemovalPolicyremoval_policyoptionalRemovalPolicyremovalPolicyoptionalRemovalPolicyRemovalPolicyoptionalRemovalPolicyRemovalPolicyoptionalRemovalPolicy= RemovalPolicy.DESTROY.#Properties
bucketPolicyRefBucketPolicyReferencebucket_policy_refBucketPolicyReferencebucketPolicyRefBucketPolicyReferenceBucketPolicyRefBucketPolicyReferenceBucketPolicyRefBucketPolicyReference#documentPolicyDocumentdocumentPolicyDocumentdocumentPolicyDocumentDocumentPolicyDocumentDocumentPolicyDocument#For more information, see Access Policy Language Overview in the Amazon Simple Storage Service Developer Guide.
envResourceEnvironmentenvResourceEnvironmentenvResourceEnvironmentEnvResourceEnvironmentEnvResourceEnvironment#notes
For resources that are created and managed in a Stack (those created by creating new class instances like new Role(), new Bucket(), etc.), this is always the same as the environment of the stack they belong to.
For referenced resources (those obtained from referencing methods like Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be different than the stack they were imported into.
PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringstatic#Methods
applyCrossStackReferenceStrength(strength: ReferenceStrength): voiddef apply_cross_stack_reference_strength(self, strength: ReferenceStrength) -> Nonepublic void applyCrossStackReferenceStrength(ReferenceStrength strength)public void ApplyCrossStackReferenceStrength(ReferenceStrength strength)func ApplyCrossStackReferenceStrength(Strength ReferenceStrength)#notes · 1 parameter
When set, any cross-stack reference to this resource will use the specified mechanism instead of the global default determined by the @aws-cdk/core:defaultCrossStackReferences context key. This is useful for selectively weakening specific references to avoid the "deadly embrace" problem without changing the app-wide default.
Parameters
strengthstrengthstrengthstrengthStrength— - The reference strength to use for this resource.
applyRemovalPolicy(removalPolicy: RemovalPolicy): voiddef apply_removal_policy(self, removal_policy: RemovalPolicy) -> Nonepublic void applyRemovalPolicy(RemovalPolicy removalPolicy)public void ApplyRemovalPolicy(RemovalPolicy removalPolicy)func ApplyRemovalPolicy(RemovalPolicy RemovalPolicy)#1 parameter
Parameters
removalPolicyremoval_policyremovalPolicyremovalPolicyRemovalPolicy— the RemovalPolicy to set.
fromCfnBucketPolicy(cfnBucketPolicy: CfnBucketPolicy): BucketPolicy // chainable@staticmethod def from_cfn_bucket_policy(cfn_bucket_policy: CfnBucketPolicy) -> BucketPolicypublic static BucketPolicy fromCfnBucketPolicy(CfnBucketPolicy cfnBucketPolicy)public static BucketPolicy FromCfnBucketPolicy(CfnBucketPolicy cfnBucketPolicy)func FromCfnBucketPolicy(CfnBucketPolicy CfnBucketPolicy) BucketPolicystatic#isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#notes · 1 parameter · returns
Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.
Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.
Parameters
xxxxX— Any object.
Returns: true if x is an object created from a class which extends Construct.
isOwnedResource(construct: IConstruct): boolean@staticmethod def is_owned_resource(construct: IConstruct) -> boolpublic static Boolean isOwnedResource(IConstruct construct)public static bool IsOwnedResource(IConstruct construct)func IsOwnedResource(Construct IConstruct) *boolstatic#isResource(construct: IConstruct): boolean@staticmethod def is_resource(construct: IConstruct) -> boolpublic static Boolean isResource(IConstruct construct)public static bool IsResource(IConstruct construct)func IsResource(Construct IConstruct) *boolstatic#toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct#Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.