Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


AWS Lambda URL proxy

28 Nov 2022 »

lambda layer

mkdir python
pip3 install -t ./python request
zip -r python.zip python

upload


import json
import requests
import base64
import os

def lambda_handler(event, context):
    # TODO implement
    path = event['rawPath']
    rooturl = "<other url>" + event['rawPath']
    rs = requests.get(rooturl)
    ct = {'Content-Type': 'text/html'}
    
    if path.endswith('.css'):
        ct = {'Content-Type': 'text/css'}
        
    if path.endswith('.png'):
        ct = {'Content-Type': 'image/png'}
          
    if path.endswith('.js'):
        ct = {'Content-Type': 'application/javascript'}  
        
    if path.endswith('.json'):
        ct = {'Content-Type': 'application/json'} 
    
    
    if ct['Content-Type'] == 'image/png':
        return {
            'headers': ct,
            'body': base64.b64encode(rs.content).decode('utf-8'),
            'statusCode': 200,
            'isBase64Encoded': True,
        }
    else:
        return {
            'headers': ct,
            'statusCode': 200,
            'body': rs.text,
        }

layout: post title: “AWS Lambda URL proxy” comments: true date: “2022-11-28 00:36:00.159000+00:00” —

/allaa/c2/me.json

{
    "version": "2.0",
    "routeKey": "$default",
    "rawPath": "/allaa/c2/me.json",
    "rawQueryString": "",
    "headers": {
        "sec-fetch-mode": "navigate",
        "x-amzn-tls-version": "TLSv1.2",
        "sec-fetch-site": "none",
{
    "version": "2.0",
    "routeKey": "$default",
    "rawPath": "/",
    "rawQueryString": "",
    "headers": {
        "sec-fetch-mode": "navigate",
        "x-amzn-tls-version": "TLSv1.2",
        "sec-fetch-site": "cross-site",
        "accept-language": "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
        "x-forwarded-proto": "https",
        "x-forwarded-port": "443",
        "x-forwarded-for": "186.204.000.000",
        "sec-fetch-user": "?1",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
        "x-amzn-tls-cipher-suite": "ECDHE-RSA-AES128-GCM-SHA256",
        "x-amzn-trace-id": "Root=1--67dfed8f0cb1a3cb4a574fdd",
        "host": ".lambda-url.sa-east-1.on.aws",
        "upgrade-insecure-requests": "1",
        "accept-encoding": "gzip, deflate, br",
        "sec-fetch-dest": "document",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0"
    },
    "requestContext": {
        "accountId": "anonymous",
        "apiId": "...",
        "domainName": "...lambda-url.sa-east-1.on.aws",
        "domainPrefix": "...",
        "http": {
            "method": "GET",
            "path": "/",
            "protocol": "HTTP/1.1",
            "sourceIp": "186.204.195.251",
            "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0"
        },
        "requestId": "2c972820-22c0-4a67-9243-5dc14aa8ea7b",
        "routeKey": "$default",
        "stage": "$default",
        "time": "28/Nov/2022:00:35:12 +0000",
        "timeEpoch": 1669595712189
    },
    "isBase64Encoded": false
}

query string parameter

post