# GrantPolicyWithResourceOptions

Options for a grant operation that directly adds a policy statement to a resource.

This differs from GrantWithResourceOptions in that it requires a pre-constructed
PolicyStatement rather than constructing one from individual permissions.
Use this when you need fine-grained control over the initial policy statement's contents.

Extends: [GrantWithResourceOptions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.GrantWithResourceOptions.html)
Used as argument in: [Grant.addStatementToResourcePolicy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.Grant.html#addstatementtoresourcepolicy)

- Source: [`aws-iam/lib/grant.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-iam/lib/grant.ts#L74)
- Type last changed: v2.186.0 · 27 Mar 2025

## Example

TypeScript:

```ts
declare const grantee: iam.IGrantable;
declare const actions: string[];
declare const resourceArns: string[];
declare const bucket: s3.Bucket;

const statement = new iam.PolicyStatement({
  effect: iam.Effect.ALLOW,
  actions: actions,
  principals: [new iam.ServicePrincipal('lambda.amazonaws.com')],
  conditions: {
    StringEquals: {
      'aws:SourceAccount': Stack.of(this).account,
    },
  },
});
iam.Grant.addStatementToResourcePolicy({
    grantee: grantee,
    actions: actions,
    resourceArns: resourceArns,
    resource: bucket,
    statement: statement,
 });
```

Python:

```python
# grantee: iam.IGrantable
# actions: List[str]
# resource_arns: List[str]
# bucket: s3.Bucket

statement = iam.PolicyStatement(
    effect=iam.Effect.ALLOW,
    actions=actions,
    principals=[iam.ServicePrincipal("lambda.amazonaws.com")],
    conditions={
        "StringEquals": {
            "aws:SourceAccount": Stack.of(self).account
        }
    }
)
iam.Grant.add_statement_to_resource_policy(
    grantee=grantee,
    actions=actions,
    resource_arns=resource_arns,
    resource=bucket,
    statement=statement
)
```

Java:

```java
IGrantable grantee;
String[] actions;
String[] resourceArns;
Bucket bucket;

PolicyStatement statement = PolicyStatement.Builder.create()
        .effect(Effect.ALLOW)
        .actions(actions)
        .principals(List.of(new ServicePrincipal("lambda.amazonaws.com")))
        .conditions(Map.of(
                "StringEquals", Map.of(
                        "aws:SourceAccount", Stack.of(this).getAccount())))
        .build();
Grant.addStatementToResourcePolicy(GrantPolicyWithResourceOptions.builder()
        .grantee(grantee)
        .actions(actions)
        .resourceArns(resourceArns)
        .resource(bucket)
        .statement(statement)
        .build());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
IGrantable grantee;
string[] actions;
string[] resourceArns;
Bucket bucket;

var statement = new PolicyStatement(new PolicyStatementProps {
    Effect = Effect.ALLOW,
    Actions = actions,
    Principals = new [] { new ServicePrincipal("lambda.amazonaws.com") },
    Conditions = new Dictionary<string, object> {
        { "StringEquals", new Dictionary<string, string> {
            { "aws:SourceAccount", Stack.Of(this).Account }
        } }
    }
});
Grant.AddStatementToResourcePolicy(new GrantPolicyWithResourceOptions {
    Grantee = grantee,
    Actions = actions,
    ResourceArns = resourceArns,
    Resource = bucket,
    Statement = statement
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var grantee IGrantable
var actions []*string
var resourceArns []*string
var bucket Bucket

statement := iam.NewPolicyStatement(&PolicyStatementProps{
	Effect: iam.Effect_ALLOW,
	Actions: actions,
	Principals: []IPrincipal{
		iam.NewServicePrincipal(jsii.String("lambda.amazonaws.com")),
	},
	Conditions: map[string]interface{}{
		"StringEquals": map[string]*string{
			"aws:SourceAccount": awscdk.*stack_of(this).account,
		},
	},
})
iam.Grant_AddStatementToResourcePolicy(&GrantPolicyWithResourceOptions{
	Grantee: grantee,
	Actions: actions,
	ResourceArns: resourceArns,
	Resource: bucket,
	Statement: statement,
})
```

## Properties

- `actions`: string[] (required) — The actions to grant.

- `conditions`: { [key: string]: { [key: string]: any } } — Any conditions to attach to the grant.

  **Default:** No conditions

- `grantee`: [IGrantable](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IGrantable.html) (required) — The principal to grant to.

  **Default:** if principal is undefined, no work is done.

- `resource`: [IResourceWithPolicyV2](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IResourceWithPolicyV2.html) (required) — The resource with a resource policy.

  The statement will be added to the resource policy if it couldn't be
  added to the principal policy.

- `resourceArns`: string[] (required) — The resource ARNs to grant to.

- `resourceSelfArns`: string[] — When referring to the resource in a resource policy, use this as ARN.

  **Default:** Same as regular resource ARNs

  (Depending on the resource type, this needs to be '*' in a resource policy).

- `statement`: [PolicyStatement](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.PolicyStatement.html) (required) — The policy statement to add to the resource's policy.

  This statement will be passed to the resource's addToResourcePolicy method.
  The actual handling of the statement depends on the specific IResourceWithPolicyV2
  implementation.
