
AWS-Managed Buckets That Must Be Allowed When Restricting an S3 Gateway Endpoint Policy to Your Own Account Only
This page has been translated by machine translation. View original
Hello, this is Hayashi.
By adding an aws:ResourceAccount condition to the policy of an S3 gateway endpoint, you can restrict the buckets accessible from the VPC to only those in your own account.
The official documentation has the following note about this condition.
To specify the AWS account ID of the resource being accessed, you can use either the
aws:ResourceAccountor thes3:ResourceAccountkey in your IAM policy. Note, however, that some AWS services rely on access to AWS managed buckets. Therefore, using theaws:ResourceAccountors3:ResourceAccountkey in your IAM policy might also affect access to these resources.
The AWS managed buckets mentioned in the note are, for example, the buckets that hold ECR image layers and Amazon Linux 2023 packages. If you restrict access to your own account only, access to these is also denied.
On an EC2 instance in a private subnet with no internet access, I checked what fails and which buckets need to be allowed for things to succeed.
Conclusion first
I tried a policy that allows only my own account in the Tokyo Region. Retrieving from a bucket in my own account succeeded, and four operations failed with S3 403 errors.
| Operation | Bucket that failed to be retrieved from |
|---|---|
| Package retrieval with dnf | al2023-repos-ap-northeast-1-de612dc2 |
| Image retrieval from ECR (docker pull) | prod-ap-northeast-1-starport-layer-bucket |
| Patch Manager scan (AWS-RunPatchBaseline) | aws-ssm-ap-northeast-1, patch-baseline-snapshot-ap-northeast-1 |
| SSM Agent update (AWS-UpdateSSMAgent) | amazon-ssm-ap-northeast-1 |
When I allowed s3:GetObject on AWS managed buckets including these, all four succeeded.
Here is the policy with the added permissions.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Access-to-own-account-buckets-only",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:ResourceAccount": "123456789012"
}
}
},
{
"Sid": "Access-to-AWS-managed-buckets",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::prod-ap-northeast-1-starport-layer-bucket/*",
"arn:aws:s3:::al2023-repos-ap-northeast-1-de612dc2/*",
"arn:aws:s3:::amazon-ssm-ap-northeast-1/*",
"arn:aws:s3:::aws-ssm-ap-northeast-1/*",
"arn:aws:s3:::patch-baseline-snapshot-ap-northeast-1/*",
"arn:aws:s3:::aws-patch-manager-ap-northeast-1-4849fa78f/*",
"arn:aws:s3:::ap-northeast-1-birdwatcher-prod/*"
]
}
]
}
The only action allowed is s3:GetObject. The examples in the ECR and Systems Manager guides also use only s3:GetObject, and in my testing the five operations succeeded without s3:ListBucket.
aws-patch-manager-ap-northeast-1-4849fa78f and ap-northeast-1-birdwatcher-prod were not confirmed in this test, but I included them following the list in the Systems Manager guide.
Bucket names are determined per Region and are documented in the AWS user guides and AWS re:Post Knowledge Center articles.
| Service | Bucket name | Where documented |
|---|---|---|
| ECR (image layers) | prod-{region}-starport-layer-bucket | ECR User Guide "Minimum Amazon S3 bucket permissions for Amazon ECR" |
| Amazon Linux 2023 (dnf) | al2023-repos-{region}-de612dc2 | AWS re:Post "How do I update yum or install packages without internet access" |
| SSM Agent update | amazon-ssm-{region} | Systems Manager User Guide "SSM Agent communications with AWS managed S3 buckets" |
| Patch Manager materials | aws-ssm-{region} | Same as above |
| Patch baseline snapshots | patch-baseline-snapshot-{region} or patch-baseline-snapshot-{region}-{suffix} | Same as above |
| Patch Manager documents | aws-patch-manager-{region}-{suffix} | Same as above |
| Distributor | {region}-birdwatcher-prod | Same as above |
{suffix} differs by Region. The actual names for each Region are listed in "Reference: Amazon S3 buckets for patching operations".
The Systems Manager guide also has the following note about not using a wildcard for the Region.
We recommend that you don't use wildcard characters (*) in place of specific Regions in this policy. For example, use
arn:aws:s3:::aws-ssm-us-east-2/*and notarn:aws:s3:::aws-ssm-*/*. Using wildcards could provide access to S3 buckets that you don't want to grant access to. If you want to use the instance profile for more than one Region, we recommend repeating the firstStatementblock for each Region.
If you use Windows Server, you also need aws-windows-downloads-{region}, which is listed in the same guide. Since this test used Amazon Linux 2023, I did not include it in the policy.
Test method
| Item | Details |
|---|---|
| EC2 | t4g.small, Amazon Linux 2023 (arm64) |
| Network | Private subnet in a VPC with no internet gateway or NAT gateway |
| VPC endpoint (gateway type) | S3 |
| VPC endpoints (interface type) | ssm, ssmmessages, ec2messages, ecr.api, ecr.dkr |
| Docker | 25.0.14 |
| SSM Agent | 3.3.5390.0 |
| Region | Tokyo (ap-northeast-1) |
The EC2 IAM role allowed s3:Get* and s3:List* on all resources, so the only condition I changed was the endpoint policy. The target of docker pull was amazonlinux:2023 placed in a private ECR repository in my own account.
I switched the S3 gateway endpoint policy among three kinds and ran the same five operations with Run Command for each.
| Policy | Details |
|---|---|
| Default | Allow all (the policy at endpoint creation) |
| Own account only | Allow s3:* only when aws:ResourceAccount is my own account |
| Own account + AWS managed buckets | The policy from "Conclusion first" |
Here is the "Own account only" policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Access-to-own-account-buckets-only",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:ResourceAccount": "123456789012"
}
}
}
]
}
The five operations I ran.
- Retrieve an object from a bucket in my own account with
aws s3 cp - Retrieve packages with
dnf install - Retrieve an ECR image with
docker pull - Run
AWS-RunPatchBaselinewith Scan - Run
AWS-UpdateSSMAgent
Results
○ means success and × means failure.
| Operation | Default | Own account only | Own account + AWS managed buckets |
|---|---|---|---|
| Retrieval from own account bucket | ○ | ○ | ○ |
| Package retrieval with dnf | ○ | × | ○ |
| Image retrieval from ECR | ○ | × | ○ |
| Patch Manager scan | ○ | × | ○ |
| SSM Agent update | ○ | × | ○ |
Running Run Command and connecting with Session Manager do not use S3, so they succeeded with every policy.
Errors on failure
These are the errors of the four operations that failed with the "Own account only" policy. In every case, the name of the bucket that could not be retrieved from with a 403 error appears.
dnf
It failed when retrieving the repository mirror list.
Error: Failed to download metadata for repo 'kernel-livepatch': Cannot prepare internal mirrorlist: Status code: 403 for https://al2023-repos-ap-northeast-1-de612dc2.s3.dualstack.ap-northeast-1.amazonaws.com/kernel-livepatch/mirrors/al2023/aarch64/mirror.list?instance_id=i-0554d801acea99583 (IP: 52.219.16.122)
docker pull
ECR authentication and manifest retrieval succeeded, and it failed when retrieving the layers. The error message also shows that it was the VPC endpoint policy that denied the request.
denied: User: arn:aws:sts::xxxxxxxxxxxx:assumed-role/storage-access-role-prod-ap-northeast-1/xxxxxxxx is not authorized to perform: s3:GetObject on resource: "arn:aws:s3:::prod-ap-northeast-1-starport-layer-bucket/..." because no VPC endpoint policy allows the s3:GetObject action
AWS-RunPatchBaseline
It failed when retrieving the patching materials.
root [ERROR]: Unable to download payload: https://s3.ap-northeast-1.amazonaws.com/aws-ssm-ap-northeast-1/patchbaselineoperations/linux/payloads/patch-baseline-operations-1.252.tar.gz.
When the materials were cached from a previous run, it failed at the next step, retrieving the patch baseline snapshot. This error does not show the bucket name, so I confirmed patch-baseline-snapshot-ap-northeast-1, where the snapshots are stored, in the Systems Manager guide.
root [ERROR]: Could not download a valid snapshot
AWS-UpdateSSMAgent
It failed when retrieving the manifest used to check whether an update exists.
failed to download file reliably, https://s3.ap-northeast-1.amazonaws.com/amazon-ssm-ap-northeast-1/ssm-agent-manifest.json, http request failed. status:403 Forbidden statuscode:403
Summary
I checked which AWS services fail, and which buckets need to be allowed, when the S3 gateway endpoint policy is restricted to only your own account.
- ECR image retrieval, dnf on Amazon Linux 2023, Patch Manager, and SSM Agent updates cannot retrieve from AWS managed buckets and fail with 403 errors
- The error on failure shows the name of the bucket that could not be retrieved from
- The bucket names that need to be allowed are determined per Region and are documented in the AWS user guides and AWS re:Post
- When
s3:GetObjectis allowed on the AWS managed buckets, every operation succeeds
When restricting access with aws:ResourceAccount, you need to check which AWS managed buckets the services you use rely on and allow them.
I hope this article is helpful to someone. Thank you for reading to the end!
References
