Advertisement
Guest User

Untitled

a guest
Nov 21st, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. const AWS = require('aws-sdk');
  2. const ec2 = new AWS.EC2();
  3.  
  4. exports.SUCCESS = "SUCCESS";
  5. exports.FAILED = "FAILED";
  6.  
  7. const send = function(event, context, responseStatus, responseData, physicalResourceId) {
  8. var responseBody = JSON.stringify({
  9. Status: responseStatus,
  10. Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
  11. PhysicalResourceId: physicalResourceId || context.logStreamName,
  12. StackId: event.StackId,
  13. RequestId: event.RequestId,
  14. LogicalResourceId: event.LogicalResourceId,
  15. Data: responseData
  16. });
  17.  
  18. console.log("Response body:\n", responseBody);
  19.  
  20. var https = require("https");
  21. var url = require("url");
  22. console.log("EVENT" + event.ResponseURL)
  23. var parsedUrl = url.parse(event.ResponseURL);
  24. var options = {
  25. hostname: parsedUrl.hostname,
  26. port: 443,
  27. path: parsedUrl.path,
  28. method: "PUT",
  29. headers: {
  30. "content-type": "",
  31. "content-length": responseBody.length
  32. }
  33. };
  34.  
  35. var request = https.request(options, function(response) {
  36. console.log("Status code: " + response.statusCode);
  37. console.log("Status message: " + response.statusMessage);
  38. context.done();
  39. });
  40.  
  41. request.on("error", function(error) {
  42. console.log("send(..) failed executing https.request(..): " + error);
  43. context.done();
  44. });
  45.  
  46. request.write(responseBody);
  47. request.end();
  48. };
  49.  
  50. exports.handler = async (event, context) => {
  51. const results = [];
  52. try {
  53. const data = await ec2.describeRegions().promise();
  54.  
  55. const allRegions = data.Regions.map(region => region.RegionName);
  56. console.log('All regions:', allRegions);
  57.  
  58. for (const region of allRegions) {
  59. try {
  60. const ec2Client = new AWS.EC2({ region });
  61. const vpcs = await ec2Client.describeVpcs().promise();
  62. const defaultVpc = vpcs.Vpcs.find(vpc => vpc.IsDefault);
  63.  
  64. if (defaultVpc) {
  65.  
  66.  
  67. results.push('Default VPC in ' + region + ':' + defaultVpc.VpcId);
  68. } else {
  69. results.push('No default VPC found in ' + region);
  70. }
  71. } catch (error) {
  72. results.push('Error in ' + region + ':' + error.message);
  73. }
  74. }
  75. } catch (error) {
  76. results.push('Error fetching active regions: ' + error.message);
  77. }
  78.  
  79. console.log('List of all default VPC:', results);
  80.  
  81. // Use the send function to send the response
  82. await send(event, context, exports.SUCCESS, JSON.stringify(results));
  83. };
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement