Skip to content

Commit 0cafa60

Browse files
MrThomasWagnerdavidgf
authored andcommitted
Optional CodeDeploy IAM Roles (#25)
* Allow for a CodeDeploy role to be specified via deploymentSettings configuration, in which case an IamRole for CodeDeploy will be referenced in the CloudFormation config instead of created * Update the readme with the new configuration option and add in an example of the policy used for codedeploy * Move codeDeployRole creation into the 'globalresources' section - i.e. dont create a role for every function * Udpate readme to document codedeploy role * Minor tweaks
1 parent 33bb644 commit 0cafa60

12 files changed

+1248
-40
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ You can see a working example in the [example folder](./example/).
6464
* `postTrafficHook`: (optional) validation Lambda function that runs after traffic shifting. It must use te CodeDeploy SDK to notify about this step's success or failure (more info [here](https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html))
6565
* `alarms`: (optional) list of CloudWatch alarms. If any of them is triggered duringt the deployment, the associated Lambda function will automatically roll back to the previous version.
6666

67+
### Default configurations
68+
69+
You can set default values for all functions in a top-level custom deploymentSettings section. E.g.:
70+
71+
```yaml
72+
custom:
73+
deploymentSettings:
74+
codeDeployRole: some_arn_value
75+
76+
functions:
77+
...
78+
```
79+
80+
Some values are only available as top-level configurations. They are:
81+
82+
* `codeDeployRole`: (optional) an arn specifying an existing IAM role for CodeDeploy. If absent, one will be created for you. See the [codeDeploy policy](./example-code-deploy-policy.json) for an example of what is needed.
83+
6784
## How it works
6885

6986
The plugin relies on the [AWS Lambda traffic shifting feature](https://docs.aws.amazon.com/lambda/latest/dg/lambda-traffic-shifting-using-aliases.html) to balance traffic between versions and [AWS CodeDeploy](https://docs.aws.amazon.com/lambda/latest/dg/automating-updates-to-serverless-apps.html) to automatically update its weight. It modifies the `CloudFormation` template generated by [Serverless](https://github.com/serverless/serverless), so that:

example-code-deploy-policy.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Action": [
6+
"cloudwatch:DescribeAlarms",
7+
"lambda:*",
8+
"sns:Publish"
9+
],
10+
"Resource": "*",
11+
"Effect": "Allow"
12+
},
13+
{
14+
"Action": [
15+
"s3:GetObject",
16+
"s3:GetObjectVersion"
17+
],
18+
"Resource": "arn:aws:s3:::*/CodeDeploy/*",
19+
"Effect": "Allow"
20+
},
21+
{
22+
"Action": [
23+
"s3:GetObject",
24+
"s3:GetObjectVersion"
25+
],
26+
"Resource": "*",
27+
"Condition": {
28+
"StringEquals": {
29+
"s3:ExistingObjectTag/UseWithCodeDeploy": "true"
30+
}
31+
},
32+
"Effect": "Allow"
33+
}
34+
]
35+
}

example/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
"devDependencies": {
1111
"serverless": "^1.26.1",
1212
"serverless-plugin-aws-alerts": "^1.2.4",
13-
"serverless-plugin-canary-deployments": "^0.3.1"
13+
"serverless-plugin-canary-deployments": "^0.4.0"
1414
},
1515
"scripts": {
1616
"test": "echo \"Error: no test specified\" && exit 1",
17-
"package": "npm un --no-save serverless-plugin-canary-deployments && npm pack ../ && npm i --no-save serverless-plugin-canary-deployments-0.3.1.tgz && sls package -s dev",
18-
"deploy": "npm un --no-save serverless-plugin-canary-deployments && npm pack ../ && npm i --no-save serverless-plugin-canary-deployments-0.3.1.tgz && sls deploy -s dev",
17+
"package": "npm un --no-save serverless-plugin-canary-deployments && npm pack ../ && npm i --no-save serverless-plugin-canary-deployments-0.4.0.tgz && sls package -s dev",
18+
"deploy": "npm un --no-save serverless-plugin-canary-deployments && npm pack ../ && npm i --no-save serverless-plugin-canary-deployments-0.4.0.tgz && sls deploy -s dev",
1919
"populate-table": "node ./scripts/populate-test-table"
2020
},
2121
"author": "",

example/serverless.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ plugins:
1313
- serverless-plugin-aws-alerts
1414
- serverless-plugin-canary-deployments
1515

16+
custom:
17+
alerts:
18+
dashboards: false
19+
1620
functions:
1721
hello:
1822
handler: handler.hello
@@ -25,7 +29,7 @@ functions:
2529
- StreamsTestTable
2630
- StreamArn
2731
- sns: snsTopic
28-
- s3: s3samplebucket
32+
# - s3: s3samplebucket
2933
alarms:
3034
- name: foo
3135
namespace: 'AWS/Lambda'

0 commit comments

Comments
 (0)