-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.ts
108 lines (94 loc) · 3.37 KB
/
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
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as cdk from 'aws-cdk-lib';
import { join } from "path";
export class Part04CognitoStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const api = new cdk.aws_apigateway.RestApi(this, 'api', {
restApiName: 'Part04Service',
});
const userPool = new cdk.aws_cognito.UserPool(this, 'myFirstUserPool', {
selfSignUpEnabled: true,
autoVerify: {
email: true,
},
});
const userPoolClient = new cdk.aws_cognito.UserPoolClient(this, 'myFirstUserPoolClient', {
userPool,
authFlows: {
userPassword: true,
},
});
const authorizer = new cdk.aws_apigateway.CognitoUserPoolsAuthorizer(this, 'myFirstAuthorizer', {
cognitoUserPools: [userPool],
identitySource: 'method.request.header.Authorization',
});
const signup = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'signup', {
entry: join(__dirname, 'signup', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
USER_POOL_CLIENT_ID: userPoolClient.userPoolClientId,
},
});
signup.addToRolePolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['cognito-idp:SignUp'],
resources: [userPool.userPoolArn],
})
);
const signin = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'signin', {
entry: join(__dirname, 'signin', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
USER_POOL_CLIENT_ID: userPoolClient.userPoolClientId,
},
});
signin.addToRolePolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['cognito-idp:InitiateAuth', 'cognito-idp:RespondToAuthChallenge'],
resources: [userPool.userPoolArn],
})
);
const confirm = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'confirm', {
entry: join(__dirname, 'confirm', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
USER_POOL_CLIENT_ID: userPoolClient.userPoolClientId,
},
});
confirm.addToRolePolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['cognito-idp:ConfirmSignUp'],
resources: [userPool.userPoolArn],
})
);
const secret = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'secret', {
entry: join(__dirname, 'secret', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
});
api.root.addResource('sign-up').addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(signup));
api.root.addResource('sign-in').addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(signin));
api.root.addResource('confirm').addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(confirm));
api.root.addResource('secret').addMethod('GET', new cdk.aws_apigateway.LambdaIntegration(secret), {
authorizer,
authorizationType: cdk.aws_apigateway.AuthorizationType.COGNITO,
});
}
}