Skip to content

VPC Endpoints

Category Cloud & AWS Networking
Status โœ… Completed
Repo nextwork-vpc-endpoints
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's VPC Endpoints guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ Cloud & AWS Networking
Status: โœ… Completed

Overview

This adds a gateway VPC endpoint so the instance reaches S3 over AWS's private network instead of the internet. It then locks the bucket with a policy that denies every request not arriving through that endpoint (aws:sourceVpce). The guide shows that this blocks even the console and your laptop.

Summary

Real AWS proof of the endpoint-only bucket policy: from the EC2 instance, S3 access through the gateway endpoint was allowed. With the endpoint removed from the route table, the same call got AccessDenied (explicit deny in the bucket policy). Re-attaching the endpoint allowed it again. Everything was torn down afterwards.

The checks behind this are in docs/verification.txt.

Skills & tools

Amazon VPC, Subnets, CIDR, Terraform, Infrastructure as Code, boto3, moto (AWS mock), VPC endpoints, S3 bucket policies, IAM conditions, Amazon S3

What I built

  • main.tf: the access-S3 setup, plus aws_vpc_endpoint (Gateway, S3) on the public route table and an aws_s3_bucket_policy that denies unless aws:sourceVpce matches.
  • checks.py: verifies the endpoint type and service, the route-table attachment, and the policy condition.

How to run

pip install -r requirements-dev.txt   # moto (local AWS mock) + boto3
./local-test.sh                       # terraform apply against the mock, run checks.py, then destroy

# Real AWS (costs money; use your own IAM user/profile):
terraform init && terraform apply      # mock_endpoint defaults to "" = real AWS
terraform destroy                      # clean up when done

Verification

I ran the verification command on 2026-12-02. The full output is in docs/verification.txt.

PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH TF_PLUGIN_CACHE_DIR=/workspace/nextwork-portfolio/tf-plugin-cache ./local-test.sh

Real AWS deployment (2026-12-02)

I deployed this project to a real, dedicated AWS learning account (us-east-1, kept separate from everything else I run), checked it, captured the output, and tore it all down right after. Every resource carried the tag Project=nextwork-vpc-endpoints so I could prove nothing was left behind.

How I ran it: ./real-aws-run.sh (in this repo) does terraform init/apply with my AWS profile, runs checks.py against the real account, runs real-extra.sh for the live tests, and then always runs terraform destroy, even if a step fails. To get onto the instances I used EC2 Instance Connect with a throwaway SSH key that is only valid for 60 seconds. No key pairs were created in AWS and no keys were saved. One safety change from the guide: the deny policy exempts the admin principal that runs Terraform (aws:PrincipalArn). Without that, the policy locks out everyone except the root user, including the teardown.

Evidence (excerpt; the full, sanitized transcript is in docs/aws-real-run.txt, with the account ID redacted):

$ python3 checks.py   (against the real account)
ALL 8 CHECKS PASSED
$ ./real-extra.sh
--- public route table now has an S3 prefix-list route via the endpoint
10.0.0.0/16 local
0.0.0.0/0   igw-08a6db9916c40bb03
pl-63a5400a vpce-0bcdb7553610809d5
--- 1) from the instance, via the endpoint: allowed
2026-12-03 02:00:49         17 nextwork-file-1.txt
2026-12-03 02:00:49         17 nextwork-file-2.txt
--- 2) detach the endpoint from the route table -> S3 traffic leaves via the internet gateway instead
True
aws: [ERROR]: An error occurred (AccessDenied) when calling the ListObjectsV2 operation: User: arn:aws:sts::<ACCOUNT_ID>:assumed-role/NextWorkEC2S3Access/i-03d28bc5ef060f731 is not authorized to perform: s3:ListBucket on resource: "arn:aws:s3:::nextwork-vpc-project-fb906741" with an explicit deny in a resource-based policy
(AccessDenied expected: the bucket policy denies anything that doesn't arrive via the endpoint)
--- 3) re-attach the endpoint -> allowed again
True
2026-12-03 02:00:49         17 nextwork-file-1.txt
2026-12-03 02:00:49         17 nextwork-file-2.txt

Teardown

Resources were up from 20:00 to 20:04 CST on 2026-12-02, then destroyed and verified gone:

=== TEARDOWN 2026-12-02 20:03 CST ===
$ terraform destroy -auto-approve -input=false -no-color -var ami_id=ami-000d209a4fc9822c4 -var project_tag=nextwork-vpc-endpoints -var ssh_cidr=0.0.0.0/0
Destroy complete! Resources: 18 destroyed.
$ terraform state list   (should be empty)
$ aws resourcegroupstaggingapi get-resources --tag-filters Key=Project,Values=nextwork-vpc-endpoints
$ live EC2 instances / volumes / VPCs tagged Project=nextwork-vpc-endpoints (should be empty)
=== teardown verified 20:04 CST ===

Cost

Estimated cost of the real run: about $0.01 (one t2.micro for about 4 minutes; gateway endpoints for S3 are free). The account has a $50/month budget with email alerts at $5, $20 and $40, and the whole AWS series is logged in a private spend ledger.


Original project: https://nextwork.ai/projects/749fab76-0a0b-5822-9b31-c7863050c9bd ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH TF_PLUGIN_CACHE_DIR=/workspace/nextwork-portfolio/tf-plugin-cache ./local-test.sh

Success! The configuration is valid.

Apply complete! Resources: 17 added, 0 changed, 0 destroyed.
bucket = "nextwork-vpc-project-579cab60"
endpoint_id = "vpce-f9e87798bca462a87"
instance_id = "i-06dde682e647a37ec"
vpc_id = "vpc-94f3746fcac123d9b"
PASS: bucket holds the 2 project files (['nextwork-file-1.txt', 'nextwork-file-2.txt'])
PASS: PutObject from outside the endpoint is denied by the bucket policy (moto enforced it: 403)
PASS: instance has the S3 instance profile (no static access keys)
PASS: role policy scoped to this bucket only
PASS: S3 gateway endpoint in the VPC
PASS: endpoint attached to the public route table
PASS: bucket policy denies all access not via the endpoint
PASS: bucket still holds the 2 project files
ALL 8 CHECKS PASSED

[exit code 0]