Showing posts with label aws. Show all posts
Showing posts with label aws. Show all posts

5/03/2026

How to Access Korean-Only Websites from Overseas Using AWS EC2 (Seoul Region VPN)

How to Access Korean-Only Websites from Overseas Using AWS EC2 (Seoul Region VPN)

Some Korean websites (government, financial, public institutions) block access from foreign IP addresses. This guide shows you how to create a quick VPN tunnel through AWS EC2 in Seoul to get a Korean IP address.

What you need: AWS account, AWS CLI installed, Terminal (macOS/Linux)


Step 1: Verify AWS CLI

Make sure AWS CLI is installed and configured:

aws --version
aws sts get-caller-identity --region ap-northeast-2

If you see your Account ID, you're good to go.


Step 2: Create a Key Pair

aws ec2 create-key-pair \
  --key-name kr-proxy-key \
  --region ap-northeast-2 \
  --query 'KeyMaterial' \
  --output text > ~/Desktop/kr-proxy-key.pem

chmod 400 ~/Desktop/kr-proxy-key.pem

Step 3: Create a Security Group

# Create security group
aws ec2 create-security-group \
  --group-name kr-proxy-sg \
  --description "SSH proxy for Korean IP access" \
  --region ap-northeast-2

# Allow SSH inbound (replace sg-xxxxx with your Group ID from above)
aws ec2 authorize-security-group-ingress \
  --group-id sg-xxxxx \
  --protocol tcp \
  --port 22 \
  --cidr 0.0.0.0/0 \
  --region ap-northeast-2

Step 4: Find the Latest AMI

aws ec2 describe-images \
  --owners amazon \
  --filters "Name=name,Values=al2023-ami-2023*-x86_64" "Name=state,Values=available" \
  --query 'Images | sort_by(@, &CreationDate) | [-1].ImageId' \
  --output text \
  --region ap-northeast-2

Note the AMI ID (e.g. ami-09a64de684ce1ac0e).


Step 5: Launch EC2 Instance

aws ec2 run-instances \
  --image-id ami-09a64de684ce1ac0e \
  --instance-type t2.micro \
  --key-name kr-proxy-key \
  --security-group-ids sg-xxxxx \
  --associate-public-ip-address \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=kr-proxy}]' \
  --region ap-northeast-2 \
  --query 'Instances[0].InstanceId' \
  --output text

Note the Instance ID (e.g. i-0df42e446381ecc9a).

t2.micro is free-tier eligible (750 hours/month for 12 months).


Step 6: Get Public IP

Wait about 30 seconds, then:

aws ec2 describe-instances \
  --instance-ids i-xxxxx \
  --region ap-northeast-2 \
  --query 'Reservations[0].Instances[0].[State.Name, PublicIpAddress]' \
  --output text

You should see something like: running 13.125.x.x


Step 7: Open SSH SOCKS5 Tunnel

ssh -D 1080 -N -f \
  -o StrictHostKeyChecking=no \
  -i ~/Desktop/kr-proxy-key.pem \
  ec2-user@13.125.x.x
  • -D 1080 — Creates a SOCKS5 proxy on local port 1080
  • -N — No remote command (tunnel only)
  • -f — Run in background

Step 8: Verify Korean IP

curl --socks5-hostname localhost:1080 https://ifconfig.me

If it returns your EC2's Korean IP (e.g. 13.125.x.x), it's working!


Step 9: Configure Browser Proxy

Option A: macOS System Settings

  1. Open System SettingsNetworkWi-Fi
  2. Click Details...Proxies
  3. Enable SOCKS Proxy
  4. Server: localhost / Port: 1080
  5. Click OK

Option B: Terminal (macOS)

sudo networksetup -setsocksfirewallproxy "Wi-Fi" localhost 1080
sudo networksetup -setsocksfirewallproxystate "Wi-Fi" on

Option C: curl only

curl --socks5-hostname localhost:1080 https://www.target-website.kr

Now open your browser and access the Korean website!


Clean Up (Important!)

When you're done, clean up everything to avoid charges:

1. Close the SSH Tunnel

pkill -f "ssh -D 1080"

2. Turn Off Browser Proxy

macOS GUI: System Settings → Network → Wi-Fi → Details → Proxies → SOCKS Proxy OFF

Terminal:

sudo networksetup -setsocksfirewallproxystate "Wi-Fi" off

# Verify
networksetup -getsocksfirewallproxy "Wi-Fi"
# Should show: Enabled: No

3. Terminate EC2 Instance

aws ec2 terminate-instances \
  --instance-ids i-xxxxx \
  --region ap-northeast-2

4. Delete Security Group

Wait about 30 seconds after termination, then:

aws ec2 delete-security-group \
  --group-id sg-xxxxx \
  --region ap-northeast-2

5. Delete Key Pair

aws ec2 delete-key-pair \
  --key-name kr-proxy-key \
  --region ap-northeast-2

rm ~/Desktop/kr-proxy-key.pem

Cost Summary

Item Cost
t2.micro (free tier) Free (750 hrs/month, 12 months)
t2.micro (after free tier) ~$0.0116/hr (~$8.5/month)
Data transfer Free up to 100GB/month

Tip: If you want to keep the instance for later, Stop it instead of terminating. You only pay for EBS storage (~$0.10/GB/month) while stopped.


Quick Reference: Reconnect Later

If you stopped (not terminated) the instance:

# Start the instance
aws ec2 start-instances --instance-ids i-xxxxx --region ap-northeast-2

# Wait ~30 seconds, get new public IP
aws ec2 describe-instances \
  --instance-ids i-xxxxx \
  --region ap-northeast-2 \
  --query 'Reservations[0].Instances[0].PublicIpAddress' \
  --output text

# Open tunnel
ssh -D 1080 -N -f -i ~/Desktop/kr-proxy-key.pem ec2-user@NEW-IP

# Set proxy
sudo networksetup -setsocksfirewallproxy "Wi-Fi" localhost 1080
sudo networksetup -setsocksfirewallproxystate "Wi-Fi" on

12/31/2025

AWS SAM Troubleshooting - Fixing pip/runtime and AWS CLI Issues


πŸ”§ AWS SAM Troubleshooting - Fixing pip/runtime and AWS CLI Issues

If you're deploying AWS Lambda functions with SAM (Serverless Application Model), you may have encountered frustrating build errors. This guide explains the two most common issues and how to fix them permanently.

Note: This guide uses generic examples (<YOUR_STACK_NAME>) and is safe to share publicly.

🚨 The Two Common Problems

Problem A — sam build fails with pip/runtime error

You may see this error:

Error: PythonPipBuilder:ResolveDependencies - Failed to find a Python runtime containing pip on the PATH.

What this means: SAM is trying to build for a specific Lambda runtime (like python3.11), but your shell has:

  • python from one location (e.g., conda environment)
  • pip from another location (e.g., ~/.local/bin or /usr/bin)

SAM requires a matching pair - the pip must belong to the same Python interpreter that matches your Lambda runtime version.

Problem B — AWS CLI crashes with botocore conflicts

You may see errors like:

KeyError: 'opsworkscm'
ModuleNotFoundError: No module named 'dateutil'

What this means: Your system AWS CLI (/usr/bin/aws) is accidentally importing incompatible botocore or boto3 packages from ~/.local/lib/python..., causing version conflicts.

πŸ” Quick Diagnosis (5 Commands)

Run these from your SAM project directory to diagnose the issue:

# 1. What Python runtime does your template.yaml require?
grep "Runtime: python" template.yaml

# 2. What python are you using?
which python
python -V

# 3. What pip are you using?
which pip
pip -V

# 4. Does pip belong to this python?
python -m pip -V

🚩 Red flag: If pip -V and python -m pip -V show different paths or Python versions, your PATH is contaminated.

✅ The Fix: Dedicated Environment + Clean PATH

The solution is to create an isolated environment that matches your Lambda runtime and force clean PATH ordering.

Step 1: Create an environment matching your Lambda runtime

If your template.yaml specifies Runtime: python3.11, create a Python 3.11 environment:

# Using conda (recommended)
conda create -n aws-sam-py311 python=3.11 pip -y
conda activate aws-sam-py311

# Or using venv
python3.11 -m venv ~/.virtualenvs/aws-sam-py311
source ~/.virtualenvs/aws-sam-py311/bin/activate

Step 2: Install SAM CLI and AWS CLI inside the environment

# Upgrade pip first
python -m pip install --upgrade pip

# Install SAM CLI
python -m pip install aws-sam-cli

# Optional: Install AWS CLI v2 (avoids system aws/botocore conflicts)
# Using conda-forge:
conda install -c conda-forge awscli -y

# Or using pip:
python -m pip install awscli

Step 3: Disable user-site imports and fix PATH ordering

This is the critical step that prevents ~/.local contamination:

# Disable user site-packages (~/.local)
export PYTHONNOUSERSITE=1

# Force clean PATH (conda/venv bin first, then system)
export PATH="$CONDA_PREFIX/bin:/usr/bin:/bin"

# Or for venv:
# export PATH="$VIRTUAL_ENV/bin:/usr/bin:/bin"

# Clear shell hash table
hash -r

Step 4: Verify the fix

# All should point to your environment
which python
which pip
which sam
which aws

# Verify versions match
python -V      # Should be 3.11.x
pip -V         # Should show python 3.11
sam --version  # Should work without errors
aws --version  # Should work without errors

Step 5: Build and deploy

sam build --cached --parallel
sam deploy --no-confirm-changeset --stack-name <YOUR_STACK_NAME> --region <YOUR_AWS_REGION>

🐳 Alternative: Container Build (Docker)

If you have Docker installed, you can avoid all Python toolchain issues by building in a container:

sam build --use-container
sam deploy --no-confirm-changeset --stack-name <YOUR_STACK_NAME> --region <YOUR_AWS_REGION>

Pros:

  • ✅ No need to match local Python version
  • ✅ Builds in environment identical to Lambda
  • ✅ Most reproducible approach

Cons:

  • ❌ Slower than native builds
  • ❌ Requires Docker installed and running

⚡ Quick Fix for Broken AWS CLI (Emergency)

If you need to use system AWS CLI right now and it's broken:

# Force it to ignore user-site packages
PYTHONNOUSERSITE=1 /usr/bin/aws --version
PYTHONNOUSERSITE=1 /usr/bin/aws sts get-caller-identity
PYTHONNOUSERSITE=1 /usr/bin/aws s3 ls

But the proper fix is: Install AWS CLI inside your dedicated environment (see Step 2 above).

πŸ€” Why Lambda is python3.11 but my machine uses python3.12?

This is a common source of confusion. They are different things:

Component What It Is Where It's Defined
Lambda Runtime Python version AWS runs in production template.yamlRuntime: python3.11
Your Local Python Python version for development/training/scripts Your system default or conda environment

Key point: When SAM builds your Lambda functions, it must build dependencies compatible with the Lambda runtime, even if your system default is Python 3.12.

Example from template.yaml:

AnprDeviceLicenseValidateFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: anpr_device_license_validate/
    Handler: app.lambda_handler
    Runtime: python3.11          # ← Lambda uses 3.11
    Architectures:
      - x86_64

So you have three options:

  1. Match local environment to Lambda (recommended) - Create python3.11 env for SAM work
  2. Use container build - Let Docker handle it with sam build --use-container
  3. Upgrade Lambda runtime - Change template.yaml to python3.12 (requires testing)

πŸ“‹ Complete Example: Deploy Script

Here's a complete bash script that implements all the fixes:

#!/usr/bin/env bash
set -euo pipefail

# Activate conda environment (matches Lambda runtime)
source ~/anaconda3/etc/profile.d/conda.sh
conda activate aws-sam-py311

# Critical: Clean PATH and disable user-site
export PYTHONNOUSERSITE=1
export PATH="$CONDA_PREFIX/bin:/usr/bin:/bin"
hash -r

echo "Environment ready:"
echo "  Python: $(python -V)"
echo "  SAM: $(sam --version | head -1)"
echo "  AWS: $(aws --version)"

# Build and deploy
sam build --cached --parallel
sam deploy --no-confirm-changeset

🎯 Troubleshooting Checklist

Issue Check Fix
sam build fails which pip vs python -m pip -V Create dedicated env, fix PATH
aws command crashes echo $PYTHONNOUSERSITE Set PYTHONNOUSERSITE=1
Wrong Python version python -V vs Lambda runtime Create env matching Lambda
Multiple pip versions which -a pip Fix PATH ordering
Conda conflicts conda env list Create separate env for SAM

πŸ”’ Security Best Practices

⚠️ When sharing code publicly:

  • Never publish template.yaml with secrets (API keys, tokens, webhook URLs)
  • ✅ Use AWS Secrets Manager or SSM Parameter Store for secrets
  • ✅ Redact from logs:
    • AWS account IDs
    • API Gateway URLs
    • Stack names and ARNs
    • Any access keys/tokens

πŸ’‘ Pro Tips

1. Create a deployment script

Instead of remembering all these environment variables, create a deploy.sh script:

#!/usr/bin/env bash
set -euo pipefail

# Activate environment
source ~/anaconda3/etc/profile.d/conda.sh
conda activate aws-sam-py311

# Clean environment
export PYTHONNOUSERSITE=1
export PATH="$CONDA_PREFIX/bin:/usr/bin:/bin"
hash -r

# Build and deploy
sam build --cached --parallel
sam deploy --no-confirm-changeset

echo "✅ Deployment complete!"

Make it executable: chmod +x deploy.sh

2. Use SAM build cache for faster builds

# First build (slow)
sam build

# Subsequent builds (much faster!)
sam build --cached --parallel

3. Test locally before deploying

# Invoke function locally
sam local invoke MyFunction --event events/test.json

# Start local API
sam local start-api

4. Skip changeset confirmation in CI/CD

# Manual deployment - shows changes
sam deploy

# CI/CD deployment - no prompts
sam deploy --no-confirm-changeset

πŸ“Š Before vs After

❌ Before (Broken)
$ sam build
Error: Failed to find Python runtime containing pip

$ aws --version
KeyError: 'opsworkscm'

$ which pip
/home/user/.local/bin/pip  # Wrong location!

$ pip -V
pip 24.0 (python 3.12)     # Wrong version!
✅ After (Fixed)
$ conda activate aws-sam-py311
$ export PYTHONNOUSERSITE=1
$ export PATH="$CONDA_PREFIX/bin:/usr/bin:/bin"

$ sam build
Build Succeeded ✨

$ aws --version
aws-cli/2.32.26 Python/3.11.14

$ which pip
/home/user/anaconda3/envs/aws-sam-py311/bin/pip  # Correct!

$ pip -V
pip 25.3 (python 3.11)     # Matches Lambda runtime!

πŸŽ“ Summary

The root cause of most SAM build failures is PATH contamination - your shell mixes Python versions and pip locations from different sources (~/.local, /usr/bin, conda environments).

The complete fix:

  1. ✅ Create dedicated environment matching Lambda runtime (python3.11)
  2. ✅ Install SAM CLI and AWS CLI inside that environment
  3. ✅ Set PYTHONNOUSERSITE=1 to disable user-site packages
  4. ✅ Fix PATH ordering: export PATH="$CONDA_PREFIX/bin:/usr/bin:/bin"
  5. ✅ Run hash -r to clear shell cache

After this, sam build and aws commands will work reliably! πŸš€

πŸ”— Additional Resources


Tags: AWS, SAM, Lambda, Python, DevOps, Deployment, Troubleshooting, ServerlessFramework, CICD, CloudComputing

9/11/2023

comparing t4g.medium, t3a.medium, and t3.medium

 



the t4g.medium, t3a.medium, and t3.medium are all part of Amazon's EC2 T-series instances, which are designed to provide a baseline level of CPU performance with the ability to burst above the baseline when needed. However, they differ in the underlying processor architecture and some other characteristics. Below is a comparative table:

Instance TypeCPU TypevCPUsMemory (GiB)ProcessorNetwork BandwidthEBS Bandwidth
t4g.mediumARM-based24Graviton2Up to 5 GbpsUp to 3.5 Gbps
t3a.mediumAMD-based24AMD EPYC 7000 seriesUp to 5 GbpsUp to 3.5 Gbps
t3.mediumIntel-based24Intel Xeon Scalable (Skylake and Broadwell options)Up to 5 GbpsUp to 3.5 Gbps

Key Differences:

  1. Processor Architecture:

    • t4g.medium uses ARM-based Graviton2 processors.
    • t3a.medium uses AMD EPYC 7000 series processors.
    • t3.medium uses Intel Xeon Scalable processors.
  2. Price:

    • t3a.medium instances are generally cheaper than t3.medium instances but offer similar performance characteristics.
    • t4g.medium instances are also generally cost-effective due to the efficiency of the Graviton2 processor.
  3. Performance:

    • The ARM-based Graviton2 processors in t4g.medium instances are designed for better power efficiency.
    • Both AMD and Intel options in t3a and t3 are more traditional and have been in use for longer periods, and their performance characteristics are well understood.
  4. Compatibility:

    • Software that is dependent on specific instruction sets might not be compatible with ARM-based processors, so t3 and t3a could be a safer bet for those applications.

For the most current and accurate information, it's always best to consult the official AWS EC2 documentation or pricing pages.

3/30/2023

The list_objects_v2 function returns up to 1000 objects by default. To read all the contents in the bucket, you can use pagination.

 refer to code:

You can modify '.json' for you case.

.

import boto3

def get_origin_fn_list(ORIGIN_DATA_S3, ORIGIN_DATA_S3_prefix):
s3 = boto3.client('s3')
paginator = s3.get_paginator('list_objects_v2')
origin_path = {}

for response in paginator.paginate(Bucket=ORIGIN_DATA_S3, Prefix=ORIGIN_DATA_S3_prefix):
for obj in response['Contents']:
if obj['Key'][-4:] == '.json':
path = obj['Key']
uid = path.split('/')[-2]
origin_path[uid] = path

print(f"get kv.json list: {len(origin_path)}/{sum(1 for _ in paginator.paginate(Bucket=ORIGIN_DATA_S3, Prefix=ORIGIN_DATA_S3_prefix))}")
return origin_path

..


Thank you.

πŸ™‡πŸ»‍♂️

www.marearts.com


3/16/2023

sync local dir with s3 bucket , code for Jupyter

 refer to code:


.

local_directory = "/path/to/your/local/directory"
s3_bucket = "your-s3-bucket-name"
s3_folder = "your-s3-folder-name"

# Sync S3 bucket folder to local directory
!aws s3 sync s3://$s3_bucket/$s3_folder $local_directory

# Sync local directory to S3 bucket folder
!aws s3 sync $local_directory s3://$s3_bucket/$s3_folder

..

Replace /path/to/your/local/directory, your-s3-bucket-name, and your-s3-folder-name with your specific values. The first aws s3 sync command downloads the S3 folder's contents to the local directory, and the second one uploads the local directory's contents to the S3 folder. You can use either of these commands as needed.

Note that the aws s3 sync command does not support excluding or including specific files like rsync, but it will only copy new and updated files by default.


Thank you.

πŸ™‡πŸ»‍♂️

www.marearts.com



3/15/2023

To save a JSON object (stored in a Python variable) to an Amazon S3 bucket

 

refer to code:

.

import boto3
import json

# Initialize the S3 client
s3 = boto3.client('s3')

# Specify the S3 bucket and JSON object key
bucket_name = 'your-bucket-name'
object_key = 'path/to/your/object.json'

# Your JSON data
json_data = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}

# Convert the JSON data to a string
json_content = json.dumps(json_data)

# Save the JSON content to the S3 bucket
s3.put_object(Bucket=bucket_name, Key=object_key, Body=json_content)

print(f"Saved JSON data to '{bucket_name}/{object_key}'")

..

This code will convert the JSON data to a string, and then save it to the specified S3 bucket and key.


Thank you.

πŸ™‡πŸ»‍♂️

www.marearts.com



copy s3 object to another bucket

refer to code: 


.

import boto3

# Initialize the S3 client
s3 = boto3.client('s3')

# Specify the source and destination S3 buckets and object keys
source_bucket = 'source-bucket-name'
source_key = 'path/to/source/object'

destination_bucket = 'destination-bucket-name'
destination_key = 'path/to/destination/object'

# Copy the object from the source bucket to the destination bucket
s3.copy_object(
CopySource={'Bucket': source_bucket, 'Key': source_key},
Bucket=destination_bucket,
Key=destination_key
)

print(f"Copied object from '{source_bucket}/{source_key}' to '{destination_bucket}/{destination_key}'")

..

Replace the placeholder values for source_bucket, source_key, destination_bucket, and destination_key with your actual bucket names and object keys. This code will copy the specified object from the source bucket to the destination bucket.


Thank you

πŸ™‡πŸ»‍♂️

www.marearts.com

3/07/2023

t3 vs c5 ec2 instance comparison spec & price table

Note: The prices mentioned are for the US East (N. Virginia) region and are subject to change. Also, keep in mind that the optimal EC2 instance type for inference may vary depending on the specific use case and workload.


EC2 Instance TypevCPUsMemory (GiB)Network Bandwidth (Gbps)Hourly Price ($)Monthly Price ($)
c5.large24Up to 100.08562.72
c5.xlarge48Up to 100.17125.44
c5.2xlarge816Up to 100.34250.88
c5.4xlarge1632Up to 100.68501.76
c5.9xlarge3672101.531,127.92
c5.18xlarge72144253.062,255.84
t3.large28Up to 50.083261.28
t3.xlarge416Up to 50.1664122.56
t3.2xlarge832Up to 50.3328245.12
t3a.large28Up to 50.07555.20
t3a.xlarge416Up to 50.15110.40
t3a.2xlarge832Up to 50.3220.80