Forked from alexcasalboni/aws-sam-automatic-rollback-demo.md
Created
May 19, 2019 01:23
Revisions
-
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This demo was presented at the AWS Summit @ Cape Town on Jul 12th. You can find the slides [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps-aws-summit-cape-town). ## What's included in this Gist? -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # How to Build and Deploy Serverless Apps [AWS Summit] This demo was presented at the AWS Summit @ Cape Town on Jul 12th. -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,4 +15,5 @@ You can find the slides [here](https://speakerdeck.com/alexcasalboni/how-to-buil * *script_stats.sh*: It reads the txt file and counts OK and KO responses to show some statistics Feel free to reach out to me [on Twitter](https://twitter.com/alex_casalboni) if you need help or if you just want to say hi :) -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,4 +12,7 @@ You can find the slides [here](https://speakerdeck.com/alexcasalboni/how-to-buil * *script_deploy.sh*: It packages and deploys the AWS SAM template, then it shows the `ENDPOINT` Output * *script_poll.sh*: It polls a given URL forever and stores responses and timings in a txt file * *script_tail.sh*: It tails the txt file that contains responses and timings to show live responses * *script_stats.sh*: It reads the txt file and counts OK and KO responses to show some statistics Feel free to reach out to me [on Twitter](https://twitter.com/alex_casalboni) if you need help or if you just want to say hi :) -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This demo was presented at the AWS Summit @ Cape Town on Jul 12th. You can find the slides [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps). ## What's included in this Gist? -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-b * *index.js*: The node.js code used for AWS Lambda * *sam_template.yaml*: The AWS SAM template in YAML format (i.e. CloudFormation) * *scripts*: Four scripts used to showcase the live deployment rollback. * *script_deploy.sh*: It packages and deploys the AWS SAM template, then it shows the `ENDPOINT` Output * *script_poll.sh*: It polls a given URL forever and stores responses and timings in a txt file * *script_tail.sh*: It tails the txt file that contains responses and timings to show live responses * *script_stats.sh*: It reads the txt file and counts OK and KO responses to show some statistics -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,5 +8,8 @@ You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-b * *index.js*: The node.js code used for AWS Lambda * *sam_template.yaml*: The AWS SAM template in YAML format (i.e. CloudFormation) * *scripts*: Four scripts used to showcase the live deployment rollback. * *script_deploy.sh*: It packages and deploys the AWS SAM template, then it shows the `ENDPOINT` CloudFormation Output * *script_poll.sh*: It polls a given URL forever and stores responses and timings in a txt file * *script_tail.sh*: It tails the txt file that contains responses and timings to show live responses * *script_stats.sh*: It reads the txt file and counts OK and KO responses to show some statistics -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,4 +9,4 @@ You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-b * *index.js*: The node.js code used for AWS Lambda * *sam_template.yaml*: The AWS SAM template in YAML format (i.e. CloudFormation) * *scripts*: four scripts used to showcase the live deployment rollback. ** test -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,4 +9,4 @@ You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-b * *index.js*: The node.js code used for AWS Lambda * *sam_template.yaml*: The AWS SAM template in YAML format (i.e. CloudFormation) * *scripts*: four scripts used to showcase the live deployment rollback. * * test -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,8 +4,9 @@ This demo was presented at the AWS Summit @ Cape Town on Jul 12th. You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps). ## What's included in this Gist? * *index.js*: The node.js code used for AWS Lambda * *sam_template.yaml*: The AWS SAM template in YAML format (i.e. CloudFormation) * *scripts*: four scripts used to showcase the live deployment rollback. ** test -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,4 +2,10 @@ This demo was presented at the AWS Summit @ Cape Town on Jul 12th. You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps). ## What does the code include? * *index.js*: The node.js code used for AWS Lambda * *sam_template.yaml*: The AWS SAM template in YAML format (i.e. CloudFormation) * *scripts*: four scripts used to showcase the live deployment rollback -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ # How to Build and Deploy Serverless Apps This demo was presented at the AWS Summit @ Cape Town on Jul 12th. You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps). -
alexcasalboni renamed this gist
Jul 11, 2018 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # How to Build and Deploy Serverless Apps This demo was given at the AWS Summit @ Cape Town on Jul 12th. You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps). -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # How to Build and Deploy Serverless Apps - AWS Summit @ Cape Town You can find the full deck [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps). -
alexcasalboni revised this gist
Jul 11, 2018 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,8 +34,8 @@ Resources: Type: AWS::CloudWatch::Alarm DependsOn: TestFunction Properties: AlarmName: !Sub alarm-sam-demo-alias-${TestFunction} AlarmDescription: "deployment alarm to check for errors in the function" ComparisonOperator: GreaterThanOrEqualToThreshold Dimensions: - Name: FunctionName @@ -51,8 +51,8 @@ Resources: Type: AWS::CloudWatch::Alarm DependsOn: TestFunction Properties: AlarmName: !Sub alarm-sam-demo-latestversion-${TestFunction} AlarmDescription: "alarm to check for errors in the function" ComparisonOperator: GreaterThanOrEqualToThreshold TreatMissingData: missing Dimensions: -
alexcasalboni revised this gist
Jul 11, 2018 . 2 changed files with 1 addition and 1 deletion.There are no files selected for viewing
File renamed without changes.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/bin/bash # variables TEMPLATE_IN=sam_template.yaml TEMPLATE_OUT=template-packaged.yaml S3_BUCKET=serverless-sam-demos STACK_NAME=ServerlessSAMDemo -
alexcasalboni revised this gist
Jul 11, 2018 . 5 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes. -
alexcasalboni revised this gist
Jul 11, 2018 . 4 changed files with 20 additions and 10 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,20 @@ #!/bin/bash # variables TEMPLATE_IN=template.yaml TEMPLATE_OUT=template-packaged.yaml S3_BUCKET=serverless-sam-demos STACK_NAME=ServerlessSAMDemo REGION=eu-west-1 # package (upload artifact to S3) echo "SAM is now packaging..." sam package --template-file $TEMPLATE_IN --s3-bucket $S3_BUCKET --output-template-file $TEMPLATE_OUT --region $REGION # deploy (CloudFormation changesets) echo "SAM is now deploying..." sam deploy --template-file $TEMPLATE_OUT --stack-name $STACK_NAME --capabilities CAPABILITY_IAM --region $REGION # fetch and show CloudFormation output ENDPOINT=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --region $REGION --query 'Stacks[0].Outputs[?OutputKey==`ENDPOINT`].OutputValue' --output text) echo "ENDPOINT: $ENDPOINT" This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,10 @@ #!/bin/bash # loop forever for ((;;)) do # print a same-line dot echo -e ".\c" # fetch the given URL and append the response to a txt file curl $1 -w " (%{time_total}s) \n" >> data.txt 2>/dev/null done This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,11 @@ #!/bin/bash # count lines KO=$(grep "Internal server error" data.txt | wc -l) OK=$(grep "OK" data.txt | wc -l) # compute percntage PERCKO=$(echo "$KO * 100 / ($KO + $OK)" | bc) PERCOK=$(echo "$OK * 100 / ($KO + $OK)" | bc) # show stats echo "OK requests: $OK ($PERCOK%)" echo "KO requests: $KO ($PERCKO%)" This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,7 @@ #!/bin/bash # empty file echo "" > data.txt # tail and wait for new daata tail -F data.txt -
alexcasalboni created this gist
Jul 11, 2018 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ #!/bin/bash # vars TEMPLATE_IN=app/template.yaml TEMPLATE_OUT=app/template-packaged.yaml S3_BUCKET=serverless-sam-demos STACK_NAME=ServerlessSAMDemo REGION=eu-west-1 # package echo "SAM is now packaging..." sam package --template-file $TEMPLATE_IN --s3-bucket $S3_BUCKET --output-template-file $TEMPLATE_OUT --region $REGION # deploy echo "SAM is now deploying..." sam deploy --template-file $TEMPLATE_OUT --stack-name $STACK_NAME --capabilities CAPABILITY_IAM --region $REGION ENDPOINT=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --region $REGION --query 'Stacks[0].Outputs[?OutputKey==`ENDPOINT`].OutputValue' --output text) echo "ENDPOINT: $ENDPOINT" This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ 'use strict'; const AWS = require('aws-sdk'); const dynamo = new AWS.DynamoDB.DocumentClient(); const tableName = process.env.TABLE_NAME; const createResponse = (statusCode, msg) => { return { statusCode: statusCode, body: JSON.stringify({message: msg}) } }; const dbGet = (params) => { return dynamo.get(params).promise() }; exports.test = (event, context, callback) => { // throw new Error("KO"); let params = { TableName: tableName, Key: { id: "test" } }; dbGet(params).then( (data) => { if (!data.Item) { return callback(null, createResponse(404, "ITEM NOT FOUND")); } callback(null, createResponse(200, data.Item.message)); }).catch( (err) => { console.log(`GET ITEM FAILED WITH ERROR: ${err}`); callback(null, createResponse(500, err)); }); }; This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ #!/bin/bash for ((;;)) do echo -e ".\c" curl $1 -w " (%{time_total}s) \n" >> curl.txt 2>/dev/null done This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ #!/bin/bash KO=$(grep "Internal server error" curl.txt | wc -l) OK=$(grep "OK" curl.txt | wc -l) PERCKO=$(echo "$KO * 100 / ($KO + $OK)" | bc) PERCOK=$(echo "$OK * 100 / ($KO + $OK)" | bc) echo "OK requests: $OK ($PERCOK%)" echo "KO requests: $KO ($PERCKO%)" This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ #!/bin/bash echo "" > curl.txt tail -F curl.txt This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ AWSTemplateFormatVersion: '2010-09-09' Transform: 'AWS::Serverless-2016-10-31' Description: Safe deployments Demo Resources: Table: Type: AWS::Serverless::SimpleTable TestFunction: Type: AWS::Serverless::Function Properties: Handler: index.test Runtime: nodejs6.10 CodeUri: ./ Policies: AmazonDynamoDBReadOnlyAccess Environment: Variables: TABLE_NAME: !Ref Table Events: GetResource: Type: Api Properties: Path: /test Method: get AutoPublishAlias: live DeploymentPreference: Type: Linear10PercentEvery1Minute Alarms: - !Ref AliasErrorMetricGreaterThanZeroAlarm - !Ref LatestVersionErrorMetricGreaterThanZeroAlarm AliasErrorMetricGreaterThanZeroAlarm: Type: AWS::CloudWatch::Alarm DependsOn: TestFunction Properties: AlarmName: !Sub aahelloworld-safemode-alias-${TestFunction} AlarmDescription: "pre-deployment alarm to check for errors in the function" ComparisonOperator: GreaterThanOrEqualToThreshold Dimensions: - Name: FunctionName Value: !Ref TestFunction EvaluationPeriods: 1 MetricName: Errors Namespace: AWS/Lambda Period: 60 Statistic: Sum Threshold: 1 LatestVersionErrorMetricGreaterThanZeroAlarm: Type: AWS::CloudWatch::Alarm DependsOn: TestFunction Properties: AlarmName: !Sub aahelloworld-safemode-latestversion-${TestFunction} AlarmDescription: "pre-deployment alarm to check for errors in the function" ComparisonOperator: GreaterThanOrEqualToThreshold TreatMissingData: missing Dimensions: - Name: FunctionName Value: !Ref TestFunction - Name: Resource Value: !Join [":", [!Ref TestFunction, !Select ["7", !Split [":", !Ref TestFunction.Version]]]] EvaluationPeriods: 1 MetricName: Errors Namespace: AWS/Lambda Period: 60 Statistic: Sum Threshold: 1 Outputs: ENDPOINT: Description: "API endpoint URL for Prod environment" Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/test"