I have a Lambda function in my VPC, and I want to access S3 bucket.
I have set S3 VPC endpoint correctly I think,
because I created an EC2 instance in the same subnet(Lambda function subnet),
use the same security group,
and run the copy of Lambda function code,
It can correctly show the S3 file content.
But when I run the code in Lambda, it failed.
So, I want to know what is the difference between "run in EC2" and "run in Lambda"?
Why it failed when I run it in Lambda?
Here is my Lambda function code:
import boto3
s3 = boto3.client('s3', region_name='ap-northeast-1')
def lambda_handler(event, context):
bucket = '*xxxxxx*'
key = 's3-upload.json'
try:
response = s3.get_object(Bucket=bucket, Key=key)
print('--------------------------------------')
print(response)
print('--------------------------------------')
body = response['Body'].read()
print(body)
print('--------------------------------------')
print("CONTENT TYPE: " + response['ContentType'])
except Exception as e:
print('Error getting object.')
print(e)
raise e