-
Notifications
You must be signed in to change notification settings - Fork 6
/
rest-api-gateway-stack.ts
170 lines (152 loc) · 5.65 KB
/
rest-api-gateway-stack.ts
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
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as elbv2 from "aws-cdk-lib/aws-elasticloadbalancingv2";
import * as apigateway from "aws-cdk-lib/aws-apigateway";
import { PassthroughBehavior } from "aws-cdk-lib/aws-apigateway"
import * as cognito from 'aws-cdk-lib/aws-cognito';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as lambdapython from '@aws-cdk/aws-lambda-python-alpha';
import { IdentityPool, UserPoolAuthenticationProvider } from '@aws-cdk/aws-cognito-identitypool-alpha';
export class RestApiGatewayStack extends cdk.Stack {
public readonly restApi: apigateway.RestApi;
public readonly userPool: cognito.UserPool;
public readonly userPoolClient: cognito.UserPoolClient
public readonly identityPool: IdentityPool;
constructor(
scope: Construct,
id: string,
httpApiInternalNLB: elbv2.NetworkLoadBalancer,
props?: cdk.StackProps
) {
super(scope, id, props);
const link = new apigateway.VpcLink(this, 'link', {
targets: [httpApiInternalNLB],
});
// User Pool
this.userPool = new cognito.UserPool(this, 'userpool', {
userPoolName: 'mlflow-user-pool',
selfSignUpEnabled: true,
signInAliases: {
email: true,
},
autoVerify: {
email: false,
},
passwordPolicy: {
minLength: 6,
requireLowercase: true,
requireDigits: true,
requireUppercase: true,
requireSymbols: true,
},
accountRecovery: cognito.AccountRecovery.EMAIL_ONLY,
removalPolicy: cdk.RemovalPolicy.RETAIN,
});
this.identityPool = new IdentityPool(this, 'mlflow-identity-pool', {
identityPoolName: 'mlflow-identity-pool',
authenticationProviders: {
userPools: [new UserPoolAuthenticationProvider({ userPool: this.userPool })],
},
});
this.userPoolClient = this.userPool.addClient('mlflow-app-client', {
supportedIdentityProviders: [
cognito.UserPoolClientIdentityProvider.COGNITO,
],
});
const defaultProxyApiIntegration = new apigateway.Integration(
{
type: apigateway.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
options: {
connectionType: apigateway.ConnectionType.VPC_LINK,
vpcLink: link,
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES,
},
uri: `http://${httpApiInternalNLB.loadBalancerDnsName}/`
}
);
this.restApi = new apigateway.RestApi(this, 'mlflow-rest-api',
{
defaultIntegration: defaultProxyApiIntegration,
defaultMethodOptions: {
methodResponses: [{
statusCode: "200"
}],
}
})
const lambdaFunction = new lambdapython.PythonFunction(this, 'MyFunction', {
entry: './lambda/authorizer/', // required
runtime: lambda.Runtime.PYTHON_3_9, // required
index: 'index.py', // optional, defaults to 'index.py'
handler: 'handler', // optional, defaults to 'handler'
environment: {
REGION: this.region,
ACCOUNT: this.account,
COGNITO_USER_POOL_ID: this.userPool.userPoolId,
REST_API_ID: this.restApi.restApiId,
COGNITO_KEYS_URL: `https://cognito-idp.${this.region}.amazonaws.com/${this.userPool.userPoolId}/.well-known/jwks.json`,
APP_CLIENT_ID: this.userPoolClient.userPoolClientId
},
});
const lambdaAuthorizer = new apigateway.RequestAuthorizer(this, 'lambda-authorizer', {
handler: lambdaFunction,
identitySources: [apigateway.IdentitySource.header('Authorization')],
resultsCacheTtl: cdk.Duration.seconds(0)
});
const proxyApiIntegration = new apigateway.Integration(
{
type: apigateway.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
options: {
connectionType: apigateway.ConnectionType.VPC_LINK,
vpcLink: link,
requestParameters: {
'integration.request.path.proxy': 'method.request.path.proxy'
},
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES
},
uri: `http://${httpApiInternalNLB.loadBalancerDnsName}/{proxy}`
}
);
const rootProxy = this.restApi.root.addProxy({
defaultIntegration: proxyApiIntegration,
defaultMethodOptions: {
requestParameters: {
'method.request.path.proxy': true
},
authorizer: lambdaAuthorizer,
authorizationType: apigateway.AuthorizationType.CUSTOM,
},
// "false" will require explicitly adding methods on the `proxy` resource
anyMethod: true // "true" is the default
});
const apiIntegration = new apigateway.Integration(
{
type: apigateway.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
options: {
connectionType: apigateway.ConnectionType.VPC_LINK,
vpcLink: link,
requestParameters: {
'integration.request.path.proxy': 'method.request.path.proxy'
},
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES
},
uri: `http://${httpApiInternalNLB.loadBalancerDnsName}/api/{proxy}`
});
this.restApi.root.addResource('api').addProxy({
defaultIntegration: apiIntegration,
defaultMethodOptions: {
requestParameters: {
'method.request.path.proxy': true
},
authorizationType: apigateway.AuthorizationType.IAM,
},
// "false" will require explicitly adding methods on the `proxy` resource
anyMethod: true // "true" is the default
});
new cdk.CfnOutput(this, "Rest API Output : ", {
value: this.restApi.url,
});
}
}