Here's a little script which can be used to programatically associate Lambda@Edge functions with CloudFront.
It's input is a JSON configuration file (cf-associations.json
) that looks like this:
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
|
[
{
"distributionId":"ASDFASDFASDF",
"DefaultCacheBehavior":
{
"Quantity": 2,
"Items": [
{
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:12345:function:yourfunctionname:",
"EventType": "origin-response"
},
{
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:12345:function:baisc-auth:",
"EventType": "viewer-request"
}
]
},
"CacheBehaviors": [
{
"path": "/somepathcsp*",
"rules":
{
"Quantity": 1,
"Items": [
{
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:12345:function:anotherfunction:",
"EventType": "origin-response"
}
]
}
},
{
"path": "/someotherpath*",
"rules":
{
"Quantity": 0
}
}
]
}
]
|
The rules are a 1 to 1 mapping to the JSON structure returned by the aws cloudfront get-distribution-config
command, with the only difference being that the ARN does not include the version number.
The script parses the config, grabs the current distribution configuration, publishes a new Lambda version (if necessary), and associates the configured behaviors to the right Lambda functions.