1

私の問題は、IGWを使用してVPC内でNATの背後で実行するラムダ機能がインターネットにアクセスできないことです。VPCのAWS LambdaにはNATの背後にインターネットアクセスがありません

私が持っているVPC作成されてやろうとしている:

  • インターネットゲートウェイを。
  • 2つのプライベートサブネット(PrivateAおよびPrivateB)は、空き領域にそれぞれAおよびBです。 PublicAサブネット
  • PrivateAPrivateB
  • 1アベイラビリティゾーンA
  • NATゲートウェイでパブリックサブネット(PublicA)は、そのNATゲートウェイにルーティング0.0.0.0/0をルートテーブルを有しています。
  • PublicAには、0.0.0.0/0をインターネットゲートウェイにルーティングするルートテーブルがあります。
  • プライベートサブネットおよびパブリックサブネットには、すべての入力トラフィックと出力トラフィックを許可するアクセス制御リストがあります。

その一部の種類の作品。

次に、VPC内にラムダ関数を作成します。私はそれをPrivateAPrivateBに入れて、すべてのEgressトラフィックとIngressトラフィックを許可するセキュリティグループを割り当てます。

以下は、この問題を再現する自立型の例(テンプレート全体)です。私はインターネット上のすべての可能なドキュメントと記事を読んだので、誰かが私を正しい方向に向けることができれば非常に感謝しています。

{ 
    "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

関連する問題