In this section, we will use AWS SAM to create the Cognito User Pool, User Pool Client, Identity Pool and the S3 bucket that stores the files users upload. These are the resources that Amplify Gen 2 created for you in workshop 134.
You need to install SAM CLI version 1.146.0 or later and configure credentials with the aws configure command (region ap-southeast-1) before doing this part.
sam init
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Choose an AWS Quick Start application template
1 - Hello World Example
...
Template: 1
Use the most popular runtime and package type? (python3.14 and zip) [y/N]: y
Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: N
Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: N
Would you like to set Structured Logging in JSON format on your Lambda functions? [y/N]: N
Project name [sam-app]: fcajdms-sam
If SAM CLI suggests a runtime other than python3.14, enter N, then select the python3.14 runtime and the Zip package type.

cd fcajdms-sam
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
fcajdms-sam
FCAJ Serverless Document Management System
Globals:
Function:
Runtime: python3.14
Timeout: 10
Architectures:
- x86_64
Resources:

# Cognito User Pool: users sign up and sign in with their email
FcajDMSUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: fcaj-dms-user-pool
UsernameAttributes:
- email
AutoVerifiedAttributes:
- email
MfaConfiguration: "OFF"
EmailConfiguration:
EmailSendingAccount: COGNITO_DEFAULT
AccountRecoverySetting:
RecoveryMechanisms:
- Name: verified_email
Priority: 1
Schema:
- Name: email
AttributeDataType: String
Required: true
Mutable: true
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: true
RequireNumbers: true
RequireSymbols: true
RequireUppercase: true
TemporaryPasswordValidityDays: 7
UserAttributeUpdateSettings:
AttributesRequireVerificationBeforeUpdate:
- email
# App client used by the web application (no client secret)
FcajDMSUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
ClientName: fcaj-dms-client
UserPoolId: !Ref FcajDMSUserPool
GenerateSecret: false
ExplicitAuthFlows:
- ALLOW_USER_SRP_AUTH
- ALLOW_REFRESH_TOKEN_AUTH
# Identity Pool: temporary AWS credentials to access S3 directly
FcajDMSIdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
IdentityPoolName: fcaj_dms_identity_pool
AllowUnauthenticatedIdentities: true
CognitoIdentityProviders:
- ClientId: !Ref FcajDMSUserPoolClient
ProviderName: !GetAtt FcajDMSUserPool.ProviderName
This code block initializes the following resources:
UsernameAttributes), and the email is verified with a code sent automatically (AutoVerifiedAttributes). The password policy requires at least 8 characters with uppercase, lowercase, number and special characters, the same as the Amplify Gen 2 default policy and the password check of the application.The User Pool must use email as the username so that the application can call signUp with an email, as in workshop 134. The Display name is saved in the standard preferred_username attribute. The app client does not restrict write attributes, so the application can write and update this attribute.

# Role for guests: no access to AWS resources
CognitoUnauthenticatedRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Federated: cognito-identity.amazonaws.com
Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud": !Ref FcajDMSIdentityPool
"ForAnyValue:StringLike":
"cognito-identity.amazonaws.com:amr": unauthenticated
# Role for signed-in users: same "protected" access level as workshop 134
CognitoAuthenticatedRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Federated: cognito-identity.amazonaws.com
Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud": !Ref FcajDMSIdentityPool
"ForAnyValue:StringLike":
"cognito-identity.amazonaws.com:amr": authenticated
Policies:
- PolicyName: FcajDMSDocumentStoreAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource: !Sub "${FcajDMSDocumentStore.Arn}/protected/*"
- Effect: Allow
Action:
- s3:PutObject
- s3:DeleteObject
Resource: !Sub "${FcajDMSDocumentStore.Arn}/protected/${!cognito-identity.amazonaws.com:sub}/*"
IdentityPoolRoleAttachment:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId: !Ref FcajDMSIdentityPool
Roles:
authenticated: !GetAtt CognitoAuthenticatedRole.Arn
unauthenticated: !GetAtt CognitoUnauthenticatedRole.Arn
protected/{entity_id}/* rule of workshop 134: every signed-in user can read the files in the protected/ folder, but only the owner can write and delete files in their own protected/{Identity ID}/ folder.${!cognito-identity.amazonaws.com:sub}: the ! tells !Sub to keep this variable as is in the policy. When a user sends a request, IAM replaces the variable with the Identity ID of that user.
# S3 bucket that stores the files uploaded by users
FcajDMSDocumentStore:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "fcajdmsstore-${AWS::AccountId}-${AWS::Region}-an"
BucketNamespace: account-regional
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- '*'
AllowedMethods:
- GET
- HEAD
- PUT
- POST
- DELETE
AllowedOrigins:
- '*'
ExposedHeaders:
- x-amz-server-side-encryption
- x-amz-request-id
- x-amz-id-2
- ETag
MaxAge: 3000
BucketNamespace: account-regional: creates the bucket in the Account Regional namespace, the same as the Account Regional namespace (recommended) option you chose when creating the bucket in workshop 135.With the Account Regional namespace, the bucket name only needs to be unique within your AWS account and Region, and it must end with a suffix that contains the account ID and Region. That is why the template builds the bucket name with ${AWS::AccountId} and ${AWS::Region}, for example: fcajdmsstore-<ACCOUNT_ID>-ap-southeast-1-an. You do not need to change the bucket name, and you will read the full name in the Outputs tab of the stack. The BucketNamespace property needs a recent SAM CLI version; if sam validate --lint reports BucketNamespace as an invalid property, update SAM CLI.

Outputs:
UserPoolId:
Description: Cognito User Pool ID
Value: !Ref FcajDMSUserPool
UserPoolClientId:
Description: Cognito User Pool app client ID
Value: !Ref FcajDMSUserPoolClient
IdentityPoolId:
Description: Cognito Identity Pool ID
Value: !Ref FcajDMSIdentityPool
DocumentStoreBucket:
Description: S3 bucket that stores the uploaded files
Value: !Ref FcajDMSDocumentStore

sam build

sam validate --lint

sam deploy --guided
Stack Name [fcajdms-sam]: fcajdms
AWS Region [ap-southeast-1]: ap-southeast-1
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [y/N]: y
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]: Y
#Preserves the state of previously provisioned resources when an operation fails
Disable rollback [y/N]: N
Save arguments to configuration file [Y/n]: Y
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:

y when asked Deploy this changeset?

SAM saves your choices in the samconfig.toml file in the fcajdms-sam folder. From now on, you only need to run sam build and sam deploy; SAM reuses the fcajdms stack name and the ap-southeast-1 Region.



You have created the authentication and storage resources with AWS SAM. In the next part, we will create the S3 bucket that hosts the web application and deploy the front-end.