This project demonstrates how to securely access an Amazon S3 bucket from an EC2 instance inside a VPC using a VPC Endpoint.
By default, when an EC2 instance communicates with S3, the traffic may route through the public internet.
To improve security and ensure private connectivity, we use an S3 Gateway VPC Endpoint so that traffic stays within the AWS network.
Normally, an EC2 instance can access S3 through the public internet.
Even though the instance runs inside a VPC, services like S3 are not deployed inside the VPC, because they are global/regional AWS-managed services designed for high availability.
✅ Solution: Use a VPC Endpoint to privately connect the VPC to S3 without requiring internet access.
- Create a VPC from scratch
- Launch an EC2 instance and configure access
- Create an S3 bucket
- Add an S3 Gateway VPC Endpoint
- Verify the endpoint by locking down the bucket using a Bucket Policy
- Ensure bucket access is allowed ONLY through the endpoint
EC2 Instance (VPC) → VPC Gateway Endpoint → S3 Bucket
✅ No public internet required
✅ Traffic stays inside AWS backbone network
- Amazon VPC
- EC2
- S3
- VPC Gateway Endpoint (S3)
- IAM / Bucket Policies
- EC2 Instance Connect
- AWS CLI (for testing)
Without a VPC endpoint:
- Traffic from EC2 to S3 can go out through the public route
With a VPC endpoint:
- EC2 can reach S3 privately without internet access
- Improved security + reduced data transfer exposure
To confirm that S3 access is truly private through the endpoint:
- Block all access to the S3 bucket
- Allow access only from the VPC endpoint
- Test from EC2 using AWS CLI
If EC2 can still access → endpoint works ✅
If EC2 is blocked → endpoint is not being used ❌
-
Created a custom VPC
- Subnets
- Route Tables
- Security Groups
-
Launched an EC2 instance
- Connected using EC2 Instance Connect
- Installed / configured AWS CLI
-
Created an S3 bucket
- Uploaded objects for testing
-
Configured an S3 Gateway VPC Endpoint
- Attached endpoint to the VPC route table
-
Applied a restricted bucket policy
- Allowed only
aws:SourceVpceendpoint traffic
- Allowed only
-
Tested access from EC2
- Verified that access works ONLY through the endpoint
Replace
YOUR_BUCKET_NAMEandYOUR_VPCE_ID
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAccessOnlyFromVPCEndpoint",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::YOUR_BUCKET_NAME",
"arn:aws:s3:::YOUR_BUCKET_NAME/*"
],
"Condition": {
"StringEquals": {
"aws:SourceVpce": "YOUR_VPCE_ID"
}
}
}
]
}