-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
224 lines (208 loc) · 7.28 KB
/
template.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
# This is the SAM template that represents the architecture of your serverless application
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWSTemplateFormatVersion: 2010-09-09
Description: >-
Beginning AWS Lambda: An exercise in learning basics of Lambda.
Transform:
- AWS::Serverless-2016-10-31
Resources:
awsDocsRepo:
Type: AWS::S3::Bucket
Properties:
BucketName:
Ref: awsDocsRepoBucketName
AccessControl: Private
getAwsDocsRepo:
Type: AWS::Serverless::Function
Properties:
FunctionName: getAwsDocsRepo
Handler: src/get-awsdocs-repo.handler
Description: Fetches AWS Docs repo from GitHub and uploads it to S3.
Environment:
Variables:
AWS_DOCS_REPO_BUCKET:
Ref: awsDocsRepoBucketName
Policies:
- AWSLambdaBasicExecutionRole
- AmazonS3FullAccess
unzipRepoArchive:
Type: AWS::Serverless::Function
Properties:
FunctionName: unzipRepoArchive
Handler: src/unzip-repo-archive.handler
Description: Reads AWS Docs repo from S3 and extracts it to EFS.
ReservedConcurrentExecutions: 1
Environment:
Variables:
EFS_PATH: "/mnt/efs"
Events:
awsDocsRepoUpdate:
Type: S3
Properties:
Bucket: !Ref awsDocsRepo
Events:
- s3:ObjectCreated:Put
- s3:ObjectCreated:Post
Filter:
S3Key:
Rules:
- Name: prefix
Value: aws-cloudformation-user-guide
- Name: suffix
Value: zip
EventInvokeConfig:
MaximumEventAgeInSeconds: 60
MaximumRetryAttempts: 2
DestinationConfig:
OnSuccess:
Type: SNS
Destination: !Ref unzipRepoArchiveSuccessfulInvocations
Policies:
- AWSLambdaBasicExecutionRole
- AmazonS3ReadOnlyAccess
- AWSLambdaVPCAccessExecutionRole
- AmazonElasticFileSystemClientFullAccess
VpcConfig:
SecurityGroupIds:
- Fn::ImportValue: !Sub "${lambdaEfsStack}-secgroup-id"
SubnetIds:
- Fn::ImportValue: !Sub "${lambdaEfsStack}-subnet-id"
FileSystemConfigs:
- Arn:
Fn::ImportValue: !Sub "${lambdaEfsStack}-access-pt-arn"
LocalMountPath: /mnt/efs
unzipRepoArchiveSuccessfulInvocations:
Type: "AWS::SNS::Topic"
Properties:
DisplayName: unzipRepoArchiveSuccessEvents
TopicName: unzipRepoArchiveSuccessEvents
createAttrFilesSuccessEvents:
Type: "AWS::SQS::Queue"
Properties:
QueueName: createAttrFilesSuccessEvents
VisibilityTimeout: 200
MessageRetentionPeriod: 300
createAttributeFiles:
Type: AWS::Serverless::Function
Properties:
FunctionName: createAttributeFiles
Handler: src/create-attribute-files.handler
Description: Reads AWS Docs repo folder from EFS and creates CloudFormation attribute files in another EFS folder.
ReservedConcurrentExecutions: 1
Environment:
Variables:
EFS_PATH: "/mnt/efs"
SUCC_NOTIFY_SQS: !Ref createAttrFilesSuccessEvents
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambdaVPCAccessExecutionRole
- AmazonElasticFileSystemClientFullAccess
- AmazonSQSFullAccess
- DynamoDBWritePolicy:
TableName: cfnAttributeFilesIndex
VpcConfig:
SecurityGroupIds:
- Fn::ImportValue: !Sub "${lambdaEfsStack}-secgroup-id"
SubnetIds:
- Fn::ImportValue: !Sub "${lambdaEfsStack}-subnet-id"
FileSystemConfigs:
- Arn:
Fn::ImportValue: !Sub "${lambdaEfsStack}-access-pt-arn"
LocalMountPath: /mnt/efs
createAttributeFilesInvoker:
Type: "AWS::SNS::Subscription"
Properties:
Endpoint: !GetAtt createAttributeFiles.Arn
Protocol: lambda
TopicArn: !Ref unzipRepoArchiveSuccessfulInvocations
createAttributeFilesInvokePermission:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !Ref createAttributeFiles
Principal: sns.amazonaws.com
cloudFormationDocsIndex:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: cfnAttributeFilesIndex
AttributeDefinitions:
- AttributeName: api
AttributeType: S
- AttributeName: resource
AttributeType: S
KeySchema:
- AttributeName: api
KeyType: HASH
- AttributeName: resource
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
generateCheatSheet:
Type: AWS::Serverless::Function
Properties:
FunctionName: generateCheatSheet
Handler: src/generate-cheat-sheet.handler
Description: Reads CloudFormation attribute files from EFS, creates a cheatsheet and uploads it to S3.
ReservedConcurrentExecutions: 1
Environment:
Variables:
EFS_PATH: "/mnt/efs"
AWS_CFN_ATTRS_CHEATSHEET_SITE: !Ref awsCfnAttrsCheatSheetSiteName
Policies:
- AWSLambdaBasicExecutionRole
- AmazonS3FullAccess
- AWSLambdaVPCAccessExecutionRole
- AmazonElasticFileSystemClientFullAccess
- AWSLambdaSQSQueueExecutionRole
- DynamoDBReadPolicy:
TableName: cfnAttributeFilesIndex
VpcConfig:
SecurityGroupIds:
- Fn::ImportValue: !Sub "${lambdaEfsStack}-secgroup-id"
SubnetIds:
- Fn::ImportValue: !Sub "${lambdaEfsStack}-subnet-id"
FileSystemConfigs:
- Arn:
Fn::ImportValue: !Sub "${lambdaEfsStack}-access-pt-arn"
LocalMountPath: /mnt/efs
generateCheatSheetEventSource:
Type: "AWS::Lambda::EventSourceMapping"
Properties:
BatchSize: 1
EventSourceArn: !GetAtt createAttrFilesSuccessEvents.Arn
FunctionName: !GetAtt generateCheatSheet.Arn
awsCfnAttrsCheatSheetSite:
Type: "AWS::S3::Bucket"
Properties:
AccessControl: PublicRead
BucketName: !Ref awsCfnAttrsCheatSheetSiteName
WebsiteConfiguration:
ErrorDocument: 404.html
IndexDocument: index.md
Globals:
Function:
CodeUri: ../sam-build/beginning-lambda.zip
Runtime: nodejs12.x
MemorySize: 256
Timeout: 180
Layers:
- Fn::ImportValue: !Sub "${awsSdkLayerStack}-arn"
Parameters:
awsDocsRepoBucketName:
Description: Name of the S3 bucket to store AWS docomentation repo.
Type: String
AllowedPattern: ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-5][0-9a-fA-F]{3}-[089abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$
awsSdkLayerStack:
Description: Stack name of the Lambda layer with Node.js AWS SDK
Type: String
Default: lib-aws-sdk
lambdaEfsStack:
Description: Lambda EFS stack name
Type: String
Default: lambda-efs-vpc
awsCfnAttrsCheatSheetSiteName:
Description: Name of the S3 website bucket to store AWS CloudFormation Attributes cheatsheet.
Type: String
Default: aws-cloudformation-attributes-cheatsheet