前提
nodejsでhttps://example.com にアクセスするコードを書いて、lambdaで動かしています。
VPCを設定していない時は問題なくアクセス出来るのですが、VPCを設定するとアクセス出来たり出来なかったりします。
アクセスできる時は問題なく出来るのですが、1時間後等に実行すると、httpリクエストが返ってこなくなり lambdaのタイムアウトになってしまいます。
lambdaのタイムアウトの時間は1分まで伸ばして試しましたが、状況は変わりませんでした。
実行方法は、awsのlambdaの管理画面のテストページからコードを実行して確認しました。
httpにアクセスするnodejsのコード抜粋は以下の通りです。
js:index.ts
1async function getExampleCom(accessUrl: string) { 2 console.log(`${accessUrl} へのアクセス 開始`); // この行のログは出力されているので、lambdaのコールドスタート問題でも無いと想います 3 try { 4 const response = await fetch(accessUrl); 5 const body = await response.text(); 6 const bodyText = body.replace(/\s+/g, " "); 7 console.log(`${accessUrl} へのアクセス 成功 ${bodyText.substring(0, 100)}`); 8 } catch (e) { 9 console.log(`${accessUrl} へのアクセス 失敗 ${e}`); 10 } 11}
VPCを設定した事が問題だと思うのですが、時間が経つと(lambdaから)インターネットにアクセス出来たり 出来なくなったりする現象が解消出来ません。
ご存じの方、教えてください。
VPCの環境はcloudFormationで以下の通りです。
こちらから抜粋して一部修正しています。
yaml
1AWSTemplateFormatVersion: '2010-09-09' 2Transform: 'AWS::Serverless-2016-10-31' 3Description: hoge環境 4Resources: 5 6Resources: 7 VPC: 8 Type: "AWS::EC2::VPC" 9 Properties: 10 CidrBlock: "10.0.0.0/16" 11 InstanceTenancy: "default" 12 EnableDnsHostnames: true # 2022-0901-0352 この行を追加 13 EnableDnsSupport: true # 2022-0901-0352 この行を追加 14 Tags: 15 - Key: "Name" 16 Value: "vpc-test-2022-0901-0106" 17 18 InternetGateway: 19 Type: "AWS::EC2::InternetGateway" 20 21 VPCGatewayAttachment: 22 Type: "AWS::EC2::VPCGatewayAttachment" 23 Properties: 24 VpcId: !Ref "VPC" 25 InternetGatewayId: !Ref "InternetGateway" 26 27 RouteTable: 28 Type: "AWS::EC2::RouteTable" 29 Properties: 30 VpcId: !Ref "VPC" 31 32 InternetRoute: 33 Type: "AWS::EC2::Route" 34 Properties: 35 DestinationCidrBlock: "0.0.0.0/0" 36 GatewayId: !Ref InternetGateway 37 RouteTableId: !Ref RouteTable 38 39 SubnetA: 40 Type: "AWS::EC2::Subnet" 41 Properties: 42 AvailabilityZone: !Select 43 - 0 44 - !GetAZs 45 Ref: 'AWS::Region' 46 VpcId: !Ref "VPC" 47 CidrBlock: "10.0.0.0/24" 48 49 SubnetB: 50 Type: "AWS::EC2::Subnet" 51 Properties: 52 AvailabilityZone: !Select 53 - 1 54 - !GetAZs 55 Ref: 'AWS::Region' 56 VpcId: !Ref "VPC" 57 CidrBlock: "10.0.1.0/24" 58 59 SubnetC: 60 Type: "AWS::EC2::Subnet" 61 Properties: 62 AvailabilityZone: !Select 63 - 2 64 - !GetAZs 65 Ref: 'AWS::Region' 66 VpcId: !Ref "VPC" 67 CidrBlock: "10.0.2.0/24" 68 69 SubnetARouteTableAssociation: 70 Type: "AWS::EC2::SubnetRouteTableAssociation" 71 Properties: 72 RouteTableId: !Ref RouteTable 73 SubnetId: !Ref SubnetA 74 75 EIP: 76 Type: "AWS::EC2::EIP" 77 Properties: 78 Domain: "vpc" 79 80 Nat: 81 Type: "AWS::EC2::NatGateway" 82 Properties: 83 AllocationId: !GetAtt "EIP.AllocationId" 84 SubnetId: !Ref "SubnetA" 85 86 NatRouteTable: 87 Type: "AWS::EC2::RouteTable" 88 Properties: 89 VpcId: !Ref "VPC" 90 91 NatRoute: 92 Type: "AWS::EC2::Route" 93 Properties: 94 DestinationCidrBlock: "0.0.0.0/0" 95 NatGatewayId: !Ref "Nat" 96 RouteTableId: !Ref "NatRouteTable" 97 98 SubnetBRouteTableAssociation: 99 Type: "AWS::EC2::SubnetRouteTableAssociation" 100 Properties: 101 RouteTableId: !Ref NatRouteTable 102 SubnetId: !Ref SubnetB 103 104 SubnetCRouteTableAssociation: 105 Type: "AWS::EC2::SubnetRouteTableAssociation" 106 Properties: 107 RouteTableId: !Ref NatRouteTable 108 SubnetId: !Ref SubnetC 109 110 InstanceSecurityGroup: 111 Type: "AWS::EC2::SecurityGroup" 112 Properties: 113 GroupName: "all-wildcard-security-group" 114 GroupDescription: "Lambda Traffic" 115 VpcId: !Ref "VPC" 116 SecurityGroupEgress: 117 - IpProtocol: "-1" 118 CidrIp: "0.0.0.0/0" 119 120 InstanceSecurityGroupIngress: 121 Type: "AWS::EC2::SecurityGroupIngress" 122 DependsOn: "InstanceSecurityGroup" 123 Properties: 124 GroupId: !Ref "InstanceSecurityGroup" 125 IpProtocol: "tcp" 126 FromPort: "0" 127 ToPort: "65535" 128 SourceSecurityGroupId: !Ref "InstanceSecurityGroup" 129 130 InstanceSecurityGroupIngress2: 131 Type: "AWS::EC2::SecurityGroupIngress" 132 DependsOn: "InstanceSecurityGroup" 133 Properties: 134 GroupId: !Ref "InstanceSecurityGroup" 135 IpProtocol: -1 136 CidrIp: "0.0.0.0/0" 137 138 InstanceSecurityGroupIngress3: 139 Type: "AWS::EC2::SecurityGroupIngress" 140 DependsOn: "InstanceSecurityGroup" 141 Properties: 142 GroupId: !Ref "InstanceSecurityGroup" 143 IpProtocol: -1 144 CidrIpv6: "::/0" 145 146 147 148Outputs: 149 VpcId: 150 Description: The VPC ID 151 Value: !Ref VPC