Skip to content

ECS Container using a Task Role can't perform a PutBucketCORS even though it's been granted the permission

0

I'm getting the following error when using the Java AWS SDK 2.x from my ECS container. Here is my error:

software.amazon.awssdk.services.s3.model.S3Exception: User: arn:aws:sts::yyyyyy:assumed-role/myapp_staging_task/324149f2333c47b289fb6561adea6309 is not authorized to perform: s3:PutBucketCORS on resource: "arn:aws:s3:::myapp-staging-2-pub" because no VPC endpoint policy allows the s3:PutBucketCORS action (Service: S3, Status Code: 403, Request ID: 747ADD3QA29N29Q0, Extended Request ID: MmZZYUlsM/r5kFS+lNVFmUHqKIEbfORtXIlD1roMzCtBkmaBSB2AbC9DPN4lMo40bqDxJSu33Pw=) (SDK Attempt Count: 1)

I'm using the default credential provider which should pick up the ECS credentials from the environment. I've confirmed that the environment variable is set inside my docker container:

 wget -O - 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI

And that the RoleARN is pointing to my expected role.

My code is this:

        s3 = S3AsyncClient.builder().multipartEnabled(true).build();
        transferManager = S3TransferManager.builder().s3Client(s3).build();
        ....
            s3.putBucketCors((r) ->
                    r.bucket(bucket)
                            .corsConfiguration((cors) ->
                                    cors.corsRules((rule) ->
                                            rule.allowedMethods("GET", "HEAD")
                                                    .allowedOrigins(String.format("https://*.%s", domain))
                                    )
                            )
            ).join();
            s3.putBucketWebsite((r) ->
                    r.bucket(bucket)
                            .websiteConfiguration((www) -> www.indexDocument((doc) -> doc.suffix("index.html")))
            ).join();

My container's Task Role is set so I'm granting permissions to my container based on this role. The policy attached to the role contains the following permissions:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "s3Access",
            "Effect": "Allow",
            "Action": [
                "s3:CreateBucket",
                "s3:PutObject",
                "s3:GetObject",
                "s3:GetObjectTagging",
                "s3:ListBucket",
                "s3:DeleteObject"
            ],
            "Resource": "*"
        },
        {
            "Sid": "sesEmailAccess",
            "Effect": "Allow",
            "Action": [
                "ses:SendEmail",
                "ses:SendRawEmail"
            ],
            "Resource": "*"
        },
        {
            "Sid": "Cloudwatch",
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData"
            ],
            "Resource": "*"
        },
        {
            "Sid": "ec2Access",
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeTags"
            ],
            "Resource": "*"
        },
        {
            "Sid": "S3PublicBucketConfig",
            "Effect": "Allow",
            "Action": [
                "s3:PutBucketWebsite",
                "s3:PutBucketCORS"
            ],
            "Resource": "arn:aws:s3:::myapp-*-*-pub"
        },
        {
            "Sid": "S3PrivateBucketConfig",
            "Effect": "Allow",
            "Action": [
                "s3:PutEncryptionConfiguration"
            ],
            "Resource": "arn:aws:s3:::myapp-*-*-prv"
        }
    ]
}

Originally I didn't have the PutBucketCORS permission attached to the role. I modified my policy and added it as you see above. I even tested this new version in the policy simulator using the same names of resources and permissions in the error, and it said it passed. I also turned off this policy in the simulator to see it fail so that I know this permission I added was being used to grant permission to PutBucketCORS.

So it seems like my ECS container isn't picking up the changes I made to the Role. I also tried to stop and start the container to see if it would pick up the changes, but it still errored out with the error above. So why isn't my container allowed to perform this operation now?

2 Answers
2
Accepted Answer

Based on the error message and the information provided, it appears that the issue is related to VPC endpoint policies rather than the IAM role permissions. The error specifically mentions "because no VPC endpoint policy allows the s3:PutBucketCORS action". This suggests that your ECS task is running in a VPC with an S3 VPC endpoint, and the endpoint policy is not allowing the PutBucketCORS action.

To resolve this issue, you need to:

  1. Locate the S3 VPC endpoint being used by your ECS task.

  2. Check the policy attached to this VPC endpoint. It should include permissions for the s3:PutBucketCORS action.

  3. Modify the VPC endpoint policy to allow the required S3 actions, including PutBucketCORS. Here's an example of how the policy might look:

    {
      "Statement": [
        {
          "Action": [
            "s3:PutBucketCORS",
            "s3:PutBucketWebsite",
            "s3:CreateBucket",
            "s3:PutObject",
            "s3:GetObject",
            "s3:ListBucket",
            "s3:DeleteObject"
          ],
          "Effect": "Allow",
          "Resource": "*",
          "Principal": "*"
        }
      ]
    }

    Make sure to include all the S3 actions that your application needs to perform.

  4. After updating the VPC endpoint policy, it may take a few minutes for the changes to propagate.

  5. If you're still experiencing issues after updating the VPC endpoint policy, you may need to restart your ECS tasks to ensure they pick up the new permissions.

Remember, when using VPC endpoints, both the IAM role permissions and the VPC endpoint policy need to allow the required actions. The IAM role permissions you've set look correct, so focusing on the VPC endpoint policy should resolve the issue.
Sources
Troubleshoot issues with Amazon ECS connection to Amazon S3 | AWS re:Post
Troubleshoot permissions issues in Amazon ECS | AWS re:Post
Using IAM roles with Amazon ECS tasks - Amazon Elastic Container Service

answered 2 years ago

AWS
SUPPORT ENGINEER

reviewed 2 years ago

EXPERT

reviewed 2 years ago

1

There are couple of things you can check.

  1. VPC endpoint policy as mentioned in previous answer - you can allow all actions and confirm if it works.
  2. S3 bucket policy - check if the bucket policy is allowing this permission.
## bucket policy
{
    "Version": "2008-10-17",
    "Statement": [       
        {
            "Sid": "AllowECS",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::{AccountId}:role/my-role"
            },
            "Action": [
				"s3:PutBucketCORS",
				"s3:PutBucketWebsite",
				"s3:CreateBucket",
				"s3:PutObject",
				"s3:GetObject",
				"s3:ListBucket",
				"s3:DeleteObject"
			  ],
            "Resource": [
				"arn:aws:s3:::my-bucket-{AccountId}/*"
                                "arn:aws:s3:::my-bucket-{AccountId}/*"
            ]            
        }       
    ]
}

Also, why are you updating CORS from ECS? I suspect it's a one off setup and therefore you can just have the setup in S3 directly.

EXPERT

answered 2 years ago

  • So I set my principal in the VPC endpoint policy to {"AWS": "arn:aws:iam::000000000:role/myapp_staging_task"}, but it doesn't not allow it. myapp_staging_task is the task for the ECS task running in the container. However, if I just use Principal: "*" it works. I'm getting awfully tired of buggering around with the policy so I'm inclined to just take the W and move on. So why's it not working?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.