forked from matsev/cloudformation-api-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation.template
191 lines (173 loc) · 5.18 KB
/
cloudformation.template
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
# https://github.com/matsev/cloudformation-api-gateway
---
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS CloudFormation sample template that contains a single Lambda function behind an API Gateway
Resources:
#
# API Layer
# ###########################################################################################
ApiGatewayAccount:
Type: AWS::ApiGateway::Account
Properties:
CloudWatchRoleArn: !GetAtt 'ApiGatewayCloudWatchLogsRole.Arn'
GreetingApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: Greeting API
Description: API used for Greeting requests
FailOnWarnings: true
GreetingResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref 'GreetingApi'
ParentId: !GetAtt 'GreetingApi.RootResourceId'
PathPart: greeting
GreetingRequest:
DependsOn: LambdaPermission
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: GET
Integration:
Type: AWS
IntegrationHttpMethod: POST
Uri: !Join
- ''
- - 'arn:aws:apigateway:'
- !Ref 'AWS::Region'
- :lambda:path/2015-03-31/functions/
- !GetAtt 'GreetingLambda.Arn'
- /invocations
IntegrationResponses:
- StatusCode: "200"
RequestTemplates:
application/json: !Join
- ''
- - '{'
- ' "name": "$input.params(''name'')"'
- '}'
RequestParameters:
method.request.querystring.name: false
ResourceId: !Ref 'GreetingResource'
RestApiId: !Ref 'GreetingApi'
MethodResponses:
- StatusCode: "200"
GreetingApiStage:
DependsOn:
- ApiGatewayAccount
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref 'ApiDeployment'
MethodSettings:
- DataTraceEnabled: true
HttpMethod: '*'
LoggingLevel: INFO
ResourcePath: /*
RestApiId: !Ref 'GreetingApi'
StageName: LATEST
ApiDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- GreetingRequest
Properties:
RestApiId: !Ref 'GreetingApi'
StageName: DummyStage
#
# Permissions Layer
# ###########################################################################################
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
LambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:invokeFunction
FunctionName: !GetAtt 'GreetingLambda.Arn'
Principal: apigateway.amazonaws.com
SourceArn: !Join
- ''
- - 'arn:aws:execute-api:'
- !Ref 'AWS::Region'
- ':'
- !Ref 'AWS::AccountId'
- ':'
- !Ref 'GreetingApi'
- /*
ApiGatewayCloudWatchLogsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: ApiGatewayLogsPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutLogEvents
- logs:GetLogEvents
- logs:FilterLogEvents
Resource: '*'
#
# Compute Layer
# ###########################################################################################
GreetingLambda:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: !Join
- "\n"
- - '''use strict'';'
- ''
- // Greeter Lambda
- exports.handler = (event, context, callback) => {
- ' console.log(''Event:'', JSON.stringify(event));'
- ' const name = event.name || ''World'';'
- ' const response = {greeting: `Hello, ${name}!`};'
- ' callback(null, response);'
- '};'
Description: A greeting function
FunctionName: GreetingLambda
Handler: index.handler
Role: !GetAtt 'LambdaExecutionRole.Arn'
Runtime: nodejs10.x
GreetingLogGroup:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: !Sub "/aws/lambda/GreetingLambda"
RetentionInDays: 14
Outputs:
apiGatewayInvokeURL:
Value: !Sub "https://${GreetingApi}.execute-api.${AWS::Region}.amazonaws.com/LATEST"
RootUrl:
Description: Root URL of the API gateway
Value: !Join
- ''
- - https://
- !Ref 'GreetingApi'
- .execute-api.
- !Ref 'AWS::Region'
- .amazonaws.com