-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.yaml
117 lines (100 loc) · 3.36 KB
/
lambda.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
AWSTemplateFormatVersion: "2010-09-09"
Description: Stack for Pipeline Notification
Parameters:
NotificationName:
Type: String
EnvironmentStage:
Type: String
Description: dev, prod, etc.
Default: dev
SecretsArn:
Type: String
Resources:
CodePipelineAlertLambdaLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: CodePipelineAlertLambda
Properties:
LogGroupName:
Fn::Join:
- '-'
- - CodePipelineAlertLambda
- !Ref EnvironmentStage
- logs
RetentionInDays: 7
CodePipelineAlertLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: PipelineAlertLambdaPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'
CodePipelineAlertLambda:
Type: AWS::Lambda::Function
Properties:
Runtime: nodejs18.x
Handler: index.handler
Code:
ZipFile: |
exports.handler = async function (event) {
try {
const { time, detail } = JSON.parse(event.Records[0].Sns.Message);
const webHookURL = process.env.CODE_PIPELINE_WEBHOOK;
const envStage = process.env.ENVIRONMENT_STAGE;
if (!webHookURL || !envStage) {
throw new Error("Webhook URL or Environment Stage is missing");
}
const utcTime = new Date(time);
const koreaTime = new Date(utcTime.getTime() + (9 * 60 * 60 * 1000));
const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' };
const koreaTimeFormatted = new Intl.DateTimeFormat('ko-KR', options).format(koreaTime);
const content = `⛳️ **AWS CodePipeline Notification-${envStage}**\n\n**Execution Status: ${detail.state}**\n\n**Time:** ${koreaTimeFormatted}`;
const response = await fetch(webHookURL, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ content })
});
if (!response.ok) {
throw new Error(`Webhook request failed with status: ${response.status}`);
}
return "Success";
} catch (e) {
console.error(e);
throw e;
}
};
Role: !GetAtt [ CodePipelineAlertLambdaRole, Arn ]
Environment:
Variables:
ENVIRONMENT_STAGE: !Ref EnvironmentStage
CODE_PIPELINE_WEBHOOK:
Fn::Sub:
- '{{resolve:secretsmanager:${SecretsArn}:SecretString:CODE_PIPELINE_WEBHOOK}}'
- SecretName: !Ref SecretsArn
Outputs:
CodePipelineAlertLambdaArn:
Value: !GetAtt [ CodePipelineAlertLambda, Arn ]
Export:
Name:
Fn::Join:
- '-'
- - !Ref AWS::StackName
- CodePipelineAlertLambda