Localstack lambda cannot conect to http. Code in spyder (python 3.8) works great but lambda doesnt
Hello everybody. I have default localstack configuration, with this environment variables (amazon cli in powershell)
$env:AWS_ACCESS_KEY_ID="dummy"
$env:AWS_SECRET_ACCESS_KEY="dummy"
$env:AWS_DEFAULT_REGION="us-east-1"
$env:AWS_ENDPOINT_URL="http://localhost:4566"
the purpose of the lambda is create a new directory on a existing bucket (bucket1):
`import boto3
def lambda_handler(event, context): # Nombre del bucket de S3 bucket_name = 'bucket1'
# Nombre del directorio que deseas crear en el bucket
directory_name = 'prueba'
# Crea el directorio en el bucket
s3_client = boto3.client('s3', endpoint_url='http://localhost:4566')
try:
s3_client.put_object(Bucket=bucket_name, Key=directory_name)
print("El archivo '{file}' se ha subido al bucket '{bucket_name}'")
variable=1
except Exception as e:
return {
'statusCode': 0, # Código de estado de error
'body': str(e) # Mensaje de error como cadena
}
return {
'statusCode': variable
}`
Spyder interface (python): no error AND DIRECTORY CREATED IN BUCKET (calling the function)
Lambda localstack: code 200 (success) but returns the exception:
{"statusCode": 0, "body": "Could not connect to the endpoint URL: "http://localhost:4566/bucket1/prueba""}
Bucket exists too. I made a rol policy to allow all actions and aplied it to lambda: { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "lambda:", "Resource": "" }, { "Effect": "Allow", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::bucket1/" } { "Effect": "Allow", "Action": "s3:PutObject", "Resource": "arn:aws:s3:::bucket1/" } ] }
aws --endpoint-url=http://localhost:4566 lambda create-function --function-name Test --runtime python3.8 --handler test.lambda_handler --role arn:aws:iam::000000000000:role/RolLambda --zip-file fileb://./test.zip --timeout 1000
Thanks for reading.