forked from jon-nona/api-gateway-proxy-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-gateway-proxy-example-stack.ts
More file actions
110 lines (102 loc) · 3.6 KB
/
api-gateway-proxy-example-stack.ts
File metadata and controls
110 lines (102 loc) · 3.6 KB
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
import * as apigateway from '@aws-cdk/aws-apigateway'
import * as lambda from '@aws-cdk/aws-lambda'
import * as secretsmanager from '@aws-cdk/aws-secretsmanager'
import * as cdk from '@aws-cdk/core'
import { StackProps } from '@aws-cdk/core'
import * as path from 'path'
import { integrationResponses, methodResponses } from './responses'
export class ApiGatewayProxyExampleStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: StackProps) {
super(scope, id, props)
const apiGatewayProxyExampleSecrets = secretsmanager.Secret.fromSecretName(
this,
'ApiGatewayProxyExampleSecret',
'apiGateWayProxyExampleStack',
)
const apiKey = apiGatewayProxyExampleSecrets.secretValueFromJson('apiKey')
const flickrApiKey = apiGatewayProxyExampleSecrets.secretValueFromJson(
'flickrApiKey',
)
const authorizerLambda = new lambda.Function(
this,
'ApiGatewayProxyExampleAuthorizerLambda',
{
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromAsset(path.join(__dirname, '..', 'dist')),
environment: {
API_TOKEN: `${apiKey}`,
},
handler: 'authorizers/authorizer.handler',
},
)
const authorizer = new apigateway.TokenAuthorizer(
this,
'apiGatewayProxyExampleAuthorizer',
{
handler: authorizerLambda,
},
)
const api = new apigateway.RestApi(this, `ApiGatewayProxyExampleApi`, {
defaultCorsPreflightOptions: {
allowOrigins: apigateway.Cors.ALL_ORIGINS,
},
deployOptions: {
stageName: 'v1',
},
})
const flickrPhotosSearchLambda = new lambda.Function(
this,
'FlickrPhotoSearchLambda',
{
functionName: 'FlickrPhotoSearchLambda',
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromAsset(path.join(__dirname, '..', 'dist')),
handler: 'modules/flickr/handlers.searchPhotos',
environment: {
API_KEY: `${flickrApiKey}`,
API_URL: 'https://www.flickr.com/services/rest',
},
},
)
const flickrRecentPhotosIntegration = new apigateway.Integration({
integrationHttpMethod: 'GET',
type: apigateway.IntegrationType.HTTP,
uri: `https://www.flickr.com/services/rest`,
options: {
connectionType: apigateway.ConnectionType.INTERNET,
integrationResponses,
requestParameters: {
'integration.request.querystring.api_key': `'${flickrApiKey}'`,
'integration.request.querystring.format': `'json'`,
'integration.request.querystring.nojsoncallback': `'1'`,
'integration.request.querystring.method': `'flickr.photos.getRecent'`,
'integration.request.querystring.extras':
'method.request.querystring.extras',
'integration.request.querystring.per_page':
'method.request.querystring.per_page',
'integration.request.querystring.page':
'method.request.querystring.page',
},
},
})
const photos = api.root.addResource('photos')
const photosSearch = photos.addResource('search')
photosSearch.addMethod(
'GET',
new apigateway.LambdaIntegration(flickrPhotosSearchLambda),
{
authorizer,
},
)
const photosRecent = photos.addResource('recent')
photosRecent.addMethod('GET', flickrRecentPhotosIntegration, {
authorizer,
requestParameters: {
'method.request.querystring.extras': true,
'method.request.querystring.per_page': true,
'method.request.querystring.page': true,
},
methodResponses,
})
}
}