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

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.pyView Source

    # Create an Amazon EKS Cluster control plane without attaching a compute service.
    create_cluster = EKSCreateClusterOperator(
        task_id='create_eks_cluster',
        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.pyView Source

    delete_cluster = EKSDeleteClusterOperator(task_id='delete_eks_cluster')
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.pyView 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', force_delete_compute=True)

Manage Amazon EKS Managed Nodegroups

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.pyView Source

    create_nodegroup = EKSCreateNodegroupOperator(
        task_id='create_eks_nodegroup',
        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.pyView Source

    delete_nodegroup = EKSDeleteNodegroupOperator(
        task_id='delete_eks_nodegroup', 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.pyView 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',
        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.pyView 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_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

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.pyView Source

    create_fargate_profile = EKSCreateFargateProfileOperator(
        task_id='create_eks_fargate_profile',
        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.pyView Source

    delete_fargate_profile = EKSDeleteFargateProfileOperator(
        task_id='delete_eks_fargate_profile',
        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.pyView Source

    start_pod = EKSPodOperator(
        task_id="run_pod",
        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?