Kha Nguyen
05/14/2021, 2:30 PMimport 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?Kevin Kho
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.Kevin Kho
Kha Nguyen
05/14/2021, 2:41 PMKha Nguyen
05/14/2021, 2:47 PMKevin Kho
Kevin Kho
Kha Nguyen
05/14/2021, 2:50 PMKha Nguyen
05/14/2021, 2:52 PMFailed to load and execute Flow’s environment: NoCredentialsError(‘Unable to locate credentials’)
. So I suppose the agent does not pickup the ~/.aws/credentials infoKevin Kho
Kevin Kho
Kha Nguyen
05/14/2021, 2:59 PMKha Nguyen
05/14/2021, 3:01 PMagent = 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()
Kha Nguyen
05/14/2021, 3:02 PMKevin Kho
Kha Nguyen
05/14/2021, 3:03 PMKevin Kho
ECSRun
on the flow side with ECSRun(…, env={'AWS_ACCESS_KEY_ID': 'aaa', 'AWS_SECRET_ACCESS_KEY': 'bbb'
}Kevin Kho
Kevin Kho
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 workingKevin Kho
Kha Nguyen
05/14/2021, 3:21 PMKha Nguyen
05/14/2021, 3:21 PMKevin Kho
Kevin Kho
Kha Nguyen
05/14/2021, 3:24 PMKevin Kho
Kha Nguyen
05/14/2021, 5:14 PMtask_role_arn
to a role that has access to S3 bucket, and it works. Now I have another error from ECS task
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')")
Kha Nguyen
05/14/2021, 5:14 PMKevin Kho
Kha Nguyen
05/14/2021, 5:27 PMKevin Kho