简体   繁体   English

AWS 安全组入站规则。 允许 lambda 函数

[英]AWS security group inbound rule. allow lambda function

I run a service on my EC2 instance and I want to setup an inbound rule that only allows my lambda function to access it.我在我的 EC2 实例上运行一个服务,我想设置一个只允许我的 lambda 函数访问它的入站规则。 The security group allows me to restrict access by a specific IP, but I don't think that lambda functions have a specific IP assigned.安全组允许我限制特定 IP 的访问,但我认为 lambda 函数没有分配特定的 IP。 Is there a way to do what I want?有没有办法做我想做的事?

If you are enabling VPC access for your Lambda function, as per this blog post , then you will create a security group for your Lambda functions to use within your VPC.如果您按照这篇博文为您的 Lambda 函数启用 VPC 访问,那么您将为 Lambda 函数创建一个安全组以在您的 VPC 中使用。 All you need to do at that point is go into the security group your EC2 instance is using and grant access to the security group the Lambda function is using.此时您需要做的就是进入您的 EC2 实例正在使用的安全组,并授予对 Lambda 函数正在使用的安全组的访问权限。 This is the method I recommend.这是我推荐的方法。

If you are not using VPC access then your EC2 instance would need to be publicly accessible and you would basically be going over the internet to access the EC2 instance from the Lambda function.如果您不使用 VPC 访问,那么您的 EC2 实例将需要可公开访问,并且您基本上将通过互联网从 Lambda 函数访问 EC2 实例。 If that's the case then there is no good way to restrict that in the security group.如果是这种情况,则没有好的方法可以在安全组中对其进行限制。 You could (with difficulty) open it up to only requests thatoriginate within AWS , but that still leaves it open to all other users of AWS.您可以(有困难地)仅对源自 AWS 的请求开放它,但这仍然对 AWS 的所有其他用户开放。 If you must go over the internet to access your EC2 instance from Lambda then it would be best to send some sort of security token with each request Lambda sends, and ignore any requests on the EC2 server that don't contain that security token.如果您必须通过互联网从 Lambda 访问您的 EC2 实例,那么最好在 Lambda 发送的每个请求中发送某种安全令牌,并忽略 EC2 服务器上不包含该安全令牌的任何请求。

A Lambda with no VPC association will be on the Internet and assigned with an arbitrary Public IP (obviously from Amazon Registered IPs) which will not be a fix address.没有 VPC 关联的 Lambda 将在 Internet 上并分配有任意公共 IP(显然来自 Amazon 注册的 IP),该 IP 不是固定地址。 So it is almost impossible to whitelist that IP in your EC2's SG since next time that Lambda spins up the IP could change and is not predictable.因此,几乎不可能在 EC2 的 SG 中将该 IP 列入白名单,因为下次 Lambda 启动 IP 时,该 IP 可能会发生变化并且无法预测。

However there is a way to Partially limit the Network Access only to the Resources Located in the VPC including Lambda's(since components will be assigned with Internal-IPs).但是,有一种方法可以将网络访问部分限制到位于 VPC 中的资源,包括 Lambda(因为组件将分配有内部 IP)。 Let's say you want that EC2 server only be accessible from Internal vpc network, therefor you place it in your Internal Subnet with no Public IP assigned to it.假设您希望 EC2 服务器只能从内部 vpc 网络访问,因此您将其放置在您的内部子网中,而没有为其分配公共 IP。 Now You can set SG on your EC2 to only accept IP from the Internal subnet CIDR range of your VPC.现在,您可以将 EC2 上的 SG 设置为仅接受来自 VPC 的内部子网 CIDR 范围的 IP。 By Associating Lambda to that VPC and Placing it in the Private Subnet, Lambda will get arbitrary IP from Internal CIDR Range of your VPC which obviously falls in the SG range already configured for your EC2 (If you are going to have lots of Lambda in parallel just make sure you have enough amount of IPs within your defined CIDR range).通过将 Lambda 关联到该 VPC 并将其放置在私有子网中,Lambda 将从您的 VPC 的内部 CIDR 范围中获取任意 IP,该 IP 显然属于已为您的 EC2 配置的 SG 范围(如果您要并行使用大量 Lambda只需确保您在定义的 CIDR 范围内拥有足够数量的 IP)。

If you want your components to Communicate Internally meanwhile be able to access the Internet as well you can add a NAT Gateway Routable to IGW and then you will add Routing rule to the Internal Subnets to point to your NAT Gateway.如果您希望您的组件在内部通信的同时也能够访问 Internet,您可以向 IGW 添加一个 NAT 网关可路由,然后您将向内部子网添加路由规则以指向您的 NAT 网关。 Therefore all your component within the Internal subnet will be assigned with a Routing Tables pointing to NAT and subsequently the Internet.因此,您在内部子网中的所有组件都将分配有指向 NAT 以及随后指向 Internet 的路由表。

What happens if we want our Lambda function to access resources in our VPC?如果我们希望 Lambda 函数访问 VPC 中的资源,会发生什么? This is a bit of a puzzlement because the Lambda function does not have a stable IP address that we can use as a source in our VPC security groups or in our subnet ACLs.这有点令人困惑,因为 Lambda 函数没有稳定的 IP 地址,我们可以将其用作 VPC 安全组或子网 ACL 中的源。

AWS has overcome these limitations by allowing you to specify a subnet and security group to associate with the Lambda function. AWS 通过允许您指定要与 Lambda 函数关联的子网和安全组来克服这些限制。 You would think that this security group and subnet ACL would control traffic in and out of the Lambda function, but this is wrong.您会认为这个安全组和子网 ACL 会控制进出 Lambda 函数的流量,但这是错误的。 Firstly, Lambda functions do not listen for traffic on any port and so the concept of inbound traffic to a Lambda function is not applicable.首先,Lambda 函数不侦听任何端口上的流量,因此 Lambda 函数的入站流量概念不适用。 Secondly, outbound connections from the Lambda function to VPC resources are not restricted in any way by the Lambda function's security group.其次,从 Lambda 函数到 VPC 资源的出站连接不受 Lambda 函数的安全组以任何方式限制。 So, what is this mysterious Lambda function security group good for and how does it work?那么,这个神秘的 Lambda 函数安全组有什么用,它是如何工作的?

It turns out that the Lambda function's security group is just a naming placeholder that we can use in our other EC2 security groups.结果证明,Lambda 函数的安全组只是一个命名占位符,我们可以在其他 EC2 安全组中使用它。 For example, a Lambda function can make a GET request to an EC2 instance on a private subnet in your VPC.例如,Lambda 函数可以向您的 VPC 中私有子网上的 EC2 实例发出 GET 请求。 We start out with two security groups.我们从两个安全组开始。 The first one is a normal security group that controls traffic to and from our EC2 instance that will be serving up the files on port 8080. It has a single inbound rule that allows port 8080 traffic with a source being the Lambda function's security group.第一个是一个普通的安全组,它控制进出我们的 EC2 实例的流量,这些流量将在端口 8080 上提供文件。它有一个单独的入站规则,允许端口 8080 流量的来源是 Lambda 函数的安全组。 Recall that security group rules can specify source or destination as another security group which is a placeholder for all the hosts that are members of that source or destination security group.回想一下,安全组规则可以将源或目标指定为另一个安全组,它是作为该源或目标安全组成员的所有主机的占位符。

The Lambda function's security group has no rules whatsoever. Lambda 函数的安全组没有任何规则 None are required.不需要。 It is merely a placeholder for the Lambda function that allows us to specify the Lambda function as source in our other EC2 security groups.它只是 Lambda 函数的占位符,允许我们将 Lambda 函数指定为其他 EC2 安全组中的源。 Remember that the Lambda function has a random IP address that changes from day to day and hour to hour, so it is not possible to specify it as source for the inbound traffic to the EC2 instance.请记住,Lambda 函数有一个随机 IP 地址,每天和每小时都在变化,因此无法将其指定为 EC2 实例的入站流量的来源。 Security groups normally control traffic in and out of a network interface but in the case of an AWS Lambda function security group, there is no interface and no rules – it is merely a placeholder.安全组通常控制进出网络接口的流量,但在 AWS Lambda 函数安全组的情况下,没有接口也没有规则——它只是一个占位符。

Your Lambda will have a temporary IP assigned to it when it runs.您的 Lambda 将在运行时为其分配一个临时 IP。 If you configure the IAM role attached to it so it can Allow/Revoke Security Group Ingress, you can make it "let himself in" to your sec group.如果您配置附加到它的 IAM 角色以便它可以允许/撤销安全组入口,您可以让它“让自己进入”您的安全组。 Check out this article for an example on adding an IP or CIDR block to your inbound list.看看这个文章在添加IP或CIDR块入站列表的例子。 I would clean it up straight away when the function is done.功能完成后,我会立即清理它。

You can refer below articles to do the same,你可以参考下面的文章来做同样的事情,
1. https://medium.com/@justanotherspyy/how-to-connect-your-lambda-function-securely-to-your-private-rds-instances-in-your-vpc-29789220a33 1. https://medium.com/@justanotherspyy/how-to-connect-your-lambda-function-securely-to-your-private-rds-instances-in-your-vpc-29789220a33
2. https://medium.com/orchestrated/steps-to-secure-aws-serverless-lambda-part-1-a6e5d1b05f45 2. https://medium.com/orchestrated/steps-to-secure-aws-serverless-lambda-part-1-a6e5d1b05f45

The summary would be,总结是,
1. Create a role for Lambda using below two policies, 1. 使用以下两个策略为 Lambda 创建一个角色,
AWSLambdaBasicExecutionRole — provides CloudWatch logging. AWSLambdaBasicExecutionRole — 提供 CloudWatch 日志记录。
AWSLambdaENIManagementAccess — provides ENI description, creation, and deletion. AWSLambdaENIManagementAccess — 提供 ENI 描述、创建和删除。
2. Put lambda in the same VPC of EC2 and create a security group for the lambda function. 2. 将lambda放在EC2的同一个VPC中,并为lambda函数创建一个安全组。
3. Put this security group in EC2's security group's inbound rules. 3. 将此安全组放入 EC2 的安全组的入站规则中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您不能为现有 IPv4 CIDR 规则指定引用的组 ID。 在 AWS 安全组中编辑入站规则时提示 - You may not specify a referenced group id for an existing IPv4 CIDR rule. prompt when editing the Inbound rule in AWS Security Group 将入站规则添加到安全组 aws cdk - Add inbound rule to security group aws cdk AWS 安全组:入站规则的来源与安全组名称相同吗? - AWS Security group : source of inbound rule same as security group name? 如何使用默认VPC将入站规则添加到AWS安全组? - How to add inbound rule to AWS security group with my default VPC? 使用 Terraform (AWS) 将安全组添加到另一个安全组的入站规则作为源 - Add a Security Group to the Inbound Rule of another Security Group as a Source with Terraform (AWS) Lambda Function 不向安全组添加规则 - Lambda Function Does not Add Rule to Security Group 从Java中的另一个安全组创建AWS安全组入站规则 - Create Aws Security Group Inbound Rule from another security group in java 适用于入站流量的AWS安全组 - AWS security group for inbound traffic 入站 ICMP Cloudformation 安全组规则问题 - Inbound ICMP Cloudformation Security Group Rule Issue AWS Cloudformation:允许所有出口的安全组规则 - AWS Cloudformation: Security Group Rule to allow all egress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM