Dev.to Security 🔐 Cybersecurity 👁 0 📖 5 min read

Calling Amazon Bedrock from a Private VPC Without Internet Access (AWS PrivateLink)

AWS PrivateLink is often easier to understand when you see it working in a small test rather than only reading documentation. In this short lab, we will: Create a VPC with no Internet Gateway and no NAT Gateway. Deplo

Calling Amazon Bedrock from a Private VPC Without Internet Access (AWS PrivateLink)

AWS PrivateLink is often easier to understand when you see it working in a small test rather than only reading documentation.

In this short lab, we will:

  1. Create a VPC with no Internet Gateway and no NAT Gateway.
  2. Deploy a Python Lambda function inside a private subnet in that VPC.
  3. Add an Interface VPC Endpoint for Amazon Bedrock (bedrock-runtime).
  4. Run one test that checks whether https://example.com is reachable (it should fail) and whether Amazon Bedrock (amazon.nova-micro-v1:0) still responds over a private IP.

Source code and IAM policies: github.com/Damdev-95/aws-private-bedrock-privatelink-vpc

How it works

Private Amazon Bedrock Access Without Internet via AWS PrivateLink

Normally, when code inside a private subnet calls bedrock-runtime.us-east-1.amazonaws.com, the request goes out through a NAT Gateway and across the public internet to reach the Bedrock endpoint.

When you create an Interface VPC Endpoint with Private DNS enabled, AWS adds a private network interface (ENI) in your subnet. DNS queries for bedrock-runtime.us-east-1.amazonaws.com inside that VPC resolve to that private IP (such as 10.0.1.44) instead of a public IP.

Your python code stays the same, traffic stays on the AWS private network, and you avoid NAT Gateway data processing charges for Bedrock calls.

Prerequisites

  • An AWS account with AWS CLI v2 configured (aws configure)
  • Region set to us-east-1

Cost note: Interface VPC Endpoints cost about \$0.01 per hour. Running this lab for 15–20 minutes costs less than a cent, just remember to delete the endpoint when you finish so it does not stay running all month.

Step 1: Create an isolated VPC and private subnet

Create a VPC (10.0.0.0/16) and one private subnet (10.0.1.0/24) without an Internet Gateway or NAT Gateway. Both dns-support and dns-hostnames must be enabled so Private DNS works on the VPC Endpoint.

REGION="us-east-1"

VPC_ID=$(aws ec2 create-vpc \
  --cidr-block 10.0.0.0/16 \
  --region $REGION \
  --query "Vpc.VpcId" --output text)

aws ec2 modify-vpc-attribute --vpc-id $VPC_ID --enable-dns-support --region $REGION
aws ec2 modify-vpc-attribute --vpc-id $VPC_ID --enable-dns-hostnames --region $REGION

SUBNET_ID=$(aws ec2 create-subnet \
  --vpc-id $VPC_ID \
  --cidr-block 10.0.1.0/24 \
  --region $REGION \
  --query "Subnet.SubnetId" --output text)

echo "VPC_ID=$VPC_ID"
echo "SUBNET_ID=$SUBNET_ID"

Step 2: Create a security group for HTTPS (port 443)

Create a security group that allows inbound TCP 443 from within 10.0.0.0/16:

SG_ID=$(aws ec2 create-security-group \
  --group-name private-bedrock-sg \
  --description "Allow HTTPS inside isolated VPC for Bedrock PrivateLink" \
  --vpc-id $VPC_ID \
  --region $REGION \
  --query "GroupId" --output text)

aws ec2 authorize-security-group-ingress \
  --group-id $SG_ID \
  --protocol tcp \
  --port 443 \
  --cidr 10.0.0.0/16 \
  --region $REGION

echo "SG_ID=$SG_ID"

Step 3: Create the Bedrock Interface VPC Endpoint

Create the PrivateLink endpoint for com.amazonaws.us-east-1.bedrock-runtime in your private subnet:

VPCE_ID=$(aws ec2 create-vpc-endpoint \
  --vpc-id $VPC_ID \
  --vpc-endpoint-type Interface \
  --service-name com.amazonaws.us-east-1.bedrock-runtime \
  --subnet-ids $SUBNET_ID \
  --security-group-ids $SG_ID \
  --private-dns-enabled \
  --region $REGION \
  --query "VpcEndpoint.VpcEndpointId" --output text)

echo "VPCE_ID=$VPCE_ID"

Wait about 60 seconds for the endpoint state to become available.

Step 4: Create the Lambda IAM role

A Lambda running in a VPC needs AWSLambdaVPCAccessExecutionRole plus permission to invoke amazon.nova-micro-v1:0.

Save trust-policy.json:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": { "Service": "lambda.amazonaws.com" },
      "Action": "sts:AssumeRole"
    }
  ]
}

Save bedrock-policy.json:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "bedrock:InvokeModel",
      "Resource": "arn:aws:bedrock:us-east-1::foundation-model/amazon.nova-micro-v1:0"
    }
  ]
}

Create the role and attach both policies:

aws iam create-role \
  --role-name PrivateBedrockLambdaRole \
  --assume-role-policy-document file://trust-policy.json

aws iam attach-role-policy \
  --role-name PrivateBedrockLambdaRole \
  --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole

aws iam put-role-policy \
  --role-name PrivateBedrockLambdaRole \
  --policy-name InvokeNovaMicro \
  --policy-document file://bedrock-policy.json

Step 5: Write and deploy the test Lambda

Save this as test_private_bedrock.py. It checks the resolved IP for Bedrock, tests outbound access to https://example.com, and calls Amazon Nova Micro.

import socket
import urllib.request
import boto3

bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")


def lambda_handler(event, context):
    results = {}

    # 1. Check what IP bedrock-runtime resolves to inside our VPC
    results["bedrock_resolved_ip"] = socket.gethostbyname(
        "bedrock-runtime.us-east-1.amazonaws.com"
    )

    # 2. Check outbound public internet access
    try:
        urllib.request.urlopen("https://example.com", timeout=3)
        results["public_internet_test"] = "REACHABLE (unexpected)"
    except Exception as exc:
        results["public_internet_test"] = f"BLOCKED ({type(exc).__name__})"

    # 3. Call Amazon Bedrock over PrivateLink
    response = bedrock.converse(
        modelId="amazon.nova-micro-v1:0",
        messages=[
            {
                "role": "user",
                "content": [
                    {"text": "Explain AWS PrivateLink in one short plain sentence."}
                ],
            }
        ],
        inferenceConfig={"maxTokens": 100, "temperature": 0.5},
    )

    results["bedrock_privatelink_test"] = "SUCCESS"
    results["ai_response"] = response["output"]["message"]["content"][0]["text"]

    return results

Zip and deploy the function inside your private subnet:

ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
ROLE_ARN="arn:aws:iam::${ACCOUNT_ID}:role/PrivateBedrockLambdaRole"

zip test_private_bedrock.zip test_private_bedrock.py

aws lambda create-function \
  --function-name private-bedrock-demo \
  --runtime python3.12 \
  --role "$ROLE_ARN" \
  --handler test_private_bedrock.lambda_handler \
  --zip-file fileb://test_private_bedrock.zip \
  --timeout 20 \
  --vpc-config "SubnetIds=${SUBNET_ID},SecurityGroupIds=${SG_ID}" \
  --region $REGION

aws lambda wait function-active-v2 --function-name private-bedrock-demo --region $REGION

Step 6: Run the test

Invoke the Lambda function:

aws lambda invoke \
  --function-name private-bedrock-demo \
  --region us-east-1 \
  response.json && cat response.json

Output:

{
  "bedrock_resolved_ip": "10.0.1.44",
  "public_internet_test": "BLOCKED (URLError)",
  "bedrock_privatelink_test": "SUCCESS",
  "ai_response": "AWS PrivateLink enables private communication between services within Amazon VPC without traversing the public internet."
}

test result

  • bedrock-runtime.us-east-1.amazonaws.com resolved to 10.0.1.44 inside the 10.0.1.0/24 private subnet.
  • https://example.com timed out (URLError) because there is no Internet Gateway or NAT Gateway.
  • Amazon Nova Micro responded normally without changing the SDK code.

Cleanup

Delete the resources once you finish testing so the VPC Endpoint does not keep incurring hourly charges:

aws lambda delete-function --function-name private-bedrock-demo --region $REGION
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids $VPCE_ID --region $REGION

aws iam delete-role-policy --role-name PrivateBedrockLambdaRole --policy-name InvokeNovaMicro
aws iam detach-role-policy --role-name PrivateBedrockLambdaRole --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
aws iam delete-role --role-name PrivateBedrockLambdaRole

# Wait ~60 seconds for Lambda's ENI to detach, then delete SG, Subnet, and VPC
aws ec2 delete-security-group --group-id $SG_ID --region $REGION
aws ec2 delete-subnet --subnet-id $SUBNET_ID --region $REGION
aws ec2 delete-vpc --vpc-id $VPC_ID --region $REGION

If you want to take this a step further, you can attach a VPC Endpoint Policy to restrict which Bedrock models are allowed from your VPC.

Are you already using PrivateLink for Bedrock or still routing through a NAT Gateway? Let me know in the comments.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.