Amazon Elastic Kubernetes Service (EKS) Operators

Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane. Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications.

Airflow provides operators to create and interact with the EKS clusters and compute infrastructure.

Prerequisite Tasks

Manage Amazon EKS Clusters

Amazon EKS Cluster State Sensor

To check the state of an Amazon EKS Cluster until it reaches the target state or another terminal state you can use EksClusterStateSensor.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.py[source]

await_create_cluster = EksClusterStateSensor(
    task_id='wait_for_create_cluster',
    cluster_name=CLUSTER_NAME,
    target_state=ClusterStates.ACTIVE,
)

Create an Amazon EKS Cluster

To create an Amazon EKS Cluster you can use EksCreateClusterOperator.

Note: An AWS IAM role with the following permissions is required:

eks.amazonaws.com must be added to the Trusted Relationships AmazonEKSClusterPolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.py[source]

# Create an Amazon EKS Cluster control plane without attaching compute service.
create_cluster = EksCreateClusterOperator(
    task_id='create_eks_cluster',
    cluster_name=CLUSTER_NAME,
    cluster_role_arn=ROLE_ARN,
    resources_vpc_config=VPC_CONFIG,
    compute=None,
)

Delete an Amazon EKS Cluster

To delete an existing Amazon EKS Cluster you can use EksDeleteClusterOperator.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.py[source]

delete_cluster = EksDeleteClusterOperator(
    task_id='delete_eks_cluster',
    cluster_name=CLUSTER_NAME,
)
Note: If the cluster has any attached resources, such as an Amazon EKS Nodegroup or AWS

Fargate profile, the cluster can not be deleted. Using the force parameter will attempt to delete any attached resources first.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroup_in_one_step.py[source]

# An Amazon EKS cluster can not be deleted with attached resources such as nodegroups or Fargate profiles.
# Setting the `force` to `True` will delete any attached resources before deleting the cluster.
delete_all = EksDeleteClusterOperator(
    task_id='delete_nodegroup_and_cluster',
    cluster_name=CLUSTER_NAME,
    force_delete_compute=True,
)

Manage Amazon EKS Managed Nodegroups

Amazon EKS Managed Nodegroup State Sensor

To check the state of an Amazon EKS managed node group until it reaches the target state or another terminal state you can use EksNodegroupStateSensor.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.py[source]

await_create_nodegroup = EksNodegroupStateSensor(
    task_id='wait_for_create_nodegroup',
    cluster_name=CLUSTER_NAME,
    nodegroup_name=NODEGROUP_NAME,
    target_state=NodegroupStates.ACTIVE,
)

Create an Amazon EKS Managed NodeGroup

To create an Amazon EKS Managed Nodegroup you can use EksCreateNodegroupOperator.

Note: An AWS IAM role with the following permissions is required:

ec2.amazon.aws.com must be in the Trusted Relationships AmazonEC2ContainerRegistryReadOnly IAM Policy must be attached AmazonEKSWorkerNodePolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.py[source]

create_nodegroup = EksCreateNodegroupOperator(
    task_id='create_eks_nodegroup',
    cluster_name=CLUSTER_NAME,
    nodegroup_name=NODEGROUP_NAME,
    nodegroup_subnets=SUBNETS,
    nodegroup_role_arn=ROLE_ARN,
)

Delete an Amazon EKS Managed Nodegroup

To delete an existing Amazon EKS Managed Nodegroup you can use EksDeleteNodegroupOperator.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.py[source]

delete_nodegroup = EksDeleteNodegroupOperator(
    task_id='delete_eks_nodegroup',
    cluster_name=CLUSTER_NAME,
    nodegroup_name=NODEGROUP_NAME,
)

Create an Amazon EKS Cluster and Nodegroup in one step

To create an Amazon EKS Cluster and an EKS Managed Nodegroup in one command, you can use EksCreateClusterOperator.

Note: An AWS IAM role with the following permissions is required:

ec2.amazon.aws.com must be in the Trusted Relationships eks.amazonaws.com must be added to the Trusted Relationships AmazonEC2ContainerRegistryReadOnly IAM Policy must be attached AmazonEKSClusterPolicy IAM Policy must be attached AmazonEKSWorkerNodePolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroup_in_one_step.py[source]

# Create an Amazon EKS cluster control plane and an EKS nodegroup compute platform in one step.
create_cluster_and_nodegroup = EksCreateClusterOperator(
    task_id='create_eks_cluster_and_nodegroup',
    cluster_name=CLUSTER_NAME,
    nodegroup_name=NODEGROUP_NAME,
    cluster_role_arn=ROLE_ARN,
    nodegroup_role_arn=ROLE_ARN,
    # Opting to use the same ARN for the cluster and the nodegroup here,
    # but a different ARN could be configured and passed if desired.
    resources_vpc_config=VPC_CONFIG,
    # Compute defaults to 'nodegroup' but is called out here for the purposed of the example.
    compute='nodegroup',
)

Create an Amazon EKS Cluster and AWS Fargate profile in one step

To create an Amazon EKS Cluster and an AWS Fargate profile in one command, you can use EksCreateClusterOperator.

Note: An AWS IAM role with the following permissions is required:

ec2.amazon.aws.com must be in the Trusted Relationships eks.amazonaws.com must be added to the Trusted Relationships AmazonEC2ContainerRegistryReadOnly IAM Policy must be attached AmazonEKSClusterPolicy IAM Policy must be attached AmazonEKSWorkerNodePolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_with_fargate_in_one_step.py[source]

# Create an Amazon EKS cluster control plane and an AWS Fargate compute platform in one step.
create_cluster_and_fargate_profile = EksCreateClusterOperator(
    task_id='create_eks_cluster_and_fargate_profile',
    cluster_name=CLUSTER_NAME,
    cluster_role_arn=ROLE_ARN,
    resources_vpc_config=VPC_CONFIG,
    compute='fargate',
    fargate_profile_name=FARGATE_PROFILE_NAME,
    # Opting to use the same ARN for the cluster and the pod here,
    # but a different ARN could be configured and passed if desired.
    fargate_pod_execution_role_arn=ROLE_ARN,
)

Manage AWS Fargate Profiles

AWS Fargate Profile State Sensor

To check the state of an AWS Fargate profile until it reaches the target state or another terminal state you can use EksFargateProfileSensor.

airflow/providers/amazon/aws/example_dags/example_eks_with_fargate_profile.py[source]

await_create_fargate_profile = EksFargateProfileStateSensor(
    task_id='wait_for_create_fargate_profile',
    cluster_name=CLUSTER_NAME,
    fargate_profile_name=FARGATE_PROFILE_NAME,
    target_state=FargateProfileStates.ACTIVE,
)

Create an AWS Fargate Profile

To create an AWS Fargate Profile you can use EksCreateFargateProfileOperator.

Note: An AWS IAM role with the following permissions is required:

ec2.amazon.aws.com must be in the Trusted Relationships AmazonEC2ContainerRegistryReadOnly IAM Policy must be attached AmazonEKSWorkerNodePolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_with_fargate_profile.py[source]

create_fargate_profile = EksCreateFargateProfileOperator(
    task_id='create_eks_fargate_profile',
    cluster_name=CLUSTER_NAME,
    pod_execution_role_arn=ROLE_ARN,
    fargate_profile_name=FARGATE_PROFILE_NAME,
    selectors=SELECTORS,
)

Delete an AWS Fargate Profile

To delete an existing AWS Fargate Profile you can use EksDeleteFargateProfileOperator.

airflow/providers/amazon/aws/example_dags/example_eks_with_fargate_profile.py[source]

delete_fargate_profile = EksDeleteFargateProfileOperator(
    task_id='delete_eks_fargate_profile',
    cluster_name=CLUSTER_NAME,
    fargate_profile_name=FARGATE_PROFILE_NAME,
)

Perform a Task on an Amazon EKS Cluster

To run a pod on an existing Amazon EKS Cluster, you can use EksPodOperator.

Note: An Amazon EKS Cluster with underlying compute infrastructure is required.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.py[source]

start_pod = EksPodOperator(
    task_id="run_pod",
    cluster_name=CLUSTER_NAME,
    pod_name="run_pod",
    image="amazon/aws-cli:latest",
    cmds=["sh", "-c", "ls"],
    labels={"demo": "hello_world"},
    get_logs=True,
    # Delete the pod when it reaches its final state, or the execution is interrupted.
    is_delete_operator_pod=True,
)

Reference

For further information, look at:

Was this entry helpful?