コンテキストの限界を突破する
コンテキストの限界を突破する —— 「読む」AIから、「文脈を創る」AIへの進化 ...
Z. Xingjie
2026年02月27日
Implement Basic Auth using lambda function for S3 Website Hosting
To implement basic authentication in AWS CloudFront using a Lambda@Edge function written in Python, follow these steps:
Here’s a step-by-step guide to achieve this:
import base64
def lambda_handler(event, context):
# Get the request object
request = event['Records'][0]['cf']['request']
# Get the headers
headers = request['headers']
# Define the username and password
USERNAME = 'your_username'
PASSWORD = 'your_password'
# Encode the username and password
auth_string = f"{USERNAME}:{PASSWORD}"
encoded_auth_string = base64.b64encode(auth_string.encode()).decode()
# Check for Authorization header
if 'authorization' in headers:
auth_header = headers['authorization'][0]['value']
# Check if the Authorization header matches the encoded auth string
if auth_header == f"Basic {encoded_auth_string}":
return request
# If no valid Authorization header is present, return a 401 Unauthorized response
return {
'status': '401',
'statusDescription': 'Unauthorized',
'headers': {
'www-authenticate': [{'key': 'WWW-Authenticate', 'value': 'Basic'}],
'content-type': [{'key': 'Content-Type', 'value': 'text/html'}]
},
'body': '<html><body><h1>Unauthorized</h1></body></html>'
}
This setup will check for basic authentication on each viewer request. If the credentials are correct, the request will proceed to CloudFront. If not, it will return a 401 Unauthorized response.
======= If you get the following type Error , then follow next steps ========
To update the IAM role to include the necessary permissions for edgelambda.amazonaws.com and lambda.amazonaws.com principals, you need to adjust the trust relationship of the IAM role. Here are the steps to do this:
You need to ensure the trust relationship includes both edgelambda.amazonaws.com and lambda.amazonaws.com. Here is the updated trust relationship policy document:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Effect": "Allow",
"Principal": {
"Service": "edgelambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
{
Replace the existing trust relationship policy document with the above JSON and click “Update Trust Policy”.
Make sure the IAM role has the necessary permissions to execute the Lambda function. Attach the AWSLambdaBasicExecutionRole policy if it’s not already attached.
Once the trust relationship is updated and the necessary permissions are attached, you can retry deploying the Lambda@Edge function to your CloudFront distribution.
コンテキストの限界を突破する —— 「読む」AIから、「文脈を創る」AIへの進化 ...
Z. Xingjie
2026年02月27日
本記事は、英語で公開されている弊社ブログ記事の日本語翻訳版です。
英語元記事:Implement Basic AUth using lambda function for S3 Website Hosting
Muntasir Ahmed MUFTI
2026年02月06日
AIによる認知能力低下? —— MITの研究結果とこれからのエンジニアリング ...
Z. Xingjie
2026年02月02日