Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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).
    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?

  2. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sam-automatic-rollback-demo.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # How to Build and Deploy Serverless Apps
    # How to Build and Deploy Serverless Apps [AWS Summit]

    This demo was presented at the AWS Summit @ Cape Town on Jul 12th.

  3. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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 :)

    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 :)
  4. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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
    * *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 :)
  5. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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 full deck [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps).
    You can find the slides [here](https://speakerdeck.com/alexcasalboni/how-to-build-and-deploy-serverless-apps).

    ## What's included in this Gist?

  6. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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` CloudFormation Output
    * *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
  7. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions aws-sam-automatic-rollback-demo.md
    Original 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.
    ** test
    * *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
  8. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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
    ** test
  9. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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
    * * test
  10. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions aws-sam-automatic-rollback-demo.md
    Original 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 does the code include?
    ## 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
    * *scripts*: four scripts used to showcase the live deployment rollback.
    ** test
  11. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion aws-sam-automatic-rollback-demo.md
    Original 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).
    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
  12. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sam-automatic-rollback-demo.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # How to Build and Deploy Serverless Apps

    This demo was given at the AWS Summit @ Cape Town on Jul 12th.
    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).
  13. @alexcasalboni alexcasalboni renamed this gist Jul 11, 2018. 1 changed file with 3 additions and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # How to Build and Deploy Serverless Apps - AWS Summit @ Cape Town
    # 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).
  14. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions aws-lambda-aws-sam-automatic-rollback-demo.md
    Original 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).
  15. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions sam_template.yaml
    Original file line number Diff line number Diff line change
    @@ -34,8 +34,8 @@ Resources:
    Type: AWS::CloudWatch::Alarm
    DependsOn: TestFunction
    Properties:
    AlarmName: !Sub aahelloworld-safemode-alias-${TestFunction}
    AlarmDescription: "pre-deployment alarm to check for errors in the function"
    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 aahelloworld-safemode-latestversion-${TestFunction}
    AlarmDescription: "pre-deployment alarm to check for errors in the function"
    AlarmName: !Sub alarm-sam-demo-latestversion-${TestFunction}
    AlarmDescription: "alarm to check for errors in the function"
    ComparisonOperator: GreaterThanOrEqualToThreshold
    TreatMissingData: missing
    Dimensions:
  16. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion script_deploy.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/bash

    # variables
    TEMPLATE_IN=template.yaml
    TEMPLATE_IN=sam_template.yaml
    TEMPLATE_OUT=template-packaged.yaml
    S3_BUCKET=serverless-sam-demos
    STACK_NAME=ServerlessSAMDemo
  17. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 5 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  18. @alexcasalboni alexcasalboni revised this gist Jul 11, 2018. 4 changed files with 20 additions and 10 deletions.
    11 changes: 6 additions & 5 deletions deploy.sh
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,20 @@
    #!/bin/bash

    # vars
    TEMPLATE_IN=app/template.yaml
    TEMPLATE_OUT=app/template-packaged.yaml
    # variables
    TEMPLATE_IN=template.yaml
    TEMPLATE_OUT=template-packaged.yaml
    S3_BUCKET=serverless-sam-demos
    STACK_NAME=ServerlessSAMDemo
    REGION=eu-west-1

    # package
    # 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
    # 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"
    5 changes: 4 additions & 1 deletion poll.sh
    Original 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"
    curl $1 -w " (%{time_total}s) \n" >> curl.txt 2>/dev/null
    # 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
    7 changes: 5 additions & 2 deletions stats.sh
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,11 @@
    #!/bin/bash

    KO=$(grep "Internal server error" curl.txt | wc -l)
    OK=$(grep "OK" curl.txt | wc -l)
    # 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%)"
    7 changes: 5 additions & 2 deletions tail.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    #!/bin/bash

    echo "" > curl.txt
    tail -F curl.txt
    # empty file
    echo "" > data.txt

    # tail and wait for new daata
    tail -F data.txt
  19. @alexcasalboni alexcasalboni created this gist Jul 11, 2018.
    19 changes: 19 additions & 0 deletions deploy.sh
    Original 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"
    37 changes: 37 additions & 0 deletions index.js
    Original 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));
    });
    };
    7 changes: 7 additions & 0 deletions poll.sh
    Original 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
    8 changes: 8 additions & 0 deletions stats.sh
    Original 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%)"
    4 changes: 4 additions & 0 deletions tail.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #!/bin/bash

    echo "" > curl.txt
    tail -F curl.txt
    73 changes: 73 additions & 0 deletions template.yaml
    Original 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"