-
Notifications
You must be signed in to change notification settings - Fork 48
/
safe_deploy.go
161 lines (150 loc) · 5.83 KB
/
safe_deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package decorator
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
sparta "github.com/mweagle/Sparta"
gocf "github.com/mweagle/go-cloudformation"
"github.com/sirupsen/logrus"
)
// codeDeployLambdaUpdateDecorator is the per-function decorator
// that adds the necessary information for CodeDeploy
func codeDeployLambdaUpdateDecorator(updateType string,
codeDeployApplicationName string,
codeDeployRoleName string) sparta.TemplateDecorator {
return func(serviceName string,
lambdaResourceName string,
lambdaResource gocf.LambdaFunction,
resourceMetadata map[string]interface{},
S3Bucket string,
S3Key string,
buildID string,
template *gocf.Template,
context map[string]interface{},
logger *logrus.Logger) error {
safeDeployResourceName := func(resType string) string {
return sparta.CloudFormationResourceName(serviceName,
lambdaResourceName,
resType)
}
// Create the AWS::Lambda::Version, with DeletionPolicy=Retain
versionResourceName := safeDeployResourceName("version" + buildID)
versionResource := &gocf.LambdaVersion{
FunctionName: gocf.Ref(lambdaResourceName).String(),
}
entry := template.AddResource(versionResourceName, versionResource)
entry.DeletionPolicy = "Retain"
// Create the AWS::CodeDeploy::DeploymentGroup entry that includes a reference
// to the IAM role
codeDeploymentGroupResourceName := safeDeployResourceName("deploymentGroup")
codeDeploymentGroup := &gocf.CodeDeployDeploymentGroup{
ApplicationName: gocf.Ref(codeDeployApplicationName).String(),
AutoRollbackConfiguration: &gocf.CodeDeployDeploymentGroupAutoRollbackConfiguration{
Enabled: gocf.Bool(true),
Events: gocf.StringList(gocf.String("DEPLOYMENT_FAILURE"),
gocf.String("DEPLOYMENT_STOP_ON_ALARM"),
gocf.String("DEPLOYMENT_STOP_ON_REQUEST")),
},
ServiceRoleArn: gocf.GetAtt(codeDeployRoleName, "Arn"),
DeploymentConfigName: gocf.String(fmt.Sprintf("CodeDeployDefault.Lambda%s", updateType)),
DeploymentStyle: &gocf.CodeDeployDeploymentGroupDeploymentStyle{
DeploymentType: gocf.String("BLUE_GREEN"),
DeploymentOption: gocf.String("WITH_TRAFFIC_CONTROL"),
},
}
template.AddResource(codeDeploymentGroupResourceName, codeDeploymentGroup)
// Create the Alias entry...
aliasResourceName := safeDeployResourceName("alias")
aliasResource := &gocf.LambdaAlias{
FunctionVersion: gocf.GetAtt(versionResourceName, "Version").String(),
FunctionName: gocf.Ref(lambdaResourceName).String(),
Name: gocf.String("live"),
}
aliasEntry := template.AddResource(aliasResourceName, aliasResource)
aliasEntry.UpdatePolicy = &gocf.UpdatePolicy{
CodeDeployLambdaAliasUpdate: &gocf.UpdatePolicyCodeDeployLambdaAliasUpdate{
ApplicationName: gocf.Ref(codeDeployApplicationName).String(),
DeploymentGroupName: gocf.Ref(codeDeploymentGroupResourceName).String(),
},
}
return nil
}
}
// CodeDeployServiceUpdateDecorator is a service level decorator that attaches
// the CodeDeploy safe update to an upgrade operation.
// Ref: https://github.com/awslabs/serverless-application-model/blob/master/docs/safe_lambda_deployments.rst
//
func CodeDeployServiceUpdateDecorator(updateType string,
lambdaFuncs []*sparta.LambdaAWSInfo,
preHook *sparta.LambdaAWSInfo,
postHook *sparta.LambdaAWSInfo) sparta.ServiceDecoratorHookFunc {
// Define the names that are shared
codeDeployApplicationName := sparta.CloudFormationResourceName("SafeDeploy",
"deployment",
"application")
codeDeployRoleResourceName := sparta.CloudFormationResourceName("SafeDeploy",
"deployment",
"role")
// Add the Execution status
// See: https://github.com/awslabs/serverless-application-model/blob/master/docs/safe_lambda_deployments.rst#traffic-shifting-using-codedeploy
for _, eachFunc := range []*sparta.LambdaAWSInfo{preHook, postHook} {
if eachFunc != nil {
eachFunc.RoleDefinition.Privileges = append(preHook.RoleDefinition.Privileges,
sparta.IAMRolePrivilege{
Actions: []string{"codedeploy:PutLifecycleEventHookExecutionStatus"},
Resource: gocf.Join("",
gocf.String("arn:aws:codedeploy:"),
gocf.Ref("AWS::Region"),
gocf.String(":"),
gocf.Ref("AWS::AccountId"),
gocf.String(":deploymentgroup:"),
gocf.String(codeDeployApplicationName),
gocf.String("/*"),
)},
)
}
}
// Add the decorator to each lambda
for _, eachLambda := range lambdaFuncs {
safeDeployDecorator := codeDeployLambdaUpdateDecorator(updateType,
codeDeployApplicationName,
codeDeployRoleResourceName)
eachLambda.Decorators = append(eachLambda.Decorators,
sparta.TemplateDecoratorHookFunc(safeDeployDecorator))
}
// Return the service decorator...
return func(context map[string]interface{},
serviceName string,
template *gocf.Template,
S3Bucket string,
S3Key string,
buildID string,
awsSession *session.Session,
noop bool,
logger *logrus.Logger) error {
// So what we really need to do is walk over all the lambda functions in the template
// and setup all the Deployment groups...
codeDeployApplication := &gocf.CodeDeployApplication{
ComputePlatform: gocf.String("Lambda"),
}
template.AddResource(codeDeployApplicationName, codeDeployApplication)
// Create the CodeDeploy role
// Ensure there is an IAM role for this...
// CodeDeployServiceRole
codeDeployRoleResource := gocf.IAMRole{
ManagedPolicyArns: gocf.StringList(gocf.String("arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda")),
AssumeRolePolicyDocument: sparta.ArbitraryJSONObject{
"Version": "2012-10-17",
"Statement": []sparta.ArbitraryJSONObject{{
"Action": []string{"sts:AssumeRole"},
"Effect": "Allow",
"Principal": sparta.ArbitraryJSONObject{
"Service": []string{"codedeploy.amazonaws.com"},
}},
},
},
}
template.AddResource(codeDeployRoleResourceName, codeDeployRoleResource)
// Ship it...
return nil
}
}