· 5 years ago · Jan 20, 2021, 08:20 PM
1AWSTemplateFormatVersion: '2010-09-09'
2Transform: AWS::Serverless-2016-10-31
3Description: 'API Gateway to connect with Elastic Search'
4Parameters:
5 elasticUrl:
6 Type: String
7 Default : 'https://95429f8162384583a4ba1c5363112387.us-east-1.aws.found.io/productdata/_doc/'
8 version:
9 Type: String
10 Default : 'v1'
11 deploymentDescription:
12 Type: String
13 Default : 'First version of api'
14 newStage:
15 Type: String
16 Default: 'false'
17 AllowedValues:
18 - 'true'
19 - 'false'
20 apiGatewayName:
21 Type: String
22 Default: 'kdz-dev-product-api'
23 lastDeployKey:
24 Description: My SSM Value
25 Type: AWS::SSM::Parameter::Value<String>
26 Default: kdz-dev-product-api-last-deployment-api-gateway
27
28Conditions:
29# EnableNewStage: !Equals
30# - !Ref newStage
31# - 'true'
32# DisableNewStage: !Equals
33# - !Ref newStage
34# - 'false'
35 DeploymentApiGateway1: !Equals
36 - !Ref lastDeployKey
37 - '1'
38 DeploymentApiGateway2: !Equals
39 - !Ref lastDeployKey
40 - '2'
41
42Resources:
43 LambdaIamRole:
44 Type: AWS::IAM::Role
45 Properties:
46 AssumeRolePolicyDocument:
47 Version: '2012-10-17'
48 Statement:
49 - Effect: 'Allow'
50 Principal:
51 Service:
52 - 'lambda.amazonaws.com'
53 Action:
54 - 'sts:AssumeRole'
55 Path: '/'
56
57 ProductsFunction:
58 Type: AWS::Serverless::Function
59 Properties:
60 CodeUri: products/
61 Handler: app.lambdaHandler
62 Runtime: nodejs12.x
63 Role: !GetAtt LambdaIamRole.Arn
64 AutoPublishAlias: !Ref version
65
66# ProductsFunctionVersion:
67# DependsOn: ProductsFunction
68# Type : AWS::Lambda::Version
69# Properties :
70# Description : !Ref version
71# FunctionName : !Ref ProductsFunction
72
73# ProductAlias:
74# DependsOn: ProductsFunctionVersion
75# Type: AWS::Lambda::Alias
76# Properties:
77# FunctionName : !Ref ProductsFunction
78# FunctionVersion: !Ref version
79
80 RestApi:
81 Type: AWS::ApiGateway::RestApi
82 Properties:
83 Name: !Ref apiGatewayName
84
85 ProductValidator:
86 Type: AWS::ApiGateway::RequestValidator
87 Properties:
88 RestApiId: !Ref RestApi
89 ValidateRequestBody: true
90 ValidateRequestParameters: true
91
92# ProductGet:
93# Type: AWS::ApiGateway::Method
94# Properties:
95# RequestParameters:
96# method.request.querystring.id: true
97# method.request.querystring.tenant : true
98# method.request.header.Authorization: True
99# MethodResponses:
100# - StatusCode: '200'
101# ResponseModels:
102# application/json: Empty
103# AuthorizationType: NONE
104# HttpMethod: GET
105# ResourceId: !GetAtt RestApi.RootResourceId
106# RestApiId: !Ref RestApi
107# RequestValidatorId: !Ref ProductValidator
108# Integration:
109# IntegrationHttpMethod: GET
110# Uri:
111# Fn::Join: [ '', [ !Ref elasticUrl, '{productid}/_source' ] ]
112# Type: HTTP
113# RequestParameters:
114# integration.request.querystring.id: method.request.querystring.id
115# integration.request.querystring.tenant : method.request.querystring.tenant
116# integration.request.header.Authorization: method.request.header.Authorization
117# integration.request.path.productid: method.request.querystring.id
118# integration.request.path.tenantid: method.request.querystring.tenant
119# IntegrationResponses:
120# - StatusCode: '200'
121
122 ApiGatewayIamRole:
123 Type: AWS::IAM::Role
124 Properties:
125 AssumeRolePolicyDocument:
126 Version: '2012-10-17'
127 Statement:
128 - Sid: ''
129 Effect: 'Allow'
130 Principal:
131 Service:
132 - 'apigateway.amazonaws.com'
133 Action:
134 - 'sts:AssumeRole'
135 Path: '/'
136 Policies:
137 - PolicyName: LambdaAccess
138 PolicyDocument:
139 Version: '2012-10-17'
140 Statement:
141 - Effect: 'Allow'
142 Action: 'lambda:*'
143 Resource:
144 Fn::Join : [ "", [ !GetAtt ProductsFunction.Arn, ":", !Ref version ] ]
145
146 ProductTest:
147 DependsOn:
148# - ProductGet
149 - ProductsFunction
150 - ApiGatewayIamRole
151 Type: AWS::ApiGateway::Method
152 Properties:
153 OperationName: 'lambda'
154 MethodResponses:
155 - StatusCode: '200'
156 ResponseModels:
157 application/json: Empty
158 AuthorizationType: NONE
159 HttpMethod: POST
160 ResourceId: !GetAtt RestApi.RootResourceId
161 RestApiId: !Ref RestApi
162 RequestValidatorId: !Ref ProductValidator
163 Integration:
164 ConnectionType: INTERNET
165 Credentials: !GetAtt ApiGatewayIamRole.Arn
166 IntegrationHttpMethod: POST
167 Type: AWS_PROXY
168 Uri: !Sub arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${ProductsFunction.Arn}:${version}/invocations
169 IntegrationResponses:
170 - StatusCode: '200'
171
172 Deployment1:
173 DependsOn: DeploymentApiGateway2
174 Type: AWS::ApiGateway::Deployment
175 Properties:
176 RestApiId: !Ref RestApi
177 Description: !Ref deploymentDescription
178 StageName: !Ref version
179
180 Deployment2:
181 DependsOn: DeploymentApiGateway1
182 Type: AWS::ApiGateway::Deployment
183 Properties:
184 RestApiId: !Ref RestApi
185 Description: !Ref deploymentDescription
186 StageName: !Ref version
187
188 ParameterDeploymentChange:
189 Type: AWS::SSM::Parameter
190 Properties:
191 Name: "kdz-dev-product-api-last-deployment-api-gateway"
192 Value:
193
194 ProductLambdaApiGatewayInvoke:
195 DependsOn: ProductTest
196 Type: AWS::Lambda::Permission
197 Properties:
198 Action: lambda:InvokeFunction
199 FunctionName:
200 Fn::Join : [ "", [ !GetAtt ProductsFunction.Arn, ":", !Ref version ] ]
201 Principal: apigateway.amazonaws.com
202 SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApi}/*/POST/
203
204#Outputs:
205 # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
206 # Find out more about other implicit resources you can reference within SAM
207 # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
208# ProductsApi:
209# Description: "API Gateway endpoint URL for Prod stage for Konduza Products functions"
210# Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/products/"
211# ProductsFunction:
212# Description: "Product Lambda Function ARN"
213# Value: !GetAtt ProductsFunction.Arn
214# ProductsFunctionIamRole:
215# Description: "Implicit IAM Role created for Products function"
216# Value: !GetAtt ProductsFunctionRole.Arn
217