Class BucketPolicy


  • public class BucketPolicy
    extends com.pulumi.resources.CustomResource
    Attaches a policy to an S3 bucket resource. > Policies can be attached to both S3 general purpose buckets and S3 directory buckets. ## Example Usage ### Basic Usage
     
     package generated_program;
     
     import com.pulumi.Context;
     import com.pulumi.Pulumi;
     import com.pulumi.core.Output;
     import com.pulumi.aws.s3.Bucket;
     import com.pulumi.aws.s3.BucketArgs;
     import com.pulumi.aws.iam.IamFunctions;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementPrincipalArgs;
     import com.pulumi.aws.s3.BucketPolicy;
     import com.pulumi.aws.s3.BucketPolicyArgs;
     import java.util.ArrayList;
     import java.util.Arrays;
     import java.util.Map;
     import java.io.File;
     import java.nio.file.Files;
     import java.nio.file.Paths;
     
     public class App {
         public static void main(String[] args) {
             Pulumi.run(App::stack);
         }
     
         public static void stack(Context ctx) {
             var example = new Bucket("example", BucketArgs.builder()
                 .bucket("my-tf-test-bucket")
                 .build());
     
             final var allowAccessFromAnotherAccount = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
                 .statements(GetPolicyDocumentStatementArgs.builder()
                     .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                         .type("AWS")
                         .identifiers("123456789012")
                         .build())
                     .actions(                
                         "s3:GetObject",
                         "s3:ListBucket")
                     .resources(                
                         example.arn(),
                         example.arn().applyValue(_arn -> String.format("%s/*", _arn)))
                     .build())
                 .build());
     
             var allowAccessFromAnotherAccountBucketPolicy = new BucketPolicy("allowAccessFromAnotherAccountBucketPolicy", BucketPolicyArgs.builder()
                 .bucket(example.id())
                 .policy(allowAccessFromAnotherAccount.applyValue(_allowAccessFromAnotherAccount -> _allowAccessFromAnotherAccount.json()))
                 .build());
     
         }
     }
     
     
    > Only one `aws.s3.BucketPolicy` resource should be defined per S3 bucket. Defining multiple `aws.s3.BucketPolicy` resources with different Pulumi names but the same `bucket` value may result in unexpected policy overwrites. Each resource uses the `PutBucketPolicy` API, which replaces the entire existing policy without error or warning. Because Pulumi treats each resource independently, the policy applied last will silently override any previously applied policy. ## Import ### Identity Schema #### Required * `bucket` (String) Name of the S3 bucket. #### Optional * `accountId` (String) AWS Account where this resource is managed. * `region` (String) Region where this resource is managed. Using `pulumi import`, import S3 bucket policies using the bucket name. For example: ```sh $ pulumi import aws:s3/bucketPolicy:BucketPolicy example my-tf-test-bucket ```
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.pulumi.resources.CustomResource

        com.pulumi.resources.CustomResource.CustomResourceInternal
      • Nested classes/interfaces inherited from class com.pulumi.resources.Resource

        com.pulumi.resources.Resource.LazyField<T extends java.lang.Object>, com.pulumi.resources.Resource.LazyFields, com.pulumi.resources.Resource.ResourceInternal
    • Field Summary

      • Fields inherited from class com.pulumi.resources.Resource

        childResources, remote
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.pulumi.core.Output<java.lang.String> bucket()  
      static BucketPolicy get​(java.lang.String name, com.pulumi.core.Output<java.lang.String> id, BucketPolicyState state, com.pulumi.resources.CustomResourceOptions options)
      Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
      com.pulumi.core.Output<java.lang.String> policy()  
      com.pulumi.core.Output<java.lang.String> region()  
      • Methods inherited from class com.pulumi.resources.CustomResource

        getId, id, idFuture
      • Methods inherited from class com.pulumi.resources.Resource

        getChildResources, getResourceName, getResourceType, getUrn, pulumiChildResources, pulumiResourceName, pulumiResourceType, urn
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BucketPolicy

        public BucketPolicy​(java.lang.String name)
        Parameters:
        name - The _unique_ name of the resulting resource.
      • BucketPolicy

        public BucketPolicy​(java.lang.String name,
                            BucketPolicyArgs args)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
      • BucketPolicy

        public BucketPolicy​(java.lang.String name,
                            BucketPolicyArgs args,
                            @Nullable
                            com.pulumi.resources.CustomResourceOptions options)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
        options - A bag of options that control this resource's behavior.
    • Method Detail

      • bucket

        public com.pulumi.core.Output<java.lang.String> bucket()
        Returns:
        Name of the bucket to which to apply the policy.
      • policy

        public com.pulumi.core.Output<java.lang.String> policy()
        Returns:
        Text of the policy. Although this is a bucket policy rather than an IAM policy, the `aws.iam.getPolicyDocument` data source may be used, so long as it specifies a principal. For more information about building AWS IAM policy documents, see the AWS IAM Policy Document Guide. Note: Bucket policies are limited to 20 KB in size.
      • region

        public com.pulumi.core.Output<java.lang.String> region()
        Returns:
        Region where this resource will be [managed](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the provider configuration.
      • get

        public static BucketPolicy get​(java.lang.String name,
                                       com.pulumi.core.Output<java.lang.String> id,
                                       @Nullable
                                       BucketPolicyState state,
                                       @Nullable
                                       com.pulumi.resources.CustomResourceOptions options)
        Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
        Parameters:
        name - The _unique_ name of the resulting resource.
        id - The _unique_ provider ID of the resource to lookup.
        state -
        options - Optional settings to control the behavior of the CustomResource.