# ServicePrincipalOpts

Options for a service principal.

Used as props by: [ServicePrincipal](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.ServicePrincipal.html)
Used as argument in: [PolicyStatement.addServicePrincipal](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.PolicyStatement.html#addserviceprincipal)

- Source: [`aws-iam/lib/principals.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-iam/lib/principals.ts#L501)
- Type last changed: v2.51.0 · 18 Nov 2022

## Example

TypeScript:

```ts
declare const fn: lambda.WebFunction;
declare const distributionArn: string;

fn.endpoint.grants.invoke(new iam.ServicePrincipal('cloudfront.amazonaws.com', {
  conditions: { ArnLike: { 'aws:SourceArn': distributionArn } },
}));
```

Python:

```python
# fn: lambda.WebFunction
# distribution_arn: str

fn.endpoint.grants.invoke(iam.ServicePrincipal("cloudfront.amazonaws.com",
    conditions={"ArnLike": {"aws:SourceArn": distribution_arn}}
))
```

Java:

```java
WebFunction fn;
String distributionArn;

fn.endpoint.grants.invoke(ServicePrincipal.Builder.create("cloudfront.amazonaws.com")
        .conditions(Map.of("ArnLike", Map.of("aws:SourceArn", distributionArn)))
        .build());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
WebFunction fn;
string distributionArn;

fn.Endpoint.Grants.Invoke(new ServicePrincipal("cloudfront.amazonaws.com", new ServicePrincipalOpts {
    Conditions = new Dictionary<string, object> { { "ArnLike", new Dictionary<string, string> { { "aws:SourceArn", distributionArn } } } }
}));
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var fn WebFunction
var distributionArn string

fn.Endpoint.Grants.Invoke(iam.NewServicePrincipal(jsii.String("cloudfront.amazonaws.com"), &ServicePrincipalOpts{
	Conditions: map[string]interface{}{
		"ArnLike": map[string]*string{
			"aws:SourceArn": distributionArn,
		},
	},
}))
```

## Properties

- `conditions`: { [key: string]: any } — Additional conditions to add to the Service Principal.

  **Default:** No conditions

- `region`: string — The region in which you want to reference the service.

  **Default:** the resolving Stack's region.

  This is only necessary for *cross-region* references to *opt-in* regions. In those
  cases, the region name needs to be included to reference the correct service principal.
  In all other cases, the global service principal name is sufficient.

  This field behaves differently depending on whether the `@aws-cdk/aws-iam:standardizedServicePrincipals`
  flag is set or not:

  - If the flag is set, the input service principal is assumed to be of the form `SERVICE.amazonaws.com`.
    That value will always be returned, unless the given region is an opt-in region and the service
    principal is rendered in a stack in a different region, in which case `SERVICE.REGION.amazonaws.com`
    will be rendered. Under this regime, there is no downside to always specifying the region property:
    it will be rendered only if necessary.
  - If the flag is not set, the service principal will resolve to a single principal
    whose name comes from the `@aws-cdk/region-info` package, using the region to override
    the stack region. If there is no entry for this service principal in the database,, the input
    service name is returned literally. This is legacy behavior and is not recommended.
