In AWS CDK, there are two ways to build a bucket policy:
utilise the addToResourcePolicy function on a Bucket class object.
Create an instance of the BucketPolicy class.
With the addToResourcePolicy method, we take an implicit approach: once we add a policy statement to the bucket, CDK constructs a bucket policy for us.
The second technique is explicit and a little easier to understand for the reader of our code.
This article's code may be found on GitHub.
Let's take a look at a real-life example of both. First, let's look at the addToResourcePolicy method: