The prefect-aws library makes it easy to leverage the capabilities of AWS in your workflows.
For example, you can retrieve secrets using AWS Secrets Manager, read and write objects with AWS S3, and deploy your flows on AWS ECS.
Create an AWSCredenitals block in the Prefect UI or use a Python script like the one below.
fromprefect_awsimportAwsCredentialsAwsCredentials(aws_access_key_id="PLACEHOLDER",aws_secret_access_key="PLACEHOLDER",aws_session_token=None,# replace this with token if necessaryregion_name="us-east-2").save("BLOCK-NAME-PLACEHOLDER")
Prefect is using the Boto3 library under the hood.
To find credentials for authentication, any data not provided to the block are sourced at runtime in the order shown in the Boto3 docs.
Prefect creates the session object using the values in the block and then, any missing values follow the sequence in the Boto3 docs.
See an example of using the AwsCredentials block with AWS Secrets Manager with third-party services without storing credentials in the block itself in this guide.
Upload a file to an AWS S3 bucket and download the same file under a different file name.
The following code assumes that the bucket already exists:
frompathlibimportPathfromprefectimportflowfromprefect_awsimportAwsCredentials,S3Bucket@flowdefs3_flow():# create a dummy file to uploadfile_path=Path("test-example.txt")file_path.write_text("Hello, Prefect!")aws_credentials=AwsCredentials.load("BLOCK-NAME-PLACEHOLDER")s3_bucket=S3Bucket(bucket_name="BUCKET-NAME-PLACEHOLDER",credentials=aws_credentials)s3_bucket_path=s3_bucket.upload_from_path(file_path)downloaded_file_path=s3_bucket.download_object_to_path(s3_bucket_path,"downloaded-test-example.txt")returndownloaded_file_path.read_text()if__name__=="__main__":s3_flow()