Can not access S3 via VPC endpoint in Lambda

筅森魡賤 提交于 2019-12-01 06:38:24

If you want to allow an AWS Lambda to access Amazon S3, use one of these methods:

  • Do not associate the function to a VPC. Access is then automatic.
  • If the function is attached to a public subnet in the VPC, associate an Elastic IP to the Lambda function's ENI that appears in the VPC
  • If the function is attached to a private subnet in the VPC, launch a NAT Gateway in the public subnet and update Route Tables. Traffic will flow to the Internet via the NAT Gateway.
  • Add an Amazon S3 VPC Endpoint in the VPC and update Route Tables. Traffic will flow through that instead of the Internet Gateway.

Even though they're in the same VPC, EC2 and Lambda are still different environments within AWS. Being able to run your code in one and not the other implies that your code is fine and works, so it's likely to be a configuration issue with AWS.

Have you checked the service/execution role that the lambda is using?

You need to ensure that the IAM role that it's using is allowed the correct level of S3 access.

This documentation on execution roles for lambda might provide a useful jumping off point: https://docs.aws.amazon.com/lambda/latest/dg/intro-permission-model.html#lambda-intro-execution-role

An IAM policy like this would give whatever execution role you use read-only access to all your S3 buckets, and happens to be one of the AWS managed policies.

{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "s3:Get*",
            "s3:List*"
        ],
        "Resource": "*"
    }
]

}

Thanks everyone! I found the reason.

My Lambda have two subnets, private_sn_1 and private_sn_2,

private_sn_1 have correctly set the vpc endpoint route table,

but the private_sn_2 set a wrong route table,

and my ec2 created in private_sn_1 so it can access the vpc endpoint.

In normal, Lambda will run randomly in private_sn_1 or private_sn_2,

but in my case it always run in private_sn_2(I don't know why),

so when I fixed the private_sn_2 route table,

everything is right.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!