max_active_runs=1, tags=['example', 'templated'], # render_template_as_native_obj=True is what converts the Jinja to Python objects, instead of a string. render_template_as_native_obj=True, ) as dag: # Create an Amazon EKS Cluster control plane without attaching a compute service. create_cluster = EKSCreateClusterOperator( task_id='create_eks_cluster', compute=None, cluster_role_arn="{{ dag_run.conf['cluster_role_arn'] }}", resources_vpc_config="{{ dag_run.conf['resources_vpc_config'] }}", ) await_create_cluster = EKSClusterStateSensor( task_id='wait_for_create_cluster', target_state=ClusterStates.ACTIVE, ) create_nodegroup = EKSCreateNodegroupOperator( task_id='create_eks_nodegroup', nodegroup_name="{{ dag_run.conf['nodegroup_name'] }}", nodegroup_subnets="{{ dag_run.conf['nodegroup_subnets'] }}", nodegroup_role_arn="{{ dag_run.conf['nodegroup_role_arn'] }}", ) await_create_nodegroup = EKSNodegroupStateSensor( task_id='wait_for_create_nodegroup', nodegroup_name="{{ dag_run.conf['nodegroup_name'] }}", target_state=NodegroupStates.ACTIVE, )
# render_template_as_native_obj=True is what converts the Jinja to Python objects, instead of a string. render_template_as_native_obj=True, ) as dag: # Create an Amazon EKS Cluster control plane without attaching a compute service. create_cluster = EKSCreateClusterOperator( task_id='create_eks_cluster', compute=None, cluster_name="{{ dag_run.conf['cluster_name'] }}", cluster_role_arn="{{ dag_run.conf['cluster_role_arn'] }}", resources_vpc_config="{{ dag_run.conf['resources_vpc_config'] }}", ) await_create_cluster = EKSClusterStateSensor( task_id='wait_for_create_cluster', cluster_name="{{ dag_run.conf['cluster_name'] }}", target_state=ClusterStates.ACTIVE, ) create_nodegroup = EKSCreateNodegroupOperator( task_id='create_eks_nodegroup', cluster_name="{{ dag_run.conf['cluster_name'] }}", nodegroup_name="{{ dag_run.conf['nodegroup_name'] }}", nodegroup_subnets="{{ dag_run.conf['nodegroup_subnets'] }}", nodegroup_role_arn="{{ dag_run.conf['nodegroup_role_arn'] }}", ) await_create_nodegroup = EKSNodegroupStateSensor( task_id='wait_for_create_nodegroup', cluster_name="{{ dag_run.conf['cluster_name'] }}", nodegroup_name="{{ dag_run.conf['nodegroup_name'] }}",
nodegroup_name=NODEGROUP_NAME, target_state=NodegroupStates.ACTIVE, ) start_pod = EKSPodOperator( task_id="run_pod", cluster_name=CLUSTER_NAME, 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, ) # [START howto_operator_eks_force_delete_cluster] # An Amazon EKS cluster can not be deleted with attached resources. # 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 ) # [END howto_operator_eks_force_delete_cluster] await_delete_cluster = EKSClusterStateSensor( task_id='wait_for_delete_cluster', cluster_name=CLUSTER_NAME, target_state=ClusterStates.NONEXISTENT, ) create_cluster_and_nodegroup >> await_create_nodegroup >> start_pod >> delete_all >> await_delete_cluster