Access Amazon S3 from a Private EC2 Instance Using a Gateway Endpoint

How to access Amazon S3 from a Private EC2 Instance Using a Gateway Endpoint


Table of Contents

  1. Introduction
  2. Architecture Overview
  3. Prerequisites
  4. Implementation
  5. Conclusion

1. Introduction

In this tutorial, we will configure an AWS VPC Gateway Endpoint to allow EC2 instances in a private subnet to access Amazon S3 without requiring internet access or a NAT Gateway.

The architecture consists of two subnets: a public subnet hosting a Bastion Host and a private subnet hosting an EC2 instance. The private EC2 instance uses an IAM Role to securely access an S3 bucket through the VPC Gateway Endpoint.

Using a Gateway Endpoint provides private connectivity to S3, eliminates the need for a NAT Gateway for S3 traffic, and helps reduce networking costs.

2. Architecture Overview

Download the editable here → Click here to download

  • Public Subnet: Hosts the Bastion Host for SSH access.
  • Private Subnet: Hosts the EC2 instance that needs S3 access.
  • S3 Gateway Endpoint: Provides private connectivity between the private EC2 instance and S3.
  • IAM Role: Grants the private EC2 instance permission to access S3.

3. Prerequisites

  • An active AWS account
  • Basic understanding of AWS networking
  • Basic Linux and SSH knowledge

4. Implementation

  1. Create VPC with Public and Private Subnets

    This implementation assumes you have already created a VPC with public and private subnets.

    If you haven’t created your VPC and subnets yet, refer to this link: [Building a Secure AWS VPC with Public and Private Subnets].

  2. Launch the Bastion Host and Private EC2 Instance

    This implementation assumes you have already launched a Bastion Host in the public subnet and a private EC2 instance in the private subnet.

    If you haven’t launched these instances yet, please refer to this document: [Building a Secure AWS VPC with Public and Private Subnets]..

    For this lab, we will use the following EC2 instances:

Bastion host

  • Subnet: demo-public-sub-01
  • Private IP: 172.16.0.10
  • Public IP: 54.221.125.192
  • Key pair: ec2-user
  • Security Group: bastion-host-sg

Private EC2 Instance (ec2-01)

  • Subnet: demo-private-sub-01
  • Private IP: 172.16.1.146
  • Public IP: None
  • Key pair: ec2-user
  • Security Group : ec2-01-sg
  1. Create IAM role for EC2 instance to allow S3Full permissions
    1. Go to IAM → Roles → Create role.
    2. Select AWS service as the trusted entity type and select EC2 as the use case.
    3. Attach the AmazonS3FullAccess policy.
    4. Enter a role name, for example, EC2-S3-Access-Role.
    5. Click Create role.
  2. Associate IAM role with the Private EC2 Instance

    To attach the IAM Role to an existing EC2 instance:

    1. Go to EC2 → Instances.
    2. Select your private instance (ec2-01).
    3. Select Actions → Security → Modify IAM role.
    4. Under IAM role, select EC2-S3-Access-Role.
    5. Click Update IAM role.
  3. Test S3 Access Before Creating the Gateway Endpoint

    Before creating the Gateway Endpoint, let’s verify whether the private EC2 instance can access S3.

    1. connect to the Bastion Host using SSH.
      ssh -i ec2-user.pem ec2-user@54.221.125.192
    2. From the Bastion Host, SSH into the private EC2 instance:
      ssh -i ec2-user ec2-user@172.16.1.146
    3. Try listing S3 buckets:
      aws s3 ls

      Expected result: The command should fail or time out if the private subnet has no NAT Gateway or other working network path to S3.

      This confirms that having an IAM Role alone does not provide network connectivity to S3.

  4. Create VPC Gateway endpoint for S3
    1. Go to VPC → Endpoints → Create endpoint.
    2. Enter an endpoint name, for example, s3-gateway-endpoint and under Service category, select AWS services.
    3. Search for the S3 Gateway Endpoint service:
    4. Select your VPC.
    5. Under Route tables, select the route table associated with your private subnet (demo-private-sub-01).
    6. Keep the default endpoint policy for this lab.
    7. Click Create endpoint.
  5. Verify the Private Subnet Route Table
    1. Go to VPC → Route Tables.
    2. Select the route table associated with demo-private-sub-01.
    3. Open the Routes tab.
    4. Verify that AWS has automatically added the S3 Gateway Endpoint route.

    Note: You do not need to manually add this route if you selected the private subnet’s route table when creating the endpoint.

  6. Test S3 Connectivity After Creating the Gateway Endpoint

    Reconnect to your private EC2 instance through the Bastion Host

    1. List S3 Buckets
      aws s3 ls
    2. Create a sample file
      echo "Testing S3 Gateway Endpoint" > test.txt
    3. Upload the file to S3
      aws s3 cp test.txt s3://demo-bucket-abc-nyyon/
    4. In the S3 bucket

    Your private EC2 instance can now access S3 through the VPC Gateway Endpoint.

5. Conclusion

We have successfully configured an S3 Gateway Endpoint to allow an EC2 instance in a private subnet to access an S3 bucket privately using an attached IAM Role.

This architecture is useful when private EC2 instances need to access S3 resources without internet connectivity or a NAT Gateway, improving security while reducing infrastructure costs.