1

내 문제는 IGW가있는 VPC 내부에서 NAT 뒤에서 실행되는 람다 기능이 인터넷의 모든 것에 액세스 할 수 없다는 것입니다.AWS VPC의 람다가 NAT 뒤에서 인터넷에 액세스 할 수 없습니다.

내가 가지고있는 VPC 만드는 할 노력하고있어 :

  • 인터넷 게이트웨이;
  • 가용성 영역에 2 개의 개인 서브넷 (PrivateAPrivateB)이 각각 AB입니다. 가용 영역 PublicA 서브넷 A
  • 게이트웨이에서 NAT
  • 1 공용 서브넷 (PublicA)는
  • PrivateAPrivateB 및 그 라우팅 NAT 게이트웨이 0.0.0.0/0 경로 표를 갖는다.
  • PublicA에는 0.0.0.0/0을 인터넷 게이트웨이로 라우팅하는 경로 테이블이 있습니다.
  • 공용 서브넷과 함께 개인 서브넷에는 모든 Ingress 및 Egress 트래픽을 허용하는 액세스 제어 목록이 있습니다.

그 부분은 작동합니다.

다음은 VPC 내부에 람다 함수를 만들고 싶습니다. 나는 PrivateAPrivateB에 넣고 모든 송신 및 수신 트래픽을 허용하는 보안 그룹을 할당합니다.

다음은이 문제를 재현 한 자체 포함 예제 (전체 템플릿)입니다. 나는 모든 가능한 문서와 기사를 인터넷에서 읽었으므로 누군가가 올바른 방향으로 나를 가리킬 수 있다면 매우 감사 할 것입니다.

{ 
    "AWSTemplateFormatVersion": "2010-09-09", 
    "Resources": { 

    "Vpc": { 
     "Type": "AWS::EC2::VPC", 
     "Properties": { 
     "CidrBlock": "10.0.0.0/16", 
     "EnableDnsSupport": true, 
     "EnableDnsHostnames": true, 
     "InstanceTenancy": "default" 
     } 
    }, 

    "InternetGateway": { 
     "Type": "AWS::EC2::InternetGateway" 
    }, 

    "VpcGatewayAttachment": { 
     "Type": "AWS::EC2::VPCGatewayAttachment", 
     "Properties": { 
     "VpcId": { "Ref": "Vpc" }, 
     "InternetGatewayId": { "Ref": "InternetGateway" } 
     } 
    }, 

    "ElasticIP":{ 
     "Type": "AWS::EC2::EIP", 
     "Properties": { 
     "Domain": "vpc" 
     } 
    }, 

    "NatGateway": { 
     "Type": "AWS::EC2::NatGateway", 
     "DependsOn": [ "VpcGatewayAttachment" ], 
     "Properties": { 
     "AllocationId": { "Fn::GetAtt": [ "ElasticIP", "AllocationId" ] }, 
     "SubnetId": { "Ref": "SubnetAPublic" } 
     } 
    }, 

    "SubnetAPublic": { 
     "Type": "AWS::EC2::Subnet", 
     "Properties": { 
     "AvailabilityZone": { "Fn::Select" : [ "0", { "Fn::GetAZs" : "" } ] }, 
     "CidrBlock": "10.0.0.0/19", 
     "MapPublicIpOnLaunch": true, 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "SubnetAPrivate": { 
     "Type": "AWS::EC2::Subnet", 
     "Properties": { 
     "AvailabilityZone": { "Fn::Select" : [ "0", { "Fn::GetAZs" : "" } ] }, 
     "CidrBlock": "10.0.64.0/19", 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "SubnetBPrivate": { 
     "Type": "AWS::EC2::Subnet", 
     "Properties": { 
     "AvailabilityZone": { "Fn::Select" : [ "1", { "Fn::GetAZs" : "" } ] }, 
     "CidrBlock": "10.0.96.0/19", 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "RouteTablePublic": { 
     "Type": "AWS::EC2::RouteTable", 
     "Properties": { 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "RouteTablePrivate": { 
     "Type": "AWS::EC2::RouteTable", 
     "Properties": { 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "RouteTableAssociationAPublic": { 
     "Type": "AWS::EC2::SubnetRouteTableAssociation", 
     "Properties": { 
     "SubnetId": { "Ref": "SubnetAPublic" }, 
     "RouteTableId": { "Ref": "RouteTablePublic" } 
     } 
    }, 

    "RouteTableAssociationAPrivate": { 
     "Type": "AWS::EC2::SubnetRouteTableAssociation", 
     "Properties": { 
     "SubnetId": { "Ref": "SubnetAPrivate" }, 
     "RouteTableId": { "Ref": "RouteTablePrivate" } 
     } 
    }, 

    "RouteTableAssociationBPrivate": { 
     "Type": "AWS::EC2::SubnetRouteTableAssociation", 
     "Properties": { 
     "SubnetId": { "Ref": "SubnetBPrivate" }, 
     "RouteTableId": { "Ref": "RouteTablePrivate" } 
     } 
    }, 

    "RouteTablePrivateInternetRoute": { 
     "Type": "AWS::EC2::Route", 
     "DependsOn": [ "VpcGatewayAttachment" ], 
     "Properties": { 
     "RouteTableId": { "Ref": "RouteTablePrivate" }, 
     "DestinationCidrBlock": "0.0.0.0/0", 
     "NatGatewayId": { "Ref": "NatGateway" } 
     } 
    }, 

    "RouteTablePublicInternetRoute": { 
     "Type": "AWS::EC2::Route", 
     "DependsOn": [ "VpcGatewayAttachment" ], 
     "Properties": { 
     "RouteTableId": { "Ref": "RouteTablePublic" }, 
     "DestinationCidrBlock": "0.0.0.0/0", 
     "GatewayId": { "Ref": "InternetGateway" } 
     } 
    }, 

    "NetworkAclPublic": { 
     "Type": "AWS::EC2::NetworkAcl", 
     "Properties": { 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "NetworkAclPrivate": { 
     "Type": "AWS::EC2::NetworkAcl", 
     "Properties": { 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "SubnetNetworkAclAssociationAPublic": { 
     "Type": "AWS::EC2::SubnetNetworkAclAssociation", 
     "Properties":{ 
     "SubnetId": { "Ref": "SubnetAPublic" }, 
     "NetworkAclId": { "Ref": "NetworkAclPublic" } 
     } 
    }, 

    "SubnetNetworkAclAssociationAPrivate": { 
     "Type": "AWS::EC2::SubnetNetworkAclAssociation", 
     "Properties":{ 
     "SubnetId": { "Ref": "SubnetAPrivate" }, 
     "NetworkAclId": { "Ref": "NetworkAclPrivate" } 
     } 
    }, 

    "SubnetNetworkAclAssociationBPrivate": { 
     "Type": "AWS::EC2::SubnetNetworkAclAssociation", 
     "Properties": { 
     "SubnetId": { "Ref": "SubnetBPrivate" }, 
     "NetworkAclId": { "Ref": "NetworkAclPrivate" } 
     } 
    }, 

    "NetworkAclEntryInPublicAllowAll": { 
     "Type": "AWS::EC2::NetworkAclEntry", 
     "Properties": { 
     "NetworkAclId": { "Ref": "NetworkAclPublic" }, 
     "RuleNumber": 99, 
     "Protocol": -1, 
     "RuleAction": "allow", 
     "Egress": false, 
     "CidrBlock": "0.0.0.0/0" 
     } 
    }, 

    "NetworkAclEntryOutPublicAllowAll": { 
     "Type": "AWS::EC2::NetworkAclEntry", 
     "Properties": { 
     "NetworkAclId": { "Ref": "NetworkAclPublic" }, 
     "RuleNumber": 99, 
     "Protocol": -1, 
     "RuleAction": "allow", 
     "Egress": true, 
     "CidrBlock": "0.0.0.0/0" 
     } 
    }, 

    "NetworkAclEntryInPrivateAllowVpc": { 
     "Type": "AWS::EC2::NetworkAclEntry", 
     "Properties": { 
     "NetworkAclId": { "Ref": "NetworkAclPrivate" }, 
     "RuleNumber": 99, 
     "Protocol": -1, 
     "RuleAction": "allow", 
     "Egress": false, 
     "CidrBlock": "0.0.0.0/16" 
     } 
    }, 

    "NetworkAclEntryOutPrivateAllowVpc": { 
     "Type": "AWS::EC2::NetworkAclEntry", 
     "Properties": { 
     "NetworkAclId": { "Ref": "NetworkAclPrivate" }, 
     "RuleNumber": 99, 
     "Protocol": -1, 
     "RuleAction": "allow", 
     "Egress": true, 
     "CidrBlock": "0.0.0.0/0" 
     } 
    }, 

    "LambdasSecurityGroup": { 
     "Type": "AWS::EC2::SecurityGroup", 
     "Properties": { 
     "GroupDescription": "Lambdas security group", 
     "SecurityGroupEgress": [ 
      { "CidrIp": "0.0.0.0/0", "IpProtocol": "-1" } 
     ], 
     "SecurityGroupIngress": [ 
      { "CidrIp": "0.0.0.0/0", "IpProtocol": "-1" } 
     ], 
     "VpcId": { "Ref": "Vpc" } 
     } 
    }, 

    "LambdaFunctionExecutionRole": { 
     "Type": "AWS::IAM::Role", 
     "Properties": { 
     "AssumeRolePolicyDocument": { 
      "Version": "2012-10-17", 
      "Statement": [ 
      { 
       "Effect": "Allow", 
       "Principal": { "Service": "lambda.amazonaws.com" }, 
       "Action": "sts:AssumeRole" 
      } 
      ] 
     }, 
     "ManagedPolicyArns": [ 
      "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", 
      "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" 
     ] 
     } 
    }, 

    "LambdaFunction": { 
     "Type": "AWS::Lambda::Function", 
     "Properties": { 
     "Handler": "index.lambda_handler", 
     "Runtime": "python2.7", 
     "Role": { 
      "Fn::GetAtt": ["LambdaFunctionExecutionRole", "Arn"] 
     }, 
     "Code": { 
      "ZipFile": { 
      "Fn::Join": ["\n", [ 
       "import urllib2", 
       "def lambda_handler(event, context):", 
       "\tresponse = urllib2.urlopen('http://python.org/')", 
       "\treturn response.read()" 
      ]] 
      } 
     }, 
     "VpcConfig": { 
      "SecurityGroupIds": [ 
      { "Fn::GetAtt": [ "LambdasSecurityGroup", "GroupId"] } 
      ], 
      "SubnetIds": [ 
      { "Ref": "SubnetAPrivate" }, 
      { "Ref": "SubnetBPrivate" } 
      ] 
     } 
     } 
    } 
    } 
} 
+2

개인 서브넷의 EC2 인스턴스가 인터넷에 액세스 할 수 있는지 여부를 테스트 했습니까? –

답변

3

실패한 연결의 원인은 "NetworkAclEntryInPrivateAllowVpc"와 "NetworkAclEntryOutPrivateAllowVpc"에 대한 ACL의 설정 내에 자리 잡고 있습니다.

"0.0.0.0/16"에서 "0.0.0.0/0"으로 해당 CIDR 블록을 열면 람다가 인터넷에 액세스 할 수 있습니다.

NAT에 대해 잘 알지 못하지만 NAT 트래픽이 해당 ACL 규칙에 의해 차단 된 것으로 보입니다.

+0

하하, 나는 하루 종일 그걸로 싸우고 그 오타 같은 간단한 일들을 알지 못했습니다. '/ 0'으로되어있었습니다. 문제가 해결되었습니다. 감사합니다. 진짜 스택에서 시도해 볼 것입니다. – ILya