aws logo
AWS Classic v5.33.0, Mar 24 23

aws.s3.BucketPolicy

Attaches a policy to an S3 bucket resource.

Example Usage

Basic Usage

using System.Collections.Generic;
using Pulumi;
using Aws = Pulumi.Aws;

return await Deployment.RunAsync(() => 
{
    var example = new Aws.S3.BucketV2("example");

    var allowAccessFromAnotherAccountPolicyDocument = Aws.Iam.GetPolicyDocument.Invoke(new()
    {
        Statements = new[]
        {
            new Aws.Iam.Inputs.GetPolicyDocumentStatementInputArgs
            {
                Principals = new[]
                {
                    new Aws.Iam.Inputs.GetPolicyDocumentStatementPrincipalInputArgs
                    {
                        Type = "AWS",
                        Identifiers = new[]
                        {
                            "123456789012",
                        },
                    },
                },
                Actions = new[]
                {
                    "s3:GetObject",
                    "s3:ListBucket",
                },
                Resources = new[]
                {
                    example.Arn,
                    $"{example.Arn}/*",
                },
            },
        },
    });

    var allowAccessFromAnotherAccountBucketPolicy = new Aws.S3.BucketPolicy("allowAccessFromAnotherAccountBucketPolicy", new()
    {
        Bucket = example.Id,
        Policy = allowAccessFromAnotherAccountPolicyDocument.Apply(getPolicyDocumentResult => getPolicyDocumentResult.Json),
    });

});
package main

import (
	"fmt"

	"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/iam"
	"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		example, err := s3.NewBucketV2(ctx, "example", nil)
		if err != nil {
			return err
		}
		allowAccessFromAnotherAccountPolicyDocument := iam.GetPolicyDocumentOutput(ctx, iam.GetPolicyDocumentOutputArgs{
			Statements: iam.GetPolicyDocumentStatementArray{
				&iam.GetPolicyDocumentStatementArgs{
					Principals: iam.GetPolicyDocumentStatementPrincipalArray{
						&iam.GetPolicyDocumentStatementPrincipalArgs{
							Type: pulumi.String("AWS"),
							Identifiers: pulumi.StringArray{
								pulumi.String("123456789012"),
							},
						},
					},
					Actions: pulumi.StringArray{
						pulumi.String("s3:GetObject"),
						pulumi.String("s3:ListBucket"),
					},
					Resources: pulumi.StringArray{
						example.Arn,
						example.Arn.ApplyT(func(arn string) (string, error) {
							return fmt.Sprintf("%v/*", arn), nil
						}).(pulumi.StringOutput),
					},
				},
			},
		}, nil)
		_, err = s3.NewBucketPolicy(ctx, "allowAccessFromAnotherAccountBucketPolicy", &s3.BucketPolicyArgs{
			Bucket: example.ID(),
			Policy: allowAccessFromAnotherAccountPolicyDocument.ApplyT(func(allowAccessFromAnotherAccountPolicyDocument iam.GetPolicyDocumentResult) (*string, error) {
				return &allowAccessFromAnotherAccountPolicyDocument.Json, nil
			}).(pulumi.StringPtrOutput),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.s3.BucketV2;
import com.pulumi.aws.iam.IamFunctions;
import com.pulumi.aws.iam.inputs.GetPolicyDocumentArgs;
import com.pulumi.aws.s3.BucketPolicy;
import com.pulumi.aws.s3.BucketPolicyArgs;
import java.util.List;
import java.util.ArrayList;
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 BucketV2("example");

        final var allowAccessFromAnotherAccountPolicyDocument = 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(allowAccessFromAnotherAccountPolicyDocument.applyValue(getPolicyDocumentResult -> getPolicyDocumentResult).applyValue(allowAccessFromAnotherAccountPolicyDocument -> allowAccessFromAnotherAccountPolicyDocument.applyValue(getPolicyDocumentResult -> getPolicyDocumentResult.json())))
            .build());

    }
}
import pulumi
import pulumi_aws as aws

example = aws.s3.BucketV2("example")
allow_access_from_another_account_policy_document = aws.iam.get_policy_document_output(statements=[aws.iam.GetPolicyDocumentStatementArgs(
    principals=[aws.iam.GetPolicyDocumentStatementPrincipalArgs(
        type="AWS",
        identifiers=["123456789012"],
    )],
    actions=[
        "s3:GetObject",
        "s3:ListBucket",
    ],
    resources=[
        example.arn,
        example.arn.apply(lambda arn: f"{arn}/*"),
    ],
)])
allow_access_from_another_account_bucket_policy = aws.s3.BucketPolicy("allowAccessFromAnotherAccountBucketPolicy",
    bucket=example.id,
    policy=allow_access_from_another_account_policy_document.json)
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const example = new aws.s3.BucketV2("example", {});
const allowAccessFromAnotherAccountPolicyDocument = aws.iam.getPolicyDocumentOutput({
    statements: [{
        principals: [{
            type: "AWS",
            identifiers: ["123456789012"],
        }],
        actions: [
            "s3:GetObject",
            "s3:ListBucket",
        ],
        resources: [
            example.arn,
            pulumi.interpolate`${example.arn}/*`,
        ],
    }],
});
const allowAccessFromAnotherAccountBucketPolicy = new aws.s3.BucketPolicy("allowAccessFromAnotherAccountBucketPolicy", {
    bucket: example.id,
    policy: allowAccessFromAnotherAccountPolicyDocument.apply(allowAccessFromAnotherAccountPolicyDocument => allowAccessFromAnotherAccountPolicyDocument.json),
});
resources:
  example:
    type: aws:s3:BucketV2
  allowAccessFromAnotherAccountBucketPolicy:
    type: aws:s3:BucketPolicy
    properties:
      bucket: ${example.id}
      policy: ${allowAccessFromAnotherAccountPolicyDocument.json}
variables:
  allowAccessFromAnotherAccountPolicyDocument:
    fn::invoke:
      Function: aws:iam:getPolicyDocument
      Arguments:
        statements:
          - principals:
              - type: AWS
                identifiers:
                  - '123456789012'
            actions:
              - s3:GetObject
              - s3:ListBucket
            resources:
              - ${example.arn}
              - ${example.arn}/*

Create BucketPolicy Resource

new BucketPolicy(name: string, args: BucketPolicyArgs, opts?: CustomResourceOptions);
@overload
def BucketPolicy(resource_name: str,
                 opts: Optional[ResourceOptions] = None,
                 bucket: Optional[str] = None,
                 policy: Optional[str] = None)
@overload
def BucketPolicy(resource_name: str,
                 args: BucketPolicyArgs,
                 opts: Optional[ResourceOptions] = None)
func NewBucketPolicy(ctx *Context, name string, args BucketPolicyArgs, opts ...ResourceOption) (*BucketPolicy, error)
public BucketPolicy(string name, BucketPolicyArgs args, CustomResourceOptions? opts = null)
public BucketPolicy(String name, BucketPolicyArgs args)
public BucketPolicy(String name, BucketPolicyArgs args, CustomResourceOptions options)
type: aws:s3:BucketPolicy
properties: # The arguments to resource properties.
options: # Bag of options to control resource's behavior.

name string
The unique name of the resource.
args BucketPolicyArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
resource_name str
The unique name of the resource.
args BucketPolicyArgs
The arguments to resource properties.
opts ResourceOptions
Bag of options to control resource's behavior.
ctx Context
Context object for the current deployment.
name string
The unique name of the resource.
args BucketPolicyArgs
The arguments to resource properties.
opts ResourceOption
Bag of options to control resource's behavior.
name string
The unique name of the resource.
args BucketPolicyArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
name String
The unique name of the resource.
args BucketPolicyArgs
The arguments to resource properties.
options CustomResourceOptions
Bag of options to control resource's behavior.

BucketPolicy Resource Properties

To learn more about resource properties and how to use them, see Inputs and Outputs in the Architecture and Concepts docs.

Inputs

The BucketPolicy resource accepts the following input properties:

Bucket string

Name of the bucket to which to apply the policy.

Policy string | string

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.

Bucket string

Name of the bucket to which to apply the policy.

Policy string | string

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.

bucket String

Name of the bucket to which to apply the policy.

policy String | String

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.

bucket string

Name of the bucket to which to apply the policy.

policy string | PolicyDocument

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.

bucket str

Name of the bucket to which to apply the policy.

policy str | str

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.

bucket String

Name of the bucket to which to apply the policy.

policy String |

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.

Outputs

All input properties are implicitly available as output properties. Additionally, the BucketPolicy resource produces the following output properties:

Id string

The provider-assigned unique ID for this managed resource.

Id string

The provider-assigned unique ID for this managed resource.

id String

The provider-assigned unique ID for this managed resource.

id string

The provider-assigned unique ID for this managed resource.

id str

The provider-assigned unique ID for this managed resource.

id String

The provider-assigned unique ID for this managed resource.

Look up Existing BucketPolicy Resource

Get an existing BucketPolicy resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

public static get(name: string, id: Input<ID>, state?: BucketPolicyState, opts?: CustomResourceOptions): BucketPolicy
@staticmethod
def get(resource_name: str,
        id: str,
        opts: Optional[ResourceOptions] = None,
        bucket: Optional[str] = None,
        policy: Optional[str] = None) -> BucketPolicy
func GetBucketPolicy(ctx *Context, name string, id IDInput, state *BucketPolicyState, opts ...ResourceOption) (*BucketPolicy, error)
public static BucketPolicy Get(string name, Input<string> id, BucketPolicyState? state, CustomResourceOptions? opts = null)
public static BucketPolicy get(String name, Output<String> id, BucketPolicyState state, CustomResourceOptions options)
Resource lookup is not supported in YAML
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
resource_name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
The following state arguments are supported:
Bucket string

Name of the bucket to which to apply the policy.

Policy string | string

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.

Bucket string

Name of the bucket to which to apply the policy.

Policy string | string

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.

bucket String

Name of the bucket to which to apply the policy.

policy String | String

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.

bucket string

Name of the bucket to which to apply the policy.

policy string | PolicyDocument

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.

bucket str

Name of the bucket to which to apply the policy.

policy str | str

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.

bucket String

Name of the bucket to which to apply the policy.

policy String |

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.

Import

S3 bucket policies can be imported using the bucket name, e.g.,

 $ pulumi import aws:s3/bucketPolicy:BucketPolicy allow_access_from_another_account my-tf-test-bucket

Package Details

Repository
AWS Classic pulumi/pulumi-aws
License
Apache-2.0
Notes

This Pulumi package is based on the aws Terraform Provider.