You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to setup a mock test for my Lambda. The Lambda is triggered using S3 PutObject event. The Lambda downloads the file and processes it and uploads it to AWS CloudSearch. So I am mocking S3 and SNS but not CloudSearch (not supported but its acceptable to me if it accesses AWS API in this case).
The upload_files.lambda_handler code which is a separate file looks like this:
def lambda_handler(event, context):
s3 = boto3.resource('s3')
user_identity = event['Records'][0]['userIdentity']['principalId']
email = user_identity[user_identity.rindex(":")+1:user_identity.rindex('-')] + '@test.com'
logger.debug("Email address:{}".format(email))
bucket = event['Records'][0]['s3']['bucket']['name']
key = unquote_plus(event['Records'][0]['s3']['object']['key'])
logger.debug("bucket name:{}".format(bucket))
logger.debug("filename:{}".format(key))
temp_file = key[key.rindex('/')+1:]
logger.debug("Filename without the prefix:{}".format(temp_file))
local_file_name = f'/tmp/{temp_file}'
s3.Bucket(bucket).download_file(key, local_file_name)
# performs some processing to the downloaded file and attempts to upload to CloudSearch
client = boto3.client('cloudsearch')
domain_name= os.environ['CS_DOMAIN_NAME']
logger.debug("CS domain name:{}".format(domain_name))
info = client.describe_domains(DomainNames=[domain_name])
endpoint_url = info['DomainStatusList'][0]['DocService']['Endpoint']
logger.debug("Endpoint document url:{}".format(endpoint_url))
client = boto3.client('cloudsearchdomain', endpoint_url=f'https://{endpoint_url}')
It throws this error:
Traceback (most recent call last):
File "src/upload_file/upload_files.py", line 148, in lambda_handler
info = client.describe_domains(DomainNames=[domain_name])
File "Library/Python/3.9/lib/python/site-packages/botocore/client.py", line 388, in _api_call
return self._make_api_call(operation_name, kwargs)
File "Library/Python/3.9/lib/python/site-packages/botocore/client.py", line 708, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidClientTokenId) when calling the DescribeDomains operation: The security token included in the request is invalid.
I tried calling sts.get_caller_identity() and it gives me the same error. If I disable the s3 mocking, it works fine and can read the AWS credential environment variables properly.
I cannot disable the S3 mock because I need the Lambda to be able to read the file and upload it to CloudSearch. I am not sure what can be the workaround for this.
The text was updated successfully, but these errors were encountered:
Hi @askkhan84 , this behavior is on purpose, actually. We intercept all requests to AWS to prevent people from accidentally changing their real infrastructure.
There is no way to only intercept only some services, but let others pass through. There is an outstanding issue to tackle this already, #4597, but no real progress on this.
I have read This issue and this one but dont work for me.
I am trying to setup a mock test for my Lambda. The Lambda is triggered using S3 PutObject event. The Lambda downloads the file and processes it and uploads it to AWS CloudSearch. So I am mocking S3 and SNS but not CloudSearch (not supported but its acceptable to me if it accesses AWS API in this case).
My test file looks like this:
The upload_files.lambda_handler code which is a separate file looks like this:
It throws this error:
I tried calling sts.get_caller_identity() and it gives me the same error. If I disable the s3 mocking, it works fine and can read the AWS credential environment variables properly.
I cannot disable the S3 mock because I need the Lambda to be able to read the file and upload it to CloudSearch. I am not sure what can be the workaround for this.
The text was updated successfully, but these errors were encountered: