启用VPC的Lambda功能无法在同一VPC中启动/访问EC2 [英] VPC-enabled Lambda function cannot launch/access EC2 in the same VPC

查看:128
本文介绍了启用VPC的Lambda功能无法在同一VPC中启动/访问EC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启用了VPC的Lambda函数,尝试使用启动模板启动EC2. EC2启动步骤(run_instances)失败,并出现以下通用网络错误.

I have a VPC enabled Lambda function which attempts to launch an EC2 using a launch template. The EC2 launch step (run_instances) fails with the below generic network error.

调用invoke API操作失败,并显示以下消息:网络错误

我可以直接从启动模板成功启动实例,因此我认为启动模板一切正常.我已经在启动模板中配置了以下内容

I can launch an instance successfully directly from the launch template, so I think everything is fine with the launch template. I have configured the following in the launch template

  • Amazon机器映像ID
  • 实例类型
  • 密钥对
  • 我在使用特定的(VPC,子网,安全组)组合之前创建的网络接口(ENI).
  • IAM角色

Lambda函数包含以下代码-

The Lambda function includes the below code-

import json
import boto3
import time

def lambda_handler(event, context):
    ec2_cl = boto3.client('ec2')
    launch_temp = {"LaunchTemplateId": "<<Launch Template ID>>"}
    resp_ec2_launch = ec2_cl.run_instances(MaxCount=1, MinCount=1, LaunchTemplate=launch_temp, SubnetId="<<Subnet ID>>")

关于Lambda函数的一些事情-

Few things on the Lambda function-

  • 我在run_instances()调用中使用了子网,因为这不是默认的vpc/子网.
  • 该功能的设置与启动模板中使用的组合(VPC,子网,安全组)组合
  • 将执行角色设置为与启动模板中使用的IAM角色相同
  • 您看到的功能仅需要访问EC2,不需要Internet访问
  • 我用describe_instance_status(使用直接从启动模板创建的实例ID)替换了run_instances()并得到了相同的错误.

该错误是网络错误,因此我认为,使用授予IAM角色的特权,一切都很好(到目前为止).我敢肯定,如果IAM角色错过任何政策,都会有一个不同的错误.

The error is a network error, so I assume all is fine (atleast as of now) with the privileges granted to the IAM role. I'm sure there would be a different error, if the IAM role missed any policies.

有人可以指出我可能会缺少什么吗?

Can someone indicate what I might be missing?

推荐答案

由于Amazon EC2 API端点处于开启状态,看来问题在于您的AWS Lambda函数能够访问Internet 互联网.

It appears that the problem is with your AWS Lambda function being able to reach the Internet, since the Amazon EC2 API endpoint is on the Internet.

如果Lambda函数未连接到VPC,则它可以自动访问Internet.

If a Lambda function is not attached to a VPC, it has automatic access to the Internet.

如果Lambda函数已附加到VPC ,并且需要Internet访问,则配置应为:

If a Lambda function is attached to a VPC and requires Internet access, then the configuration should be:

  • 仅将Lambda函数附加到私有子网
  • 在公共子网中启动 NAT网关
  • 在专用子网上配置路由表,以通过NAT网关发送Internet绑定流量(0.0.0.0/0)
  • Attach the Lambda function only to private subnet(s)
  • Launch a NAT Gateway in a public subnet
  • Configure the Route Table on the private subnets to send Internet-bound traffic (0.0.0.0/0) through the NAT Gateway

这篇关于启用VPC的Lambda功能无法在同一VPC中启动/访问EC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆