-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathauth-stack.ts
More file actions
130 lines (117 loc) · 4.72 KB
/
Copy pathauth-stack.ts
File metadata and controls
130 lines (117 loc) · 4.72 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as cdk from 'aws-cdk-lib';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
export class AuthStack extends cdk.Stack {
public readonly userPool: cognito.UserPool;
public readonly userPoolClient: cognito.UserPoolClient;
public readonly identityPool: cognito.CfnIdentityPool;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Cognito User Pool
this.userPool = new cognito.UserPool(this, 'AgentCoreUserPool', {
userPoolName: 'agentcore-users',
selfSignUpEnabled: true,
signInAliases: {
email: true,
},
autoVerify: {
email: true,
},
standardAttributes: {
email: {
required: true,
mutable: false,
},
},
passwordPolicy: {
minLength: 8,
requireLowercase: true,
requireUppercase: true,
requireDigits: true,
requireSymbols: false,
},
accountRecovery: cognito.AccountRecovery.EMAIL_ONLY,
removalPolicy: cdk.RemovalPolicy.DESTROY, // For dev - change to RETAIN for prod
});
// User Pool Client for frontend
this.userPoolClient = new cognito.UserPoolClient(this, 'AgentCoreUserPoolClient', {
userPool: this.userPool,
userPoolClientName: 'agentcore-web-client',
authFlows: {
userPassword: true,
userSrp: true,
},
generateSecret: false, // Public client (frontend)
preventUserExistenceErrors: true,
});
// Cognito Identity Pool for AWS SDK access
this.identityPool = new cognito.CfnIdentityPool(this, 'AgentCoreIdentityPool', {
identityPoolName: 'agentcore-identity-pool',
allowUnauthenticatedIdentities: false,
cognitoIdentityProviders: [{
clientId: this.userPoolClient.userPoolClientId,
providerName: this.userPool.userPoolProviderName,
}],
});
// IAM Role for authenticated users (frontend)
const authenticatedRole = new iam.Role(this, 'CognitoAuthenticatedRole', {
roleName: 'AgentCoreAuthenticatedRole',
description: 'Role for authenticated Cognito users to access AgentCore Memory',
assumedBy: new iam.FederatedPrincipal(
'cognito-identity.amazonaws.com',
{
'StringEquals': {
'cognito-identity.amazonaws.com:aud': this.identityPool.ref,
},
'ForAnyValue:StringLike': {
'cognito-identity.amazonaws.com:amr': 'authenticated',
},
},
'sts:AssumeRoleWithWebIdentity'
),
});
// Add policy for AgentCore Memory read-only access
const region = cdk.Stack.of(this).region;
const account = cdk.Stack.of(this).account;
authenticatedRole.addToPolicy(new iam.PolicyStatement({
sid: 'AgentCoreMemoryReadAccess',
effect: iam.Effect.ALLOW,
actions: [
'bedrock-agentcore:ListSessions',
'bedrock-agentcore:ListEvents',
],
resources: [
`arn:aws:bedrock-agentcore:${region}:${account}:memory/*`,
],
}));
// Attach roles to Identity Pool
new cognito.CfnIdentityPoolRoleAttachment(this, 'IdentityPoolRoleAttachment', {
identityPoolId: this.identityPool.ref,
roles: {
authenticated: authenticatedRole.roleArn,
},
});
// Outputs
new cdk.CfnOutput(this, 'UserPoolId', {
value: this.userPool.userPoolId,
description: 'Cognito User Pool ID',
exportName: 'AgentCoreUserPoolId',
});
new cdk.CfnOutput(this, 'UserPoolArn', {
value: this.userPool.userPoolArn,
description: 'Cognito User Pool ARN',
exportName: 'AgentCoreUserPoolArn',
});
new cdk.CfnOutput(this, 'UserPoolClientId', {
value: this.userPoolClient.userPoolClientId,
description: 'Cognito User Pool Client ID',
exportName: 'AgentCoreUserPoolClientId',
});
new cdk.CfnOutput(this, 'IdentityPoolId', {
value: this.identityPool.ref,
description: 'Cognito Identity Pool ID',
exportName: 'AgentCoreIdentityPoolId',
});
}
}