Want to learn about running containers in Lambda? Join our free hands-on workshop. Register Now

BucketPolicy

Attaches a policy to an S3 bucket resource.

Example Usage

Basic Usage

using Pulumi;
using Aws = Pulumi.Aws;

class MyStack : Stack
{
    public MyStack()
    {
        var bucket = new Aws.S3.Bucket("bucket", new Aws.S3.BucketArgs
        {
        });
        var bucketPolicy = new Aws.S3.BucketPolicy("bucketPolicy", new Aws.S3.BucketPolicyArgs
        {
            Bucket = bucket.Id,
            Policy = @"{
  ""Version"": ""2012-10-17"",
  ""Id"": ""MYBUCKETPOLICY"",
  ""Statement"": [
    {
      ""Sid"": ""IPAllow"",
      ""Effect"": ""Deny"",
      ""Principal"": ""*"",
      ""Action"": ""s3:*"",
      ""Resource"": ""arn:aws:s3:::my_tf_test_bucket/*"",
      ""Condition"": {
         ""IpAddress"": {""aws:SourceIp"": ""8.8.8.8/32""}
      }
    }
  ]
}
",
        });
    }

}
package main

import (
    "fmt"

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

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        bucket, err := s3.NewBucket(ctx, "bucket", nil)
        if err != nil {
            return err
        }
        _, err = s3.NewBucketPolicy(ctx, "bucketPolicy", &s3.BucketPolicyArgs{
            Bucket: bucket.ID(),
            Policy: pulumi.String(fmt.Sprintf("%v%v%v%v%v%v%v%v%v%v%v%v%v%v%v%v", "{\n", "  \"Version\": \"2012-10-17\",\n", "  \"Id\": \"MYBUCKETPOLICY\",\n", "  \"Statement\": [\n", "    {\n", "      \"Sid\": \"IPAllow\",\n", "      \"Effect\": \"Deny\",\n", "      \"Principal\": \"*\",\n", "      \"Action\": \"s3:*\",\n", "      \"Resource\": \"arn:aws:s3:::my_tf_test_bucket/*\",\n", "      \"Condition\": {\n", "         \"IpAddress\": {\"aws:SourceIp\": \"8.8.8.8/32\"}\n", "      }\n", "    }\n", "  ]\n", "}\n")),
        })
        if err != nil {
            return err
        }
        return nil
    })
}
import pulumi
import pulumi_aws as aws

bucket = aws.s3.Bucket("bucket")
bucket_policy = aws.s3.BucketPolicy("bucketPolicy",
    bucket=bucket.id,
    policy="""{
  "Version": "2012-10-17",
  "Id": "MYBUCKETPOLICY",
  "Statement": [
    {
      "Sid": "IPAllow",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": "arn:aws:s3:::my_tf_test_bucket/*",
      "Condition": {
         "IpAddress": {"aws:SourceIp": "8.8.8.8/32"}
      }
    }
  ]
}
""")
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const bucket = new aws.s3.Bucket("bucket", {});
const bucketPolicy = new aws.s3.BucketPolicy("bucketPolicy", {
    bucket: bucket.id,
    policy: `{
  "Version": "2012-10-17",
  "Id": "MYBUCKETPOLICY",
  "Statement": [
    {
      "Sid": "IPAllow",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": "arn:aws:s3:::my_tf_test_bucket/*",
      "Condition": {
         "IpAddress": {"aws:SourceIp": "8.8.8.8/32"}
      }
    }
  ]
}
`,
});

Create a BucketPolicy Resource

def BucketPolicy(resource_name: str, opts: Optional[ResourceOptions] = None, bucket: Optional[str] = None, policy: Optional[str] = None)
func NewBucketPolicy(ctx *Context, name string, args BucketPolicyArgs, opts ...ResourceOption) (*BucketPolicy, error)
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.
opts ResourceOptions
A bag of options that control this 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.

BucketPolicy Resource Properties

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

Inputs

The BucketPolicy resource accepts the following input properties:

Bucket string

The name of the bucket to which to apply the policy.

Policy string

The text of the policy.

Bucket string

The name of the bucket to which to apply the policy.

Policy interface{}

The text of the policy.

bucket string

The name of the bucket to which to apply the policy.

policy string | PolicyDocument

The text of the policy.

bucket str

The name of the bucket to which to apply the policy.

policy str

The text of the policy.

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 str
The provider-assigned unique ID for this managed resource.

Look up an 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)
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.

The following state arguments are supported:

Bucket string

The name of the bucket to which to apply the policy.

Policy string

The text of the policy.

Bucket string

The name of the bucket to which to apply the policy.

Policy interface{}

The text of the policy.

bucket string

The name of the bucket to which to apply the policy.

policy string | PolicyDocument

The text of the policy.

bucket str

The name of the bucket to which to apply the policy.

policy str

The text of the policy.

Import

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

 $ pulumi import aws:s3/bucketPolicy:BucketPolicy example my-bucket-name

Package Details

Repository
https://github.com/pulumi/pulumi-aws
License
Apache-2.0
Notes
This Pulumi package is based on the aws Terraform Provider.