Custom resource provider for executing kubectl operations in EKS clusters, enabling direct Kubernetes resource management through AWS Lambda functions.
Lambda-based custom resource provider for kubectl operations.
/**
* Custom resource provider for kubectl operations
*/
class KubectlProvider extends NestedStack implements IKubectlProvider {
constructor(scope: Construct, id: string, props: KubectlProviderProps);
/** Custom resource provider's service token */
readonly serviceToken: string;
/** IAM role ARN for kubectl operations */
readonly roleArn: string;
/** IAM execution role of the handler */
readonly handlerRole: iam.IRole;
/** Get or create kubectl provider for cluster */
static getOrCreate(scope: Construct, cluster: ICluster): IKubectlProvider;
/** Import existing kubectl provider */
static fromKubectlProviderAttributes(scope: Construct, id: string, attrs: KubectlProviderAttributes): IKubectlProvider;
}
/**
* Kubectl provider interface
*/
interface IKubectlProvider extends IConstruct {
/** Custom resource provider's service token */
readonly serviceToken: string;
/** IAM role ARN for kubectl operations */
readonly roleArn: string;
/** IAM execution role of the handler */
readonly handlerRole: iam.IRole;
}
/**
* Kubectl provider properties
*/
interface KubectlProviderProps {
/** EKS cluster to control */
readonly cluster: ICluster;
}
/**
* Kubectl provider import attributes
*/
interface KubectlProviderAttributes {
/** Kubectl provider lambda ARN */
readonly functionArn: string;
/** IAM role ARN for kubectl operations */
readonly kubectlRoleArn: string;
/** IAM execution role of the handler */
readonly handlerRole: iam.IRole;
}Usage Examples:
import * as eks from "@aws-cdk/aws-eks";
// Get or create kubectl provider (automatically managed)
const provider = eks.KubectlProvider.getOrCreate(this, cluster);
// Import existing kubectl provider
const importedProvider = eks.KubectlProvider.fromKubectlProviderAttributes(this, "ImportedProvider", {
functionArn: "arn:aws:lambda:us-west-2:123456789012:function:MyKubectlHandler",
kubectlRoleArn: "arn:aws:iam::123456789012:role/MyKubectlRole",
handlerRole: existingRole,
});
// Use provider for custom resources
const customResource = new CustomResource(this, "CustomK8sResource", {
serviceToken: provider.serviceToken,
properties: {
ClusterName: cluster.clusterName,
RoleArn: provider.roleArn,
Manifest: JSON.stringify({
apiVersion: "v1",
kind: "ConfigMap",
metadata: {
name: "my-config",
namespace: "default",
},
data: {
"config.yaml": "key: value",
},
}),
},
});The kubectl provider creates a Lambda-based custom resource with the following components:
Lambda Function Configuration:
IAM Permissions:
eks:DescribeCluster on the target clustersts:AssumeRole on the cluster's kubectl roleAmazonEC2ContainerRegistryReadOnly for OCI Helm chartsNetwork Configuration:
/**
* Get existing provider or create new one for cluster
* @param scope - Construct scope
* @param cluster - EKS cluster
* @returns Kubectl provider instance
*/
static getOrCreate(scope: Construct, cluster: ICluster): IKubectlProvider;
/**
* Import existing kubectl provider
* @param scope - Construct scope
* @param id - Resource identifier
* @param attrs - Provider attributes
* @returns Imported kubectl provider
*/
static fromKubectlProviderAttributes(
scope: Construct,
id: string,
attrs: KubectlProviderAttributes
): IKubectlProvider;The kubectl provider is automatically used by the following EKS constructs:
KubernetesManifest - Apply raw Kubernetes manifestsHelmChart - Install Helm chartsKubernetesPatch - Apply patches to resourcesKubernetesObjectValue - Query resource valuesAwsAuth - Manage aws-auth ConfigMap// These constructs automatically use the kubectl provider
cluster.addManifest("MyManifest", {
apiVersion: "v1",
kind: "Pod",
metadata: { name: "my-pod" },
spec: {
containers: [{ name: "app", image: "nginx" }],
},
});
cluster.addHelmChart("MyChart", {
chart: "stable/nginx-ingress",
repository: "https://kubernetes-charts.storage.googleapis.com/",
});The kubectl provider supports the following operations:
Create/Update Operations:
Delete Operations:
Query Operations:
The provider includes comprehensive error handling:
// Provider will retry operations with exponential backoff
// Errors are surfaced as CloudFormation custom resource failures
// Logs are available in CloudWatch for troubleshooting
// Example error scenarios:
// - Cluster unreachable
// - Insufficient RBAC permissions
// - Invalid Kubernetes manifests
// - Helm chart installation failures// Best practice: Configure cluster for private kubectl access
const cluster = new eks.Cluster(this, "PrivateCluster", {
version: eks.KubernetesVersion.V1_21,
kubectlMemory: Size.mebibytes(2048),
kubectlPrivateSubnets: vpc.privateSubnets,
kubectlSecurityGroup: kubectlSecurityGroup,
kubectlEnvironment: {
"CLUSTER_NAME": "my-private-cluster",
},
});Common Issues:
Debugging:
// Enable detailed logging
const cluster = new eks.Cluster(this, "Cluster", {
kubectlEnvironment: {
"AWS_LAMBDA_LOG_LEVEL": "DEBUG",
},
});
// Check CloudWatch logs at:
// /aws/lambda/[stack-name]-[cluster-id]-KubectlProvider-Handler