BucketPolicy

Attaches a policy to an S3 bucket resource.

Example Usage

Basic Usage

using System.Collections.Generic;
using System.Text.Json;
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 = Output.Tuple(bucket.Arn, bucket.Arn).Apply(values =>
            {
                var bucketArn = values.Item1;
                var bucketArn1 = values.Item2;
                return JsonSerializer.Serialize(new Dictionary<string, object?>
                {
                    { "Version", "2012-10-17" },
                    { "Id", "MYBUCKETPOLICY" },
                    { "Statement", new[]
                        {
                            new Dictionary<string, object?>
                            {
                                { "Sid", "IPAllow" },
                                { "Effect", "Deny" },
                                { "Principal", "*" },
                                { "Action", "s3:*" },
                                { "Resource", new[]
                                    {
                                        bucketArn,
                                        $"{bucketArn1}/*",
                                    }
                                 },
                                { "Condition", new Dictionary<string, object?>
                                {
                                    { "IpAddress", new Dictionary<string, object?>
                                    {
                                        { "aws:SourceIp", "8.8.8.8/32" },
                                    } },
                                } },
                            },
                        }
                     },
                });
            }),
        });
    }

}
package main

import (
	"encoding/json"
	"fmt"

	"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/s3"
	"github.com/pulumi/pulumi/sdk/v3/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.All(bucket.Arn, bucket.Arn).ApplyT(func(_args []interface{}) (pulumi.String, error) {
				bucketArn := _args[0].(string)
				bucketArn1 := _args[1].(string)
				var _zero pulumi.String
				tmpJSON0, err := json.Marshal(map[string]interface{}{
					"Version": "2012-10-17",
					"Id":      "MYBUCKETPOLICY",
					"Statement": []map[string]interface{}{
						map[string]interface{}{
							"Sid":       "IPAllow",
							"Effect":    "Deny",
							"Principal": "*",
							"Action":    "s3:*",
							"Resource": []string{
								bucketArn,
								fmt.Sprintf("%v%v", bucketArn1, "/*"),
							},
							"Condition": map[string]interface{}{
								"IpAddress": map[string]interface{}{
									"aws:SourceIp": "8.8.8.8/32",
								},
							},
						},
					},
				})
				if err != nil {
					return _zero, err
				}
				json0 := string(tmpJSON0)
				return pulumi.String(json0), nil
			}).(pulumi.StringOutput),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
import pulumi
import json
import pulumi_aws as aws

bucket = aws.s3.Bucket("bucket")
bucket_policy = aws.s3.BucketPolicy("bucketPolicy",
    bucket=bucket.id,
    policy=pulumi.Output.all(bucket.arn, bucket.arn).apply(lambda bucketArn, bucketArn1: json.dumps({
        "Version": "2012-10-17",
        "Id": "MYBUCKETPOLICY",
        "Statement": [{
            "Sid": "IPAllow",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                bucket_arn,
                f"{bucket_arn1}/*",
            ],
            "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: pulumi.all([bucket.arn, bucket.arn]).apply(([bucketArn, bucketArn1]) => JSON.stringify({
        Version: "2012-10-17",
        Id: "MYBUCKETPOLICY",
        Statement: [{
            Sid: "IPAllow",
            Effect: "Deny",
            Principal: "*",
            Action: "s3:*",
            Resource: [
                bucketArn,
                `${bucketArn1}/*`,
            ],
            Condition: {
                IpAddress: {
                    "aws:SourceIp": "8.8.8.8/32",
                },
            },
        }],
    })),
});

Create a 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)
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.

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 | string
The text of the policy. Note: Bucket policies are limited to 20 KB in size.
Bucket string
The name of the bucket to which to apply the policy.
Policy string | string
The text of the policy. Note: Bucket policies are limited to 20 KB in size.
bucket string
The name of the bucket to which to apply the policy.
policy string | PolicyDocument
The text of the policy. Note: Bucket policies are limited to 20 KB in size.
bucket str
The name of the bucket to which to apply the policy.
policy str | str
The text of the policy. 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 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 | string
The text of the policy. Note: Bucket policies are limited to 20 KB in size.
Bucket string
The name of the bucket to which to apply the policy.
Policy string | string
The text of the policy. Note: Bucket policies are limited to 20 KB in size.
bucket string
The name of the bucket to which to apply the policy.
policy string | PolicyDocument
The text of the policy. Note: Bucket policies are limited to 20 KB in size.
bucket str
The name of the bucket to which to apply the policy.
policy str | str
The text of the policy. 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 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.