Skip to content
v2.274.0

ServiceAccount ​

Service Account.

Example ​

ts
import * as s3 from 'aws-cdk-lib/aws-s3';
// you can import an existing provider
const provider = eks.OidcProviderNative.fromOidcProviderArn(this, 'Provider', 'arn:aws:iam::123456:oidc-provider/oidc.eks.eu-west-1.amazonaws.com/id/AB123456ABC');

// or create a new one using an existing issuer url
declare const issuerUrl: string;
const provider2 = new eks.OidcProviderNative(this, 'Provider', {
  url: issuerUrl,
});

import { KubectlV36Layer } from '@aws-cdk/lambda-layer-kubectl-v36';

const cluster = eks.Cluster.fromClusterAttributes(this, 'MyCluster', {
  clusterName: 'Cluster',
  openIdConnectProvider: provider,
  kubectlProviderOptions: {
    kubectlLayer: new KubectlV36Layer(this, 'kubectl'),
  }});

const serviceAccount = cluster.addServiceAccount('MyServiceAccount');

const bucket = new s3.Bucket(this, 'Bucket');
bucket.grantReadWrite(serviceAccount);
python
import aws_cdk.aws_s3 as s3

# or create a new one using an existing issuer url
# issuer_url: str

from aws_cdk.lambda_layer_kubectl_v36 import KubectlV36Layer

# you can import an existing provider
provider = eks.OidcProviderNative.from_oidc_provider_arn(self, "Provider", "arn:aws:iam::123456:oidc-provider/oidc.eks.eu-west-1.amazonaws.com/id/AB123456ABC")
provider2 = eks.OidcProviderNative(self, "Provider",
    url=issuer_url
)

cluster = eks.Cluster.from_cluster_attributes(self, "MyCluster",
    cluster_name="Cluster",
    open_id_connect_provider=provider,
    kubectl_provider_options=eks.KubectlProviderOptions(
        kubectl_layer=KubectlV36Layer(self, "kubectl")
    )
)

service_account = cluster.add_service_account("MyServiceAccount")

bucket = s3.Bucket(self, "Bucket")
bucket.grant_read_write(service_account)
java
import software.amazon.awscdk.services.s3.*;

// or create a new one using an existing issuer url
String issuerUrl;

import software.amazon.awscdk.cdk.lambdalayer.kubectl.v36.KubectlV36Layer;

// you can import an existing provider
IOidcProvider provider = OidcProviderNative.fromOidcProviderArn(this, "Provider", "arn:aws:iam::123456:oidc-provider/oidc.eks.eu-west-1.amazonaws.com/id/AB123456ABC");
OidcProviderNative provider2 = OidcProviderNative.Builder.create(this, "Provider")
        .url(issuerUrl)
        .build();

ICluster cluster = Cluster.fromClusterAttributes(this, "MyCluster", ClusterAttributes.builder()
        .clusterName("Cluster")
        .openIdConnectProvider(provider)
        .kubectlProviderOptions(KubectlProviderOptions.builder()
                .kubectlLayer(new KubectlV36Layer(this, "kubectl"))
                .build())
        .build());

ServiceAccount serviceAccount = cluster.addServiceAccount("MyServiceAccount");

Bucket bucket = new Bucket(this, "Bucket");
bucket.grantReadWrite(serviceAccount);
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.S3;

// or create a new one using an existing issuer url
string issuerUrl;

using Amazon.CDK.LambdaLayer.KubectlV36;

// you can import an existing provider
var provider = OidcProviderNative.FromOidcProviderArn(this, "Provider", "arn:aws:iam::123456:oidc-provider/oidc.eks.eu-west-1.amazonaws.com/id/AB123456ABC");
var provider2 = new OidcProviderNative(this, "Provider", new OidcProviderNativeProps {
    Url = issuerUrl
});

var cluster = Cluster.FromClusterAttributes(this, "MyCluster", new ClusterAttributes {
    ClusterName = "Cluster",
    OpenIdConnectProvider = provider,
    KubectlProviderOptions = new KubectlProviderOptions {
        KubectlLayer = new KubectlV36Layer(this, "kubectl")
    }
});

var serviceAccount = cluster.AddServiceAccount("MyServiceAccount");

var bucket = new Bucket(this, "Bucket");
bucket.GrantReadWrite(serviceAccount);
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import s3 "github.com/aws/aws-cdk-go/awscdk"

// or create a new one using an existing issuer url
var issuerUrl string

import "github.com/cdklabs/awscdk-kubectl-go/kubectlv36"

// you can import an existing provider
provider := eks.OidcProviderNative_FromOidcProviderArn(this, jsii.String("Provider"), jsii.String("arn:aws:iam::123456:oidc-provider/oidc.eks.eu-west-1.amazonaws.com/id/AB123456ABC"))
provider2 := eks.NewOidcProviderNative(this, jsii.String("Provider"), &OidcProviderNativeProps{
	Url: issuerUrl,
})

cluster := eks.Cluster_FromClusterAttributes(this, jsii.String("MyCluster"), &ClusterAttributes{
	ClusterName: jsii.String("Cluster"),
	OpenIdConnectProvider: provider,
	KubectlProviderOptions: &KubectlProviderOptions{
		KubectlLayer: kubectlv36.NewKubectlV36Layer(this, jsii.String("kubectl")),
	},
})

serviceAccount := cluster.AddServiceAccount(jsii.String("MyServiceAccount"))

bucket := s3.NewBucket(this, jsii.String("Bucket"))
bucket.GrantReadWrite(serviceAccount)

Initializer ​

ts
new ServiceAccount(scope: Construct, id: string, props: ServiceAccountProps)
python
ServiceAccount(scope: Construct, id: str, props: ServiceAccountProps)
java
new ServiceAccount(Construct scope, String id, ServiceAccountProps props)
csharp
new ServiceAccount(Construct scope, string id, ServiceAccountProps props)
go
NewServiceAccount(Scope Construct, Id *string, Props ServiceAccountProps)

Construct Props ​

annotationsoptional{ [key: string]: string }annotationsoptionaltyping.Mapping[str, str]annotationsoptionalMap<String, String>AnnotationsoptionalIDictionary<string, string>Annotationsoptional*map[string]*string#
Additional annotations of the service account.

Default: no additional annotations

clusterIClusterclusterIClusterclusterIClusterClusterIClusterClusterIClusterrequired#
The cluster to apply the patch to.
identityTypeoptionalIdentityTypeidentity_typeoptionalIdentityTypeidentityTypeoptionalIdentityTypeIdentityTypeoptionalIdentityTypeIdentityTypeoptionalIdentityType= IdentityType.IRSA#
The identity type to use for the service account.
labelsoptional{ [key: string]: string }labelsoptionaltyping.Mapping[str, str]labelsoptionalMap<String, String>LabelsoptionalIDictionary<string, string>Labelsoptional*map[string]*string#
Additional labels of the service account.

Default: no additional labels

nameoptionalstringnameoptionalstrnameoptionalStringNameoptionalstringNameoptional*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/

namespaceoptionalstringnamespaceoptionalstrnamespaceoptionalStringNamespaceoptionalstringNamespaceoptional*string= "default"#
The namespace of the service account.
overwriteServiceAccountoptionalbooleanoverwrite_service_accountoptionalbooloverwriteServiceAccountoptionalBooleanOverwriteServiceAccountoptionalboolOverwriteServiceAccountoptional*bool= 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.

removalPolicyoptionalRemovalPolicyremoval_policyoptionalRemovalPolicyremovalPolicyoptionalRemovalPolicyRemovalPolicyoptionalRemovalPolicyRemovalPolicyoptionalRemovalPolicy= RemovalPolicy.DESTROY#
The removal policy applied to the service account resources.
notes

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 ​

assumeRoleActionstringassume_role_actionstrassumeRoleActionStringAssumeRoleActionstringAssumeRoleAction*string#
When this Principal is used in an AssumeRole policy, the action to use.
grantPrincipalIPrincipalgrant_principalIPrincipalgrantPrincipalIPrincipalGrantPrincipalIPrincipalGrantPrincipalIPrincipal#
The principal to grant permissions to.
nodeNodenodeNodenodeNodeNodeNodeNodeNode#
The tree node.
Return the policy fragment that identifies this principal in a Policy.
roleIRoleroleIRoleroleIRoleRoleIRoleRoleIRole#
The role which is linked to the service account.
serviceAccountNamestringservice_account_namestrserviceAccountNameStringServiceAccountNamestringServiceAccountName*string#
The name of the service account.
serviceAccountNamespacestringservice_account_namespacestrserviceAccountNamespaceStringServiceAccountNamespacestringServiceAccountNamespace*string#
The namespace where the service account is located in.

Methods ​

addToPolicy(statement: PolicyStatement): booleandef add_to_policy(self, statement: PolicyStatement) -> boolpublic Boolean addToPolicy(PolicyStatement statement)public bool AddToPolicy(PolicyStatement statement)func AddToPolicy(Statement PolicyStatement) *booldeprecated#
use `addToPrincipalPolicy()`
addToPrincipalPolicy(statement: PolicyStatement): AddToPrincipalPolicyResultdef add_to_principal_policy(self, statement: PolicyStatement) -> AddToPrincipalPolicyResultpublic AddToPrincipalPolicyResult addToPrincipalPolicy(PolicyStatement statement)public AddToPrincipalPolicyResult AddToPrincipalPolicy(PolicyStatement statement)func AddToPrincipalPolicy(Statement PolicyStatement) AddToPrincipalPolicyResult#
Add to the policy of this principal.
isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#
Checks if `x` is a construct.
notes · 1 parameter · returns

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

  • xxxxX — Any object.

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

toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of this construct.
with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func 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.

1 parameter · returns

Parameters

  • mixinsmixinsmixinsmixinsMixins — The mixins to apply.

Returns: This construct for chaining