UNPKG

serverless-blue-green-deployments

Version:

A Serverless plugin to implement blue/green deployment of Lambda functions

644 lines (643 loc) 17.5 kB
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "The AWS CloudFormation template for this Serverless application", "Resources": { "ServerlessDeploymentBucket": { "Type": "AWS::S3::Bucket" }, "HelloLogGroup": { "Type": "AWS::Logs::LogGroup", "Properties": { "LogGroupName": "/aws/lambda/canary-deployments-test-dev-hello" } }, "PreHookLogGroup": { "Type": "AWS::Logs::LogGroup", "Properties": { "LogGroupName": "/aws/lambda/canary-deployments-test-dev-preHook" } }, "PostHookLogGroup": { "Type": "AWS::Logs::LogGroup", "Properties": { "LogGroupName": "/aws/lambda/canary-deployments-test-dev-postHook" } }, "IamRoleLambdaExecution": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "Policies": [ { "PolicyName": { "Fn::Join": [ "-", [ "dev", "canary-deployments-test", "lambda" ] ] }, "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogStream" ], "Resource": [ { "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/canary-deployments-test-dev-hello:*" }, { "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/canary-deployments-test-dev-preHook:*" }, { "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/canary-deployments-test-dev-postHook:*" } ] }, { "Effect": "Allow", "Action": [ "logs:PutLogEvents" ], "Resource": [ { "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/canary-deployments-test-dev-hello:*:*" }, { "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/canary-deployments-test-dev-preHook:*:*" }, { "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/canary-deployments-test-dev-postHook:*:*" } ] }, { "Effect": "Allow", "Action": [ "codedeploy:*" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:DescribeStream", "dynamodb:ListStreams" ], "Resource": [ { "Fn::GetAtt": [ "StreamsTestTable", "StreamArn" ] } ] } ] } } ], "Path": "/", "RoleName": { "Fn::Join": [ "-", [ "canary-deployments-test", "dev", "us-east-1", "lambdaRole" ] ] } } }, "HelloLambdaFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "S3Bucket": { "Ref": "ServerlessDeploymentBucket" }, "S3Key": "serverless/canary-deployments-test/dev/1520191533287-2018-03-04T19:25:33.287Z/canary-deployments-test.zip" }, "FunctionName": "canary-deployments-test-dev-hello", "Handler": "handler.hello", "MemorySize": 1024, "Role": { "Fn::GetAtt": [ "IamRoleLambdaExecution", "Arn" ] }, "Runtime": "nodejs6.10", "Timeout": 6 }, "DependsOn": [ "HelloLogGroup", "IamRoleLambdaExecution" ] }, "HelloLambdaVersionFYAirphUvjV7H12yGxU1eQrqAiSBMjAi9hdLPgV62L8": { "Type": "AWS::Lambda::Version", "DeletionPolicy": "Retain", "Properties": { "FunctionName": { "Ref": "HelloLambdaFunction" }, "CodeSha256": "sZvdDgxnAbKe1yaQga0XJPD82+o5jFWz+J3lR+q9UHU=" } }, "PreHookLambdaFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "S3Bucket": { "Ref": "ServerlessDeploymentBucket" }, "S3Key": "serverless/canary-deployments-test/dev/1520191533287-2018-03-04T19:25:33.287Z/canary-deployments-test.zip" }, "FunctionName": "canary-deployments-test-dev-preHook", "Handler": "hooks.pre", "MemorySize": 1024, "Role": { "Fn::GetAtt": [ "IamRoleLambdaExecution", "Arn" ] }, "Runtime": "nodejs6.10", "Timeout": 6 }, "DependsOn": [ "PreHookLogGroup", "IamRoleLambdaExecution" ] }, "PreHookLambdaVersionIYyrXlfQM5jjU68REvnAzRxhgq9eoLqSsDjy0": { "Type": "AWS::Lambda::Version", "DeletionPolicy": "Retain", "Properties": { "FunctionName": { "Ref": "PreHookLambdaFunction" }, "CodeSha256": "sZvdDgxnAbKe1yaQga0XJPD82+o5jFWz+J3lR+q9UHU=" } }, "PostHookLambdaFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "S3Bucket": { "Ref": "ServerlessDeploymentBucket" }, "S3Key": "serverless/canary-deployments-test/dev/1520191533287-2018-03-04T19:25:33.287Z/canary-deployments-test.zip" }, "FunctionName": "canary-deployments-test-dev-postHook", "Handler": "hooks.post", "MemorySize": 1024, "Role": { "Fn::GetAtt": [ "IamRoleLambdaExecution", "Arn" ] }, "Runtime": "nodejs6.10", "Timeout": 6 }, "DependsOn": [ "PostHookLogGroup", "IamRoleLambdaExecution" ] }, "PostHookLambdaVersiondh0VUUAh9BrmvORqx3vDEIcHxolKWKCO1YL45mVTbg": { "Type": "AWS::Lambda::Version", "DeletionPolicy": "Retain", "Properties": { "FunctionName": { "Ref": "PostHookLambdaFunction" }, "CodeSha256": "sZvdDgxnAbKe1yaQga0XJPD82+o5jFWz+J3lR+q9UHU=" } }, "ApiGatewayRestApi": { "Type": "AWS::ApiGateway::RestApi", "Properties": { "Name": "dev-canary-deployments-test", "EndpointConfiguration": { "Types": [ "EDGE" ] } } }, "ApiGatewayResourceHello": { "Type": "AWS::ApiGateway::Resource", "Properties": { "ParentId": { "Fn::GetAtt": [ "ApiGatewayRestApi", "RootResourceId" ] }, "PathPart": "hello", "RestApiId": { "Ref": "ApiGatewayRestApi" } } }, "ApiGatewayMethodHelloGet": { "Type": "AWS::ApiGateway::Method", "Properties": { "HttpMethod": "GET", "RequestParameters": {}, "ResourceId": { "Ref": "ApiGatewayResourceHello" }, "RestApiId": { "Ref": "ApiGatewayRestApi" }, "ApiKeyRequired": false, "AuthorizationType": "NONE", "Integration": { "IntegrationHttpMethod": "POST", "Type": "AWS_PROXY", "Uri": { "Fn::Join": [ "", [ "arn:aws:apigateway:", { "Ref": "AWS::Region" }, ":lambda:path/2015-03-31/functions/", { "Ref": "LiveAlias" }, "/invocations" ] ] } }, "MethodResponses": [] } }, "ApiGatewayDeployment12345": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ApiGatewayRestApi" }, "StageName": "dev" }, "DependsOn": [ "ApiGatewayMethodHelloGet" ] }, "HelloLambdaPermissionApiGateway": { "Type": "AWS::Lambda::Permission", "Properties": { "FunctionName": { "Ref": "LiveAlias" }, "Action": "lambda:InvokeFunction", "Principal": "apigateway.amazonaws.com", "SourceArn": { "Fn::Join": [ "", [ "arn:aws:execute-api:", { "Ref": "AWS::Region" }, ":", { "Ref": "AWS::AccountId" }, ":", { "Ref": "ApiGatewayRestApi" }, "/*/*" ] ] } } }, "SNSTopicSnsTopic": { "Type": "AWS::SNS::Topic", "Properties": { "TopicName": "snsTopic", "DisplayName": "", "Subscription": [ { "Endpoint": { "Ref": "LiveAlias" }, "Protocol": "lambda" } ] } }, "HelloLambdaPermissionSnsTopicSNS": { "Type": "AWS::Lambda::Permission", "Properties": { "FunctionName": { "Ref": "LiveAlias" }, "Action": "lambda:InvokeFunction", "Principal": "sns.amazonaws.com", "SourceArn": { "Fn::Join": [ "", [ "arn:aws:sns:", { "Ref": "AWS::Region" }, ":", { "Ref": "AWS::AccountId" }, ":", "snsTopic" ] ] } } }, "S3BucketS3SampleBucket": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": "s3SampleBucket", "NotificationConfiguration": { "LambdaConfigurations": [ { "Event": "s3:ObjectCreated:*", "Function": { "Ref": "LiveAlias" } } ] } }, "DependsOn": [ "HelloLambdaPermissionS3SampleBucketS3" ] }, "HelloLambdaPermissionS3SampleBucketS3": { "Type": "AWS::Lambda::Permission", "Properties": { "FunctionName": { "Ref": "LiveAlias" }, "Action": "lambda:InvokeFunction", "Principal": "s3.amazonaws.com", "SourceArn": { "Fn::Join": [ "", [ "arn:aws:s3:::s3SampleBucket" ] ] } } }, "HelloEventSourceMappingDynamodbStreamsTestTable": { "Type": "AWS::Lambda::EventSourceMapping", "DependsOn": "IamRoleLambdaExecution", "Properties": { "BatchSize": 10, "EventSourceArn": { "Fn::GetAtt": [ "StreamsTestTable", "StreamArn" ] }, "FunctionName": { "Ref": "LiveAlias" }, "StartingPosition": "TRIM_HORIZON", "Enabled": "True" } }, "HelloFooAlarm": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "Namespace": "AWS/Lambda", "MetricName": "Errors", "Threshold": 1, "Period": 60, "EvaluationPeriods": 1, "ComparisonOperator": "GreaterThanOrEqualToThreshold", "OKActions": [], "AlarmActions": [], "InsufficientDataActions": [], "Dimensions": [ { "Name": "FunctionName", "Value": { "Ref": "HelloLambdaFunction" } } ], "TreatMissingData": "missing", "Statistic": "Minimum" } }, "StreamsTestTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "TableName": "StreamsTestTable", "AttributeDefinitions": [ { "AttributeName": "id", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }, "StreamSpecification": { "StreamViewType": "NEW_AND_OLD_IMAGES" } } }, "BluegreendeploymentstestdevDeploymentApplication": { "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" } }, "CodeDeployServiceRole": { "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda", "arn:aws:iam::aws:policy/AWSLambda_FullAccess" ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" ], "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" ] } } ] } } }, "HelloLambdaFunctionDeploymentGroup": { "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "BluegreendeploymentstestdevDeploymentApplication" }, "AutoRollbackConfiguration": { "Enabled": true, "Events": [ "DEPLOYMENT_FAILURE", "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] }, "ServiceRoleArn": { "Fn::GetAtt": [ "CodeDeployServiceRole", "Arn" ] }, "DeploymentConfigName": { "Fn::Sub": [ "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery1Minute" } ] }, "DeploymentStyle": { "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" }, "AlarmConfiguration": { "Alarms": [ { "Name": { "Ref": "HelloFooAlarm" } } ], "Enabled": true } } }, "LiveAlias": { "Type": "AWS::Lambda::Alias", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ "HelloLambdaVersionFYAirphUvjV7H12yGxU1eQrqAiSBMjAi9hdLPgV62L8", "Version" ] }, "FunctionName": { "Ref": "HelloLambdaFunction" }, "Name": "Live" }, "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "BluegreendeploymentstestdevDeploymentApplication" }, "AfterAllowTrafficHook": { "Ref": "PostHookLambdaFunction" }, "BeforeAllowTrafficHook": { "Ref": "PreHookLambdaFunction" }, "DeploymentGroupName": { "Ref": "HelloLambdaFunctionDeploymentGroup" } } } } }, "Outputs": { "ServerlessDeploymentBucketName": { "Value": { "Ref": "ServerlessDeploymentBucket" } }, "HelloLambdaFunctionQualifiedArn": { "Description": "Current Lambda function version", "Value": { "Ref": "HelloLambdaVersionFYAirphUvjV7H12yGxU1eQrqAiSBMjAi9hdLPgV62L8" } }, "PreHookLambdaFunctionQualifiedArn": { "Description": "Current Lambda function version", "Value": { "Ref": "PreHookLambdaVersionIYyrXlfQM5jjU68REvnAzRxhgq9eoLqSsDjy0" } }, "PostHookLambdaFunctionQualifiedArn": { "Description": "Current Lambda function version", "Value": { "Ref": "PostHookLambdaVersiondh0VUUAh9BrmvORqx3vDEIcHxolKWKCO1YL45mVTbg" } }, "ServiceEndpoint": { "Description": "URL of the service endpoint", "Value": { "Fn::Join": [ "", [ "https://", { "Ref": "ApiGatewayRestApi" }, ".execute-api.us-east-1.amazonaws.com/dev" ] ] } } } }