Hi I have a lambda (python3.6) below that is unable to read a file from S3, even though the lambda is in a role that has unfettered permissions for S3 (IAM policy below).
The Lambda simply attempts to retrieve a file from S3 and write it to a temporary location. However it blocks on calling s3.Bucket()
and times out (even with a timeout in the minutes).
What's really weird is that it's timing out without any exception, and not rejecting the call to s3.Bucket()
with some kind of permission error.
This is pretty basic, but I'm at a loss to get this sorted out.
import boto3
from botocore import exceptions
def lambda_handler(event, context):
key = event['image']
bucket = event['bucket']
tempfile = '/tmp/%s-%s' % (bucket, key)
print('(p) bucket: %s::%s' % (bucket, key))
print('(p) tempfile: %s' % tempfile)
s3 = boto3.resource('s3')
print('(p) resource intiialized')
try:
b = s3.Bucket(bucket)
print('(p) bucket info: %s [%s]' % (b.name, b.creation_date))
b.download_file(prefixed_key, tempfile)
print('(p} file downloaded to %s' % tempfile)
except exceptions.ParamValidationError as e:
return {"statusCode": 400, "body": 'ParamValidationError: [%s]' % e}
except exceptions.ClientError as e:
message = '[%s]: [%s]' % (e.response['Error']['Code'], e.response['Error']['Message'])
return {"statusCode": e.response['ResponseMetadata']['HTTPStatusCode'], "body": message}
print('(p) image downloaded from s3 and stored at: %s' % tempfile)
return None
IAM Policy that the role has is:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::my_bucket",
"arn:aws:s3:::my_bucket/*"
]
}
]
}
Example logs:
22:42:43
START RequestId: 61c60167-839d-11e7-97b1-a772bbde2609 Version: $LATEST
START RequestId: 61c60167-839d-11e7-97b1-a772bbde2609 Version: $LATEST
22:42:43
(p) bucket: my_bucket::my_key
22:42:43
(p) tempfile: /tmp/my_bucket/my_key
22:42:43
(p) resource intiialized
22:43:13
END RequestId: 61c60167-839d-11e7-97b1-a772bbde2609
END RequestId: 61c60167-839d-11e7-97b1-a772bbde2609