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.
it seems you are addressing the LocalStack main container in the wrong way. If you look at the documentation here, you are in the scenario of accessing LocalStack from a LocalStack-created container. In this case, you need to make sure you define your own Docker network, like in the docs, and build the endpoint using the name of the LocalStack container (usually http://localstack:4566
or the default http://localstack_main:4566
).