Example – Restrict Amazon S3 writes to a user's scheduled workflow
In this example, you create a scheduled workflow that writes an object to Amazon S3. An Amazon S3 bucket policy allows the write only when the request carries your identity. If another user creates the same workflow, the task fails with an AccessDenied error.
Before you begin, make sure that you have the following:
-
The setup in Completing the prerequisites.
-
Credentials from a sign-in path that supports scheduled runs on your behalf.
-
Permissions to create workflows, including
iam:PassRolefor the execution role. -
An execution role with
s3:PutObjectpermission on thereports/prefix of the bucket.
Complete the following steps:
-
Use the Amazon S3 console at https://console.aws.amazon.com/s3/
or the AWS CLI to add a statement to your bucket policy. The statement denies writes to the reports/prefix unless the request carries your subject.In the following JSON bucket policy, replace
with your bucket name andamzn-s3-demo-bucketwith your subject. The policy contains only this statement.user-subject{ "Version": "2012-10-17", "Statement": [ { "Sid": "DenyWritesWithoutExpectedUser", "Effect": "Deny", "Principal": {"AWS": "*"}, "Action": "s3:PutObject", "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/reports/*", "Condition": { "StringNotEquals": { "aws:OriginIdentity/Subject": "user-subject" } } } ] }The statement also denies requests without an identity, including requests that use only the execution role. To also limit which identity provider the subject comes from, add a separate
Denystatement with aStringNotEqualscondition onaws:OriginIdentity/Issuer. If you can't update the bucket policy, make sure that you have thes3:PutBucketPolicypermission. -
Save the following YAML workflow definition as
tip_daily_report.yaml. The workflow runs once a day and writes an object to the bucket. TheS3CreateObjectOperatoroperator is part of the Apache Airflow Amazon provider, which the Amazon MWAA Serverless execution environment includes.tip_daily_report: default_args: retries: 0 schedule: "0 6 * * *" tasks: write_report: operator: airflow.providers.amazon.aws.operators.s3.S3CreateObjectOperator s3_bucket:amzn-s3-demo-buckets3_key: reports/daily.txt data: "Report written with the identity of the workflow owner." replace: true -
Upload the workflow definition to the
dags/prefix of the same bucket.aws s3 cp tip_daily_report.yaml \ s3://amzn-s3-demo-bucket/dags/tip_daily_report.yaml -
With your credentials, run the following AWS CLI command to create the workflow with
--on-behalf-of ORIGIN_IDENTITY:aws mwaa-serverless create-workflow \ --nametip-daily-report\ --definition-s3-location '{"Bucket": "amzn-s3-demo-bucket", "ObjectKey": "dags/tip_daily_report.yaml"}' \ --role-arnarn:aws:iam::111122223333:role/MyMWAAServerlessRole\ --on-behalf-of ORIGIN_IDENTITY \ --regionus-east-1The command returns a
WorkflowArn. Use it in the following steps. If the command fails with aValidationException, see Resolving trusted identity propagation errors. -
Run the following AWS CLI command to check the identity that scheduled runs use:
aws mwaa-serverless get-workflow \ --workflow-arnarn:aws:airflow-serverless:us-east-1:111122223333:workflow/tip-daily-report-a1b2c3d4e5\ --regionus-east-1The response includes fields similar to the following JSON:
{ "OnBehalfOf": { "Subject": "user-subject" }, "IdentityContextStatus": { "Status": "ACTIVE", "ExpiresAt": "2026-12-07T06:00:00+00:00" } } -
List the workflow's runs and check the run's state after the next scheduled run. The workflow runs once a day, so the first scheduled run can take up to 24 hours to start.
aws mwaa-serverless list-workflow-runs \ --workflow-arnarn:aws:airflow-serverless:us-east-1:111122223333:workflow/tip-daily-report-a1b2c3d4e5\ --regionus-east-1When the run state is
SUCCESS, the objectreports/daily.txtis in the bucket. If a different user created the workflow, the run state isFAILED, and the task log shows anAccessDeniederror forPutObject.