-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment copy.yaml
151 lines (138 loc) · 5.21 KB
/
deployment copy.yaml
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
AWSTemplateFormatVersion: 2010-09-09
Description: "CI/CD pipeline to deploy to aws cloudformation stack from github hook"
Parameters:
DeploymentBucket:
Type: String
Description: Enter the name of the S3 bucket where your deployment packages are.
DeploymentPackageKey:
Type: String
Description: Enter the name of the S3 key of your deployment package file is.
S3CSVBucketName:
Type: String
Description: S3 bucket that's used for the CSV to land
Resources:
Team4lambdaFunctionIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: Team4CFCICDRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "s3:GetBucketNotification"
- "s3:PutBucketNotification"
Resource: !Sub "arn:aws:s3:::${S3CSVBucketName}"
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
- Effect: Allow
Action:
- iam:GetRole
- iam:PassRole
Resource: arn:aws:iam::account-id:role/EC2-roles-for-XYZ-*
Team4lambdaFunction:
Type: AWS::Lambda::Function
Properties:
Description: "Lambda to handle the extract transform and load"
FunctionName: team4-lambda-cf
Handler: lambda.handler
Layers:
- arn:aws:lambda:eu-west-1:336392948345:layer:AWSDataWrangler-Python39:5
- arn:aws:lambda:eu-west-1:506555054152:layer:psycopg2:8
MemorySize: 128
Role: !GetAtt Team4lambdaFunctionIamRole.Arn
Runtime: python3.8
Timeout: 50
Code:
S3Bucket: !Ref DeploymentBucket
S3Key: !Ref DeploymentPackageKey
LambdaInvokePermission:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName: !GetAtt Team4lambdaFunction.Arn
Action: "lambda:InvokeFunction"
Principal: s3.amazonaws.com
SourceAccount: !Ref "AWS::AccountId"
SourceArn: !Sub "arn:aws:s3:::${S3CSVBucketName}"
Team4NotificationFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: index.lambda_handler
Role: !GetAtt Team4lambdaFunctionIamRole.Arn
Code:
ZipFile: |
from __future__ import print_function
import json
import boto3
import cfnresponse
SUCCESS = "SUCCESS"
FAILED = "FAILED"
print('Loading function')
s3 = boto3.resource('s3')
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
responseData={}
try:
if event['RequestType'] == 'Delete':
print("Request Type:",event['RequestType'])
Bucket=event['ResourceProperties']['Bucket']
delete_notification(Bucket)
print("Sending response to custom resource after Delete")
elif event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
print("Request Type:",event['RequestType'])
LambdaArn=event['ResourceProperties']['LambdaArn']
Bucket=event['ResourceProperties']['Bucket']
add_notification(LambdaArn, Bucket)
responseData={'Bucket':Bucket}
print("Sending response to custom resource")
responseStatus = 'SUCCESS'
except Exception as e:
print('Failed to process:', e)
responseStatus = 'FAILED'
responseData = {'Failure': 'Something bad happened.'}
cfnresponse.send(event, context, responseStatus, responseData)
def add_notification(LambdaArn, Bucket):
bucket_notification = s3.BucketNotification(Bucket)
response = bucket_notification.put(
NotificationConfiguration={
'LambdaFunctionConfigurations': [
{
'LambdaFunctionArn': LambdaArn,
'Events': [
's3:ObjectCreated:*'
]
}
]
}
)
print("Put request completed....")
def delete_notification(Bucket):
bucket_notification = s3.BucketNotification(Bucket)
response = bucket_notification.put(
NotificationConfiguration={}
)
print("Delete request completed....")
Runtime: python3.8
Timeout: 50
LambdaTrigger:
Type: "Custom::LambdaTrigger"
DeletionPolicy : Retain
DependsOn: LambdaInvokePermission
Properties:
ServiceToken: !GetAtt Team4NotificationFunction.Arn
LambdaArn: !GetAtt Team4lambdaFunction.Arn
Bucket: !Ref S3CSVBucketName