I cannot run a simple flow on ECS. I have setup a ...
# ask-community
k
I cannot run a simple flow on ECS. I have setup a simple flow like this
Copy code
import prefect
from prefect import task, Flow
from prefect.run_configs import ECSRun


@task
def hello_task():
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>("Hello World!")
    return 0


task_definition = {
    'networkMode': 'awsvpc',
    'cpu': 1024,
    'memory': 2048,
    'containerDefinitions': [
        {'name': 'flow'}
    ]
}


run_config = ECSRun(
    image='prefecthq/prefect',
    task_definition=task_definition
)

flow = Flow("hello-flow",
            tasks=[hello_task],
            run_config=run_config)

flow.register(project_name='Prefect_Tutorials', labels=['aws', 'ecs', 'fargate'])
The ECS agent can successfully trigger an ECS task. However, the flow run failed with error
Failed to load and execute Flow's environment: ModuleNotFoundError("No module named '/Users/kha/'")
. Why does it include something specific to my name in the flow?
k
Hi @Kha Nguyen I suspect this is because you did not define a
Stoage
in the flow. The storage is the location the agent will pull your Flow from. A lot of people use S3 storage with ECS. The default storage is LocalStorage and saves it in a default path. When your agent picks up the flow, it is looking for the flow in the default path, which is your home directory.
To solve this, you can try S3 Storage . The agent will grab the flow from S3 instead at that bucket you specify
k
Ah I see. Thanks Kevin, I will setup the storage
Does it mean that I have to setup execution role that can access S3 bucket?
k
I think it’s more about the credentials you use having access to S3.
So I guess you need these AWS credentials on the agent too
k
The agent is running locally, and it has superadmin access to AWS
I am seeing
Failed to load and execute Flow’s environment: NoCredentialsError(‘Unable to locate credentials’)
. So I suppose the agent does not pickup the ~/.aws/credentials info
k
you need them as environment variables on the agent
This doc might provide more detail
k
I already specified the credential, but it still cannot locate credentials
Copy code
agent = ECSAgent(
    name='Test Agent',
    region_name='eu-west-1',
    cluster='my-test-cluster',
    labels=['aws', 'ecs', 'fargate'],
    launch_type='FARGATE',
    run_task_kwargs_path='./config.yaml',
    aws_access_key_id='foo',
    aws_secret_access_key='bar'
)

agent.start()
I set my own access key, with all permissions, and it still says that it cannot locate credentials. Is the error coming from agent?
k
Looking into this
k
It seems it is not from agent. It came from running this command on an ECS task.
k
I’m not 100% sure where this is coming from at the moment. Could you try adding the environment variables first through the
ECSRun
on the flow side with
ECSRun(…, env={'AWS_ACCESS_KEY_ID': 'aaa', 'AWS_SECRET_ACCESS_KEY': 'bbb'
}
This will make them env variables on the agent
I re-read and I see what you’re saying. Yes that error is coming from the agent not being to locate credentials. I still want to try the
ECSRun
so we can just pass in our environment variables that way just to try to get it working first, and then we can check why that
agent.start()
script isn’t working
I deleted the previous msg. It was too specific to k8s
k
I tried your suggestion. It is still failing in the same way
I just setup a role with access to S3, and give it to ECSRun, and it is still failing in the same way
k
Let me test it on my end and get back to you
Do you see your file in S3 btw? Just checking if the build part succeeded
k
Yes, the file is visible in S3
k
Thanks!
k
It is the role policy issue. I set the
task_role_arn
to a role that has access to S3 bucket, and it works. Now I have another error from ECS task
Copy code
Failed to load and execute Flow's environment: StorageError("An error occurred while unpickling the flow:\n  TypeError('code() takes at most 15 arguments (16 given)')\nThis may be due to one of the following version mismatches between the flow build and execution environments:\n  - python: (flow built with '3.9.2', currently running with '3.7.10')")
I guess I only have to use matching python version
k
This is image matching yes. We don’t have a 3.9 image and the default image gets 3.7. Images here .
k
Thanks Kevin
k
Glad you got it working 🙂
1