forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Nginx-access-log-data-archive.yml
510 lines (499 loc) · 14.5 KB
/
Nginx-access-log-data-archive.yml
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: Nginx access log data archive
zh-cn: Nginx访问日志数据归档
Parameters:
CommonName:
Type: String
Default: app01
VSwitchZoneId:
Type: String
Label:
en: Availability zone
zh-cn: 可用区
Description:
en: Available area id, <a href='https://help.aliyun.com/document_detail/123712.html'
target='_blank'><b><font color='blue'>View region and zone info</font></b></a>.
zh-cn: 可用区ID,</font><a href='https://help.aliyun.com/document_detail/123712.html'
target='_blank'><b> 查看可用区信息</b><font color='blue'></a>。
AssociationProperty: ALIYUN::ECS::Instance:ZoneId
EcsInstanceType:
Type: String
Label:
en: Instance type
zh-cn: 实例规格
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: VSwitchZoneId
Default: ecs.g6.large
EcsDiskCategory:
Type: String
Label:
en: System disk type
zh-cn: 系统盘类型
Default: cloud_ssd
AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
AssociationPropertyMetadata:
LocaleKey: DiskCategory
InstanceType: ${EcsInstanceType}
ZoneId: ${VSwitchZoneId}
SlsProjectName:
Type: String
Label:
en: Name of sls project
zh-cn: 日志项目的名称
Description:
en: The name contains 3 to 36 characters. It must start and end with a lowercase letter or number. The value can contain lowercase letters, digits, and hyphens (-).
zh-cn: 长度为3~36个字符。必须以小写英文字母或数字开头和结尾。可包含小写英文字母、数字和短划线(-)。
AssociationProperty: AutoCompleteInput
AssociationPropertyMetadata:
Length: 6
Prefix: sls-project-app01-
CharacterClasses:
- Class: lowercase
min: 1
SlsLogstoreName:
Type: String
Label:
en: Name of sls logstore
zh-cn: 日志库的名称
Description:
en: |-
The name of the log library must be unique within a logging project.
The name contains 3 to 36 characters. It must start and end with a lowercase letter or number. The value can contain lowercase letters, digits, and hyphens (-).
zh-cn: |-
在一个日志项目中,日志库的名称必须具有唯一性。
长度为3~36个字符。必须以小写英文字母或数字开头和结尾。可包含小写英文字母、数字和短划线(-)。
Default: sls-logstore-app01
OssBucketName:
Type: String
Label:
en: Bucket name
zh-cn: 存储空间名称
Description:
en: The name contains 3 to 36 characters. It must start and end with a lowercase letter or number. The value can contain lowercase letters, digits, and hyphens (-).
zh-cn: 长度为3~36个字符。必须以小写英文字母或数字开头和结尾。可包含小写英文字母、数字和短划线(-)。
AssociationProperty: AutoCompleteInput
AssociationPropertyMetadata:
Length: 6
Prefix: bucket-app01-
CharacterClasses:
- Class: lowercase
min: 1
Resources:
EcsVpc:
Type: ALIYUN::ECS::VPC
Properties:
CidrBlock: 192.168.0.0/16
VpcName:
Fn::Sub: vpc_${CommonName}
EcsVSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
ZoneId:
Ref: VSwitchZoneId
VpcId:
Ref: EcsVpc
CidrBlock: 192.168.1.0/24
VSwitchName:
Fn::Sub: vsw_${CommonName}
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
VpcId:
Ref: EcsVpc
SecurityGroupIngress:
- PortRange: 80/80
IpProtocol: tcp
NicType: intranet
SourceCidrIp: 0.0.0.0/0
- PortRange: 22/22
IpProtocol: tcp
NicType: intranet
SourceCidrIp: 0.0.0.0/0
- PortRange: 3306/3306
IpProtocol: tcp
NicType: intranet
SourceCidrIp: 0.0.0.0/0
SecurityGroupName:
Fn::Sub: sg_${CommonName}
SecurityGroupType: normal
EcsInstance:
Type: ALIYUN::ECS::Instance
Properties:
InstanceName:
Fn::Sub: ecs_${CommonName}
InstanceChargeType: PostPaid
InstanceType:
Ref: EcsInstanceType
ImageId: centos_7
SystemDiskCategory:
Ref: EcsDiskCategory
SystemDiskSize: 40
AllocatePublicIP: true
InternetChargeType: PayByTraffic
InternetMaxBandwidthOut: 1
ZoneId:
Ref: VSwitchZoneId
SecurityGroupId:
Ref: EcsSecurityGroup
VpcId:
Ref: EcsVpc
VSwitchId:
Ref: EcsVSwitch
SlsProject:
DependsOn: AutoEnableSLS
Type: ALIYUN::SLS::Project
Properties:
Name:
Ref: SlsProjectName
SlsLogStore:
Type: ALIYUN::SLS::Logstore
Properties:
LogstoreName:
Ref: SlsLogstoreName
ProjectName:
Ref: SlsProjectName
DependsOn:
- SlsProject
SlsIndex:
Type: ALIYUN::SLS::Index
Properties:
ProjectName:
Ref: SlsProject
FullTextIndex:
Enable: true
LogstoreName:
Ref: SlsLogstoreName
DependsOn:
- SlsLogStore
OssBucket:
Type: ALIYUN::OSS::Bucket
DependsOn: AutoEnableOSS
Properties:
BucketName:
Ref: OssBucketName
StorageClass: IA
DeletionForce: true
InstallNginx:
Type: ALIYUN::ECS::RunCommand
Properties:
InstanceIds:
- Ref: EcsInstance
Timeout: 600
Type: RunShellScript
Sync: true
CommandContent: |-
#!/bin/bash
sudo yum update -y
sudo yum install -y nginx
sudo systemctl start nginx
CreateLogs:
Type: ALIYUN::ECS::RunCommand
Properties:
InstanceIds:
- Ref: EcsInstance
Type: RunShellScript
Frequency: '0 * * * * ?'
Timed: true
CommandContent: >-
#!/bin/bash
cur_date=$(date +'%d/%b/%Y')
echo "127.0.0.0 - - [${cur_date}:$(date +'%H:%M:%S %z')] \"GET /
HTTP/1.1\" 200 4897 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36\""
| tee -a /var/log/nginx/access.log
DependsOn:
- InstallNginx
InstallLogAgent:
Type: ALIYUN::OOS::Execution
Properties:
TemplateName: ACS-ECS-BulkyInstallLogAgent
SafetyCheck: Skip
Parameters:
regionId:
Ref: ALIYUN::Region
OOSAssumeRole: ''
targets:
ResourceIds:
- Fn::GetAtt:
- EcsInstance
- InstanceId
RegionId:
Ref: ALIYUN::Region
Type: ResourceIds
rateControl:
Mode: Concurrency
MaxErrors: 100%
Concurrency: 1
action: install
DependsOn:
- InstallNginx
- SlsIndex
SlsMachineGroup:
Type: ALIYUN::SLS::MachineGroup
Properties:
GroupName: app01
ProjectName:
Ref: SlsProject
MachineIdentifyType: ip
MachineList:
- Fn::GetAtt:
- EcsInstance
- PrivateIp
DependsOn:
- InstallLogAgent
SlsLogtailConfig:
Type: ALIYUN::SLS::LogtailConfig
Properties:
ProjectName:
Ref: SlsProject
LogtailConfigName: app01
LogstoreName:
Fn::GetAtt:
- SlsLogStore
- LogstoreName
RawConfigData:
configName: app01
logSample: |-
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$request_time $request_length '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
inputType: file
outputType: LogService
outputDetail:
logstoreName:
Fn::GetAtt:
- SlsLogStore
- LogstoreName
inputDetail:
discardUnmatch: false
maxDepth: 10
advanced:
tail_size_kb: 1024
blacklist: { }
k8s:
ExternalEnvTag: { }
logPath: /var/log/nginx
filePattern: access.log
enableRawLog: false
topicFormat: none
fileEncoding: utf8
adjustTimezone: false
logTimezone: ''
preserve: true
preserveDepth: 1
dockerFile: false
dockerIncludeLabel: { }
dockerExcludeLabel: { }
dockerIncludeEnv: { }
dockerExcludeEnv: { }
logType: common_reg_log
regex: (\S*)\s*-\s*(\S*)\s*\[(\d+/\S+/\d+:\d+:\d+:\d+)\s+\S+\]\s*"(\S+)\s+(\S+)\s+\S+"\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*"([^"]*)"\s*"([^"]*)".*
key:
- remote_addr
- remote_user
- time_local
- request_method
- request_uri
- request_time
- request_length
- status
- body_bytes_sent
- http_referer
- http_user_agent
ApplyConfigToMachineGroup:
Type: ALIYUN::SLS::ApplyConfigToMachineGroup
Properties:
ProjectName:
Ref: SlsProject
ConfigName:
Fn::GetAtt:
- SlsLogtailConfig
- LogtailConfigName
GroupName:
Fn::GetAtt:
- SlsMachineGroup
- GroupName
RoleNameSuffix:
Type: ALIYUN::RandomString
Properties:
sequence: lowercase
DealLogOssExportStack:
Type: ALIYUN::ROS::Stack
Properties:
Parameters:
sls_project_name:
Ref: SlsProjectName
sls_logstore_name:
Ref: SlsLogstoreName
bucket_name:
Ref: OssBucketName
export_name:
Ref: CommonName
aliuid:
Ref: ALIYUN::TenantId
role_name:
Fn::Join:
- '-'
- - 'oss-export'
- Fn::GetAtt:
- RoleNameSuffix
- value
TemplateBody:
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Terraform-v1.2'
Workspace:
main.tf: |-
variable "sls_project_name" {
type = string
}
variable "sls_logstore_name" {
type = string
}
variable "bucket_name" {
type = string
}
variable "export_name" {
type = string
}
variable "aliuid" {
type = string
}
variable "role_name" {
type = string
}
resource "alicloud_ram_role" "log_export_to_oss_role" {
name = var.role_name
document = <<EOF
{
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": [
"${var.aliuid}@log.aliyuncs.com",
"log.aliyuncs.com"
]
}
}
],
"Version": "1"
}
EOF
}
resource "alicloud_ram_role_policy_attachment" "oss_write_attachment" {
policy_name = "AliyunOSSFullAccess"
policy_type = "System"
role_name = var.role_name
depends_on = [
alicloud_ram_role.log_export_to_oss_role
]
}
resource "alicloud_ram_role_policy_attachment" "log_read_attachment" {
policy_name = "AliyunLogReadOnlyAccess"
policy_type = "System"
role_name = var.role_name
depends_on = [
alicloud_ram_role.log_export_to_oss_role
]
}
resource "alicloud_log_oss_export" "example" {
project_name = var.sls_project_name
logstore_name = var.sls_logstore_name
export_name = var.export_name
display_name = var.export_name
bucket = var.bucket_name
prefix = "app01"
suffix = ""
buffer_interval = 300
buffer_size = 250
compress_type = "gzip"
path_format = "%Y/%m/%d/%H/%M"
content_type = "json"
json_enable_tag = true
role_arn = "acs:ram::${var.aliuid}:role/${var.role_name}"
time_zone = "+0800"
depends_on = [
alicloud_ram_role_policy_attachment.log_read_attachment,
alicloud_ram_role_policy_attachment.oss_write_attachment
]
}
DependsOn:
- SlsProject
- SlsLogStore
- OssBucket
- ApplyConfigToMachineGroup
AutoEnableSLS:
Type: ALIYUN::ROS::AutoEnableService
Properties:
ServiceName: SLS
AutoEnableOSS:
Type: ALIYUN::ROS::AutoEnableService
Properties:
ServiceName: OSS
Outputs:
VpcId:
Description: Vpc id.
Value:
Ref: EcsVpc
VSwitchId:
Description: VSwitch id.
Value:
Ref: EcsVSwitch
SecurityGroupId:
Description: SecurityGroup id.
Value:
Ref: EcsSecurityGroup
EcsInstanceId:
Description: The id of the ecs instance.
Value:
Fn::GetAtt:
- EcsInstance
- InstanceId
ProjectName:
Description: The name of SLS project.
Value:
Fn::GetAtt:
- SlsProject
- Name
RoleName:
Description: The name of RAM role.
Value:
Fn::Join:
- '-'
- - 'oss-export'
- Fn::GetAtt:
- RoleNameSuffix
- value
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- VSwitchZoneId
- EcsInstanceType
- EcsImageId
- EcsDiskCategory
Label:
default:
en: ECS configure
zh-cn: 云服务器ECS配置
- Parameters:
- SlsProjectName
- SlsLogstoreName
Label:
default:
en: SLS
zh-cn: 日志服务SLS配置
- Parameters:
- OssBucketName
Label:
default:
en: OSS bucket
zh-cn: 对象存储OSS配置
Hidden:
- CommonName
TemplateTags:
- acs:technical-solution:internet-application-development:应用日志数据归档-tech_solu_08