From 082cbbbb6a9859348ce3adfa3484c65acb746925 Mon Sep 17 00:00:00 2001 From: Pawan Puthran Date: Fri, 15 Nov 2019 17:02:25 +0000 Subject: [PATCH 1/3] updating the runtime version to Node10 --- packaged.yaml | 58 ++++++++++++++++ preTrafficHook.js | 2 +- returnS3Buckets.js | 58 +++++++++------- ...rnS3BucketsNew.js => returnS3Bucketsold.js | 66 ++++++++----------- template.yaml | 4 +- 5 files changed, 125 insertions(+), 63 deletions(-) create mode 100644 packaged.yaml rename returnS3BucketsNew.js => returnS3Bucketsold.js (56%) diff --git a/packaged.yaml b/packaged.yaml new file mode 100644 index 0000000..268f899 --- /dev/null +++ b/packaged.yaml @@ -0,0 +1,58 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: An example SAM template for Lambda Safe Deployments. +Resources: + preTrafficHook: + Properties: + CodeUri: s3://aws-samples-pputhran-frankfurt/fd41e1dc2b50364fc5e54e2eca42093b + DeploymentPreference: + Enabled: false + Environment: + Variables: + NewVersion: + Ref: returnS3Buckets.Version + FunctionName: CodeDeployHook_preTrafficHook + Handler: preTrafficHook.handler + Policies: + - Statement: + - Action: + - codedeploy:PutLifecycleEventHookExecutionStatus + Effect: Allow + Resource: + Fn::Sub: arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/* + Version: '2012-10-17' + - Statement: + - Action: + - lambda:InvokeFunction + Effect: Allow + Resource: + Ref: returnS3Buckets.Version + Version: '2012-10-17' + Runtime: nodejs10.x + Timeout: 5 + Type: AWS::Serverless::Function + returnS3Buckets: + Properties: + AutoPublishAlias: live + CodeUri: s3://aws-samples-pputhran-frankfurt/fd41e1dc2b50364fc5e54e2eca42093b + DeploymentPreference: + Hooks: + PreTraffic: + Ref: preTrafficHook + Type: Linear10PercentEvery1Minute + Events: + Api: + Properties: + Method: get + Path: /test + Type: Api + Handler: returnS3Buckets.handler + Policies: + - Statement: + - Action: + - s3:ListAllMyBuckets + Effect: Allow + Resource: '*' + Version: '2012-10-17' + Runtime: nodejs10.x + Type: AWS::Serverless::Function +Transform: AWS::Serverless-2016-10-31 diff --git a/preTrafficHook.js b/preTrafficHook.js index 75f8634..e6138e7 100644 --- a/preTrafficHook.js +++ b/preTrafficHook.js @@ -54,7 +54,7 @@ exports.handler = (event, context, callback) => { // "statusCode": 200, // "body": 51 // } - if(result.body == 9){ + if(result.body == 29){ lambdaResult = "Succeeded"; console.log ("Validation testing succeeded!"); } diff --git a/returnS3Buckets.js b/returnS3Buckets.js index f633c43..18826f4 100644 --- a/returnS3Buckets.js +++ b/returnS3Buckets.js @@ -15,30 +15,42 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - 'use strict'; +'use strict'; -var AWS = require('aws-sdk'); -var s3 = new AWS.S3(); +const AWS = require('aws-sdk'); +const s3 = new AWS.S3(); -exports.handler = (event, context, callback) => { - console.log("I am here! " + context.functionName + ":" + context.functionVersion); +exports.handler = async (event, context) => { + console.log(`I am here! " ${context.functionName} + ":" + ${context.functionVersion}`); - s3.listBuckets(function (err, data){ - if(err){ - console.log(err, err.stack); - callback(null, { - statusCode: 500, - body: "Failed!" - }); - } - else{ - var allBuckets = data.Buckets; - - console.log("Total buckets: " + allBuckets.length); - callback(null, { - statusCode: 200, - body: allBuckets.length - }); + let response + try { + + const data = await s3.listBuckets().promise(); + const allBuckets = data.Buckets; + + console.log(`Total buckets: ${allBuckets.length}`) + + // New Code begins here + let counter = 0; + for (var i in allBuckets) { + if (allBuckets[i].Name[0] === "a") + counter++; } - }); -} \ No newline at end of file + console.log(`Total buckets starting with a: ${counter}`); + + response = { + statusCode: 200, + body: counter + }; + + } catch (error) { + console.log(`Error - `, error, error.stack); + return { + statusCode: 500, + body: JSON.stringify(error) + }; + } + + return response +} diff --git a/returnS3BucketsNew.js b/returnS3Bucketsold.js similarity index 56% rename from returnS3BucketsNew.js rename to returnS3Bucketsold.js index 39a023f..87f8252 100644 --- a/returnS3BucketsNew.js +++ b/returnS3Bucketsold.js @@ -1,4 +1,4 @@ -/* +1/* * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of this @@ -17,39 +17,31 @@ 'use strict'; -var AWS = require('aws-sdk'); -var s3 = new AWS.S3(); - -exports.handler = (event, context, callback) => { - console.log("I am here! " + context.functionName + ":" + context.functionVersion); - - s3.listBuckets(function (err, data){ - if(err){ - console.log(err, err.stack); - callback(null, { - statusCode: 500, - body: "Failed!" - }); - } - else{ - var allBuckets = data.Buckets; - - console.log("Total buckets: " + allBuckets.length); - //callback(null, allBuckets.length); - - // New Code begins here - var counter=0; - for(var i in allBuckets){ - if(allBuckets[i].Name[0] === "a") - counter++; - } - console.log("Total buckets starting with a: " + counter); - - callback(null, { - statusCode: 200, - body: counter - }); - - } - }); -} \ No newline at end of file +const AWS = require('aws-sdk'); +const s3 = new AWS.S3(); + +exports.handler = async (event, context) => { + console.log(`I am here! " ${context.functionName} + ":" + ${context.functionVersion}`); + let response + try { + const data = await s3.listBuckets().promise(); + const allBuckets = data.Buckets; + + console.log(`Total buckets: ${allBuckets.length}`) + + response = { + statusCode: 200, + body: allBuckets.length + }; + + } catch (error) { + console.log(`Error - `, error, error.stack); + return { + statusCode: 500, + body: JSON.stringify(error) + }; + } + + return response +} + diff --git a/template.yaml b/template.yaml index 1222a8f..1763bdf 100644 --- a/template.yaml +++ b/template.yaml @@ -8,7 +8,7 @@ Resources: Type: AWS::Serverless::Function Properties: Handler: returnS3Buckets.handler - Runtime: nodejs6.10 + Runtime: nodejs10.x AutoPublishAlias: live Policies: - Version: "2012-10-17" @@ -46,7 +46,7 @@ Resources: Action: - "lambda:InvokeFunction" Resource: !Ref returnS3Buckets.Version - Runtime: nodejs6.10 + Runtime: nodejs10.x FunctionName: 'CodeDeployHook_preTrafficHook' DeploymentPreference: Enabled: false From 42bdd88287064939d07d9a1120f304870cf3f3ad Mon Sep 17 00:00:00 2001 From: Pawan Puthran Date: Fri, 15 Nov 2019 17:07:30 +0000 Subject: [PATCH 2/3] removing packaged.yaml file --- packaged.yaml | 58 --------------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 packaged.yaml diff --git a/packaged.yaml b/packaged.yaml deleted file mode 100644 index 268f899..0000000 --- a/packaged.yaml +++ /dev/null @@ -1,58 +0,0 @@ -AWSTemplateFormatVersion: '2010-09-09' -Description: An example SAM template for Lambda Safe Deployments. -Resources: - preTrafficHook: - Properties: - CodeUri: s3://aws-samples-pputhran-frankfurt/fd41e1dc2b50364fc5e54e2eca42093b - DeploymentPreference: - Enabled: false - Environment: - Variables: - NewVersion: - Ref: returnS3Buckets.Version - FunctionName: CodeDeployHook_preTrafficHook - Handler: preTrafficHook.handler - Policies: - - Statement: - - Action: - - codedeploy:PutLifecycleEventHookExecutionStatus - Effect: Allow - Resource: - Fn::Sub: arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/* - Version: '2012-10-17' - - Statement: - - Action: - - lambda:InvokeFunction - Effect: Allow - Resource: - Ref: returnS3Buckets.Version - Version: '2012-10-17' - Runtime: nodejs10.x - Timeout: 5 - Type: AWS::Serverless::Function - returnS3Buckets: - Properties: - AutoPublishAlias: live - CodeUri: s3://aws-samples-pputhran-frankfurt/fd41e1dc2b50364fc5e54e2eca42093b - DeploymentPreference: - Hooks: - PreTraffic: - Ref: preTrafficHook - Type: Linear10PercentEvery1Minute - Events: - Api: - Properties: - Method: get - Path: /test - Type: Api - Handler: returnS3Buckets.handler - Policies: - - Statement: - - Action: - - s3:ListAllMyBuckets - Effect: Allow - Resource: '*' - Version: '2012-10-17' - Runtime: nodejs10.x - Type: AWS::Serverless::Function -Transform: AWS::Serverless-2016-10-31 From fecf06e439421e1f9de3386c7219904535e6e4e5 Mon Sep 17 00:00:00 2001 From: Pawan Puthran Date: Fri, 15 Nov 2019 17:10:04 +0000 Subject: [PATCH 3/3] renaming files --- returnS3Buckets.js | 15 +++------------ returnS3Bucketsold.js => returnS3BucketsNew.js | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 15 deletions(-) rename returnS3Bucketsold.js => returnS3BucketsNew.js (87%) diff --git a/returnS3Buckets.js b/returnS3Buckets.js index 18826f4..87f8252 100644 --- a/returnS3Buckets.js +++ b/returnS3Buckets.js @@ -1,4 +1,4 @@ -/* +1/* * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of this @@ -22,26 +22,16 @@ const s3 = new AWS.S3(); exports.handler = async (event, context) => { console.log(`I am here! " ${context.functionName} + ":" + ${context.functionVersion}`); - let response try { - const data = await s3.listBuckets().promise(); const allBuckets = data.Buckets; console.log(`Total buckets: ${allBuckets.length}`) - // New Code begins here - let counter = 0; - for (var i in allBuckets) { - if (allBuckets[i].Name[0] === "a") - counter++; - } - console.log(`Total buckets starting with a: ${counter}`); - response = { statusCode: 200, - body: counter + body: allBuckets.length }; } catch (error) { @@ -54,3 +44,4 @@ exports.handler = async (event, context) => { return response } + diff --git a/returnS3Bucketsold.js b/returnS3BucketsNew.js similarity index 87% rename from returnS3Bucketsold.js rename to returnS3BucketsNew.js index 87f8252..18826f4 100644 --- a/returnS3Bucketsold.js +++ b/returnS3BucketsNew.js @@ -1,4 +1,4 @@ -1/* +/* * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of this @@ -22,16 +22,26 @@ const s3 = new AWS.S3(); exports.handler = async (event, context) => { console.log(`I am here! " ${context.functionName} + ":" + ${context.functionVersion}`); + let response try { + const data = await s3.listBuckets().promise(); const allBuckets = data.Buckets; console.log(`Total buckets: ${allBuckets.length}`) + // New Code begins here + let counter = 0; + for (var i in allBuckets) { + if (allBuckets[i].Name[0] === "a") + counter++; + } + console.log(`Total buckets starting with a: ${counter}`); + response = { statusCode: 200, - body: allBuckets.length + body: counter }; } catch (error) { @@ -44,4 +54,3 @@ exports.handler = async (event, context) => { return response } -