I can't access an Amazon Simple Storage Service (Amazon S3) bucket from my Amazon Elastic Compute Cloud (Amazon EC2) instance.
Resolution
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
Create an IAM instance profile that grants access to Amazon S3
Complete the following steps:
- Open the AWS Identity and Access Management (IAM) console.
- In the navigation pane, under Access management, choose Roles.
- Choose Create role.
- Under Trusted entity type, choose AWS service. For Service or use case, choose EC2. For Use Case, choose EC2.
Note: When you use the IAM console to create an IAM role and choose EC2 as the trusted entity, an IAM instance profile is automatically created. The newly created IAM instance profile has the same name as the role. However, if you use the AWS CLI or an API to create the role, then an instance profile isn't automatically created. For more information, see I created an IAM role, but the role doesn't appear in the dropdown list when I launch an instance. What do I do?
- Choose Next.
- Create a custom policy that provides the minimum required permissions to access your S3 bucket.
Note: It's a security best practice to create a policy with the minimum required permissions. However, to give Amazon EC2 access to all of your S3 buckets, use the AmazonS3ReadOnlyAccess or AmazonS3FullAccess managed IAM policy.
- Select the required policy, and then choose Next.
- Enter a role name, and then choose Create role.
Attach the IAM instance profile to the EC2 instance
Complete the following steps:
- Open the Amazon EC2 console.
- In the navigation pane, choose Instances.
- Select the instance that you want to attach the IAM role to.
- Choose Actions, and then choose Security.
- Choose Modify IAM role.
- Select the IAM role you just created, and then choose Save. The IAM role is assigned to your EC2 instance.
Verify permissions on your S3 bucket
Complete the following steps:
- Open the Amazon S3 console.
- Select the S3 bucket link that you want to verify the policy for.
- Choose the Permissions tab.
- In the Bucket policy section, search for statements that contain Effect: Deny.
- Edit the IAM policy to remove any Effect: Deny statements that deny the IAM instance profile access to your bucket.
Verify network connectivity from the EC2 instance to Amazon S3
Prerequisites
- An EC2 instance with a public IP address and a route table entry with the default route to an internet gateway.
- A private EC2 instance with a default route through an NAT gateway.
- A private EC2 instance with connectivity to Amazon S3 with a gateway virtual private cloud (VPC) endpoint.
To troubleshoot connectivity between a private EC2 instance and an S3 bucket, see How do I troubleshoot connectivity issues with my gateway Amazon VPC endpoints?
Verify access to S3 buckets
Complete the following steps:
- Install the AWS CLI on your EC2 instance.
- Run the following command to verify access to your S3 buckets:
aws s3 ls s3://DOC-EXAMPLE-BUCKET
Note: Replace DOC-EXAMPLE-BUCKET with the name of your S3 bucket.
For S3 objects that are encrypted with an AWS Key Management Service (AWS KMS) key, grant kms: Decrypt permissions in the IAM role and KMS key policy. If you don't grant kms:Decrypt permissions, then you can't copy or download the S3 objects.
Related information
Identity and Access Management for Amazon S3
How do I troubleshoot connectivity issues when I use interface VPC endpoints to connect to my Amazon S3 bucket?