

# 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](security-iam-trusted-identity-propagation.md#security-iam-tip-prerequisites).
+ Credentials from a sign-in path that supports scheduled runs on your behalf.
+ Permissions to create workflows, including `iam:PassRole` for the execution role.
+ An execution role with `s3:PutObject` permission on the `reports/` prefix of the bucket.

Complete the following steps:

1. Use the Amazon S3 console at [https://console.aws.amazon.com/s3/](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 `{{amzn-s3-demo-bucket}}` with your bucket name and `{{user-subject}}` with your subject. The policy contains only this statement.

   ```json
   {
     "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 `Deny` statement with a `StringNotEquals` condition on `aws:OriginIdentity/Issuer`. If you can't update the bucket policy, make sure that you have the `s3:PutBucketPolicy` permission.

1. Save the following YAML workflow definition as `tip_daily_report.yaml`. The workflow runs once a day and writes an object to the bucket. The `S3CreateObjectOperator` operator is part of the Apache Airflow Amazon provider, which the Amazon MWAA Serverless execution environment includes.

   ```yaml
   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-bucket}}
         s3_key: reports/daily.txt
         data: "Report written with the identity of the workflow owner."
         replace: true
   ```

1. 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
   ```

1. With your credentials, run the following AWS CLI command to create the workflow with `--on-behalf-of ORIGIN_IDENTITY`:

   ```
   aws mwaa-serverless create-workflow \
       --name {{tip-daily-report}} \
       --definition-s3-location '{"Bucket": "{{amzn-s3-demo-bucket}}", "ObjectKey": "dags/tip_daily_report.yaml"}' \
       --role-arn {{arn:aws:iam::111122223333:role/MyMWAAServerlessRole}} \
       --on-behalf-of ORIGIN_IDENTITY \
       --region {{us-east-1}}
   ```

   The command returns a `WorkflowArn`. Use it in the following steps. If the command fails with a `ValidationException`, see [Resolving trusted identity propagation errors](security-iam-trusted-identity-propagation.md#security-iam-tip-errors).

1. Run the following AWS CLI command to check the identity that scheduled runs use:

   ```
   aws mwaa-serverless get-workflow \
       --workflow-arn {{arn:aws:airflow-serverless:us-east-1:111122223333:workflow/tip-daily-report-a1b2c3d4e5}} \
       --region {{us-east-1}}
   ```

   The response includes fields similar to the following JSON:

   ```json
   {
       "OnBehalfOf": {
           "Subject": "{{user-subject}}"
       },
       "IdentityContextStatus": {
           "Status": "ACTIVE",
           "ExpiresAt": "2026-12-07T06:00:00+00:00"
       }
   }
   ```

1. 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-arn {{arn:aws:airflow-serverless:us-east-1:111122223333:workflow/tip-daily-report-a1b2c3d4e5}} \
       --region {{us-east-1}}
   ```

   When the run state is `SUCCESS`, the object `reports/daily.txt` is in the bucket. If a different user created the workflow, the run state is `FAILED`, and the task log shows an `AccessDenied` error for `PutObject`.