forked from mbj/stratosphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-copy.hs
137 lines (117 loc) · 3.26 KB
/
s3-copy.hs
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
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Lens
import Data.Aeson (Value (Array), object)
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Text (Text)
import Stratosphere
main :: IO ()
main = B.putStrLn $ encodeTemplate myTemplate
myTemplate :: Template
myTemplate =
template
[ role'
, lambda
, permission
, incomingS3Bucket
, outgoingS3Bucket
]
& templateDescription ?~ "Simple event triggered S3 bucket to bucket copy example"
& templateFormatVersion ?~ "2010-09-09"
lambda :: Resource
lambda = (
resource "CopyS3ObjectLambda" $
lambdaFunction
lambdaCode
"index.handler"
(GetAtt "IAMRole" "Arn")
(Literal NodeJS12x)
& lfFunctionName ?~ "copyS3Object"
)
& resourceDependsOn ?~ [ role' ^. resourceName ]
where
lambdaCode :: LambdaFunctionCode
lambdaCode = lambdaFunctionCode
& lfcZipFile ?~ code
code :: Val Text
code = "\
\ var AWS = require('aws-sdk'); \
\ var s3 = new AWS.S3({apiVersion: '2006-03-01'}); \
\ exports.handler = function(event, context, callback) { \
\ console.log(JSON.stringify(event)); \
\ var rec = event.Records[0]; \
\ var bucket = rec.s3.bucket.name; \
\ var key = rec.s3.object.key; \
\ s3.copyObject({Bucket: \"stratosphere-s3-copy-outgoing\", Key: key, CopySource: \"stratosphere-s3-copy-incoming/\"+key}, function(err){ \
\ callback(null, \"copied s3 object\"); \
\ }); \
\ } \
\ "
role' :: Resource
role' =
resource "IAMRole" $
iamRole
rolePolicyDocumentObject
& iamrPolicies ?~ [ executePolicy ]
& iamrRoleName ?~ "MyLambdaBasicExecutionRole"
& iamrPath ?~ "/"
where
executePolicy =
iamRolePolicy
[ ("Version", "2012-10-17")
, ("Statement", statement)
]
"MyLambdaExecutionPolicy"
where
statement = object
[ ("Effect", "Allow")
, ("Action", actions)
, ("Resource", "*")
]
actions = Array
[ "logs:CreateLogGroup"
, "logs:CreateLogStream"
, "logs:PutLogEvents"
, "s3:PutObject"
, "s3:GetObject"
]
rolePolicyDocumentObject =
[ ("Version", "2012-10-17")
, ("Statement", statement)
]
where
statement = object
[ ("Effect", "Allow")
, ("Principal", principal)
, ("Action", "sts:AssumeRole")
]
principal = object
[ ("Service", "lambda.amazonaws.com") ]
incomingS3Bucket :: Resource
incomingS3Bucket = (
resource "IncomingBucket" $
s3Bucket
& sbBucketName ?~ "stratosphere-s3-copy-incoming"
& sbNotificationConfiguration ?~ config
)
& resourceDependsOn ?~ [ lambda ^. resourceName ]
where
config =
s3BucketNotificationConfiguration
& sbncLambdaConfigurations ?~ [lambdaConfig]
lambdaConfig =
s3BucketLambdaConfiguration
"s3:ObjectCreated:*"
(GetAtt "CopyS3ObjectLambda" "Arn")
outgoingS3Bucket :: Resource
outgoingS3Bucket =
resource "OutgoingBucket" $
s3Bucket
& sbBucketName ?~ "stratosphere-s3-copy-outgoing"
permission :: Resource
permission = resource "LambdaPermission" $
lambdaPermission
"lambda:*"
(GetAtt "CopyS3ObjectLambda" "Arn")
"s3.amazonaws.com"