{"id":6200,"date":"2019-07-24T01:13:33","date_gmt":"2019-07-24T01:13:33","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=6200"},"modified":"2019-07-24T01:13:33","modified_gmt":"2019-07-24T01:13:33","slug":"debugging-aws-services-with-lambda","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/debugging-aws-services-with-lambda\/","title":{"rendered":"Debugging AWS Services with Lambda"},"content":{"rendered":"<p>Say you have connectivity issues between AWS services in a VPC. Rather than change the existing system, you can debug it using a lambda that runs HTTP requests. I&#8217;ve included an example below. You can use this lambda to test many URLs and configurations quickly to find a range of problems.<\/p>\n<p>Note that to communicate with AWS services, you must &#8220;sign&#8221; AWS HTTP requests. This allows the destination AWS service to know what IAM role the lambda runs as. This example uses a library called aws4 to sign URLs. <\/p>\n<p>This lambda is small enough to edit through the AWS console, so you can fiddle with the requests once you have it deployed.<\/p>\n<pre lang=\"javascript\">\nexports.handler = (event, context, cb) => {\n  const aws4 = require('aws4');\n  const https = require('https');\n \n  \/\/ Update this to match the AWS url you want to test.\n  const signed = aws4.sign(\n    {\n      host: 'test.us-east-1.es.amazonaws.com', \n      path: '\/'\n    });\n \n  console.log(\"Requesting \" + signed);\n \n  https.get(signed, (resp) => {\n    let data = '';\n \n    resp.on('data', (chunk) => {\n      console.log('received ' + chunk);\n      data += chunk;\n    });\n \n    resp.on('end', () => {\n      console.log('DATA COMPLETE: ' + JSON.parse(data).explanation);\n      cb();\n    });\n  }).on(\"error\", (err) => {\n    console.log(\"ERRORED OUT: \" + err.message);\n \n    cb();\n  });\n};\n<\/pre>\n<p>Once you deploy this lambda, set the VPC, security group, subnets, and role and run it through the AWS console &#8211; you can name the test event anything you want. <\/p>\n<p>If the lambda times out, you likely have a network connectivity problem &#8211; e.g. a security group doesn&#8217;t allow outbound traffic, a firewall prevents traffic to the destination, etc. If you do have network connectivity but it fails quickly, you may have a problem with IAM roles or the HTTPS certificate on the destination. <\/p>\n<p>Note you can&#8217;t do npm install within a lambda because lambda has a readonly filesystem &#8211; I tried doing this first to create something you could just paste into the lambda console.<\/p>\n<p>The follow script will deploy &#8211; you can also drop the VPC settings entirely and set them through the console.<\/p>\n<pre lang=\"bash\">\nzip -r lambda.zip .\n \naws lambda create-function \\\n  --function-name test-lambda \\\n  --zip-file fileb:\/\/lambda.zip \\\n  --runtime 'nodejs10.x' \\\n  --handler 'index.handler' \\\n  --role 'arn:aws:iam::***:role\/***' \\\n  --vpc-config 'SubnetIds=subnet-***,subnet-***,SecurityGroupIds=sg-***'(base)\n<\/pre>\n<p><a href=\"https:\/\/github.com\/garysieling\/aws-test-lambda\">Full source here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A test lambda for AWS<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[71,334],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6200"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=6200"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6200\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=6200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=6200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=6200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}