# ServiceAccount

Service Account.

Extends: Construct › ServiceAccount
Implements: IGrantable, IPrincipal, IConstruct, IDependable

- Source: [`aws-eks/lib/service-account.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-eks/lib/service-account.ts#L123)

## Example

```ts
declare const cluster: eks.Cluster;

new eks.ServiceAccount(this, 'ServiceAccount', {
  cluster,
  name: 'test-sa',
  namespace: 'default',
  identityType: eks.IdentityType.POD_IDENTITY,
});
```

## Initializer

```ts
new ServiceAccount(scope: Construct, id: string, props: ServiceAccountProps)
```

## Construct Props

- `annotations`: { [key: string]: string } — Additional annotations of the service account.

  **Default:** no additional annotations

- `cluster`: [ICluster](/cdk/api/v2/docs/aws-cdk-lib.aws_eks.ICluster.html) (required) — The cluster to apply the patch to.

- `identityType`: [IdentityType](/cdk/api/v2/docs/aws-cdk-lib.aws_eks.IdentityType.html) = IdentityType.IRSA — The identity type to use for the service account.

- `labels`: { [key: string]: string } — Additional labels of the service account.

  **Default:** no additional labels

- `name`: string — The name of the service account.

  **Default:** If no name is given, it will use the id of the resource.

  The name of a ServiceAccount object must be a valid DNS subdomain name.
  https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/

- `namespace`: string = "default" — The namespace of the service account.

  All namespace names must be valid RFC 1123 DNS labels.
  https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#namespaces-and-dns

- `overwriteServiceAccount`: boolean = false — Overwrite existing service account.

  If this is set, we will use `kubectl apply` instead of `kubectl create`
  when the service account is created. Otherwise, if there is already a service account
  in the cluster with the same name, the operation will fail.

- `removalPolicy`: [RemovalPolicy](/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) = RemovalPolicy.DESTROY — The removal policy applied to the service account resources.

  The removal policy controls what happens to the resources if they stop being managed by CloudFormation.
  This can happen in one of three situations:

  - The resource is removed from the template, so CloudFormation stops managing it
  - A change to the resource is made that requires it to be replaced, so CloudFormation stops managing it
  - The stack is deleted, so CloudFormation stops managing all resources in it

## Properties

- `assumeRoleAction`: string — When this Principal is used in an AssumeRole policy, the action to use.

- `grantPrincipal`: [IPrincipal](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IPrincipal.html) — The principal to grant permissions to.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `policyFragment`: [PrincipalPolicyFragment](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.PrincipalPolicyFragment.html) — Return the policy fragment that identifies this principal in a Policy.

- `role`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The role which is linked to the service account.

- `serviceAccountName`: string — The name of the service account.

- `serviceAccountNamespace`: string — The namespace where the service account is located in.

## Methods

- `addToPrincipalPolicy(statement: PolicyStatement): AddToPrincipalPolicyResult` — Add to the policy of this principal.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
