https://pulumi.com logo
#aws
Title
# aws
j

jolly-manchester-8306

02/25/2024, 12:22 AM
Hi All, I need to create a Lambda@Edge function to check if a file exists in an S3 bucket. I've inserted the bucket name into the Lambda, but upon creation, I got an error message in the code of function: bucket_name = 'Calling str on an Output[T] is not supported. I can't use bucket name as ENV variable because of limitation of lambda@edge My code:
Copy code
s3_bucket = aws.s3.Bucket("myS3Bucket")

lambda_text=f"""
import json
import boto3
def handler(event, context):
    s3 = boto3.client('s3')
    request = event['Records'][0]['cf']['request']
    uri = request['uri']
    key = uri[1:]
    bucket_name = '{s3_bucket.id}'
    try:
        s3.head_object(Bucket=bucket_name, Key=key)
    except s3.exceptions.ClientError as e:
        request['uri'] = '/index.html'
    return request
"""

lambda_function = aws.lambda_.Function("my_lambda",
    runtime="python3.12",
    handler="index.handler",
    code=pulumi.AssetArchive({"index.py": pulumi.StringAsset(lambda_text)}),
    role="arn:aws:iam::000000000:role/Lambda_role",
    publish=True
)
I would be grateful if someone could help me