Skip to content

Instantly share code, notes, and snippets.

@singledigit
Last active September 20, 2025 17:27
Show Gist options
  • Select an option

  • Save singledigit/2c4d7232fa96d9e98a3de89cf6ebe7a5 to your computer and use it in GitHub Desktop.

Select an option

Save singledigit/2c4d7232fa96d9e98a3de89cf6ebe7a5 to your computer and use it in GitHub Desktop.

Revisions

  1. singledigit revised this gist Jul 12, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion cognito.yaml
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@ Parameters:
    Description: Unique Auth Name for Cognito Resources

    Resources:

    # Creates a role that allows Cognito to send SNS messages
    SNSRole:
    Type: "AWS::IAM::Role"
  2. singledigit revised this gist May 1, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cognito.yaml
    Original file line number Diff line number Diff line change
    @@ -86,7 +86,7 @@ Resources:
    Statement:
    - Effect: "Allow"
    Principal:
    Federated: "cognito-idp.amazonaws.com"
    Federated: "cognito-identity.amazonaws.com"
    Action:
    - "sts:AssumeRoleWithWebIdentity"
    Condition:
    @@ -115,7 +115,7 @@ Resources:
    Statement:
    - Effect: "Allow"
    Principal:
    Federated: "cognito-idp.amazonaws.com"
    Federated: "cognito-identity.amazonaws.com"
    Action:
    - "sts:AssumeRoleWithWebIdentity"
    Condition:
  3. singledigit revised this gist May 1, 2017. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions cognito.yaml
    Original file line number Diff line number Diff line change
    @@ -147,3 +147,17 @@ Resources:
    Roles:
    authenticated: !GetAtt CognitoAuthorizedRole.Arn
    unauthenticated: !GetAtt CognitoUnAuthorizedRole.Arn

    Outputs:
    UserPoolId:
    Value: !Ref UserPool
    Export:
    Name: "UserPool::Id"
    UserPoolClientId:
    Value: !Ref UserPoolClient
    Export:
    Name: "UserPoolClient::Id"
    IdentityPoolId:
    Value: !Ref IdentityPool
    Export:
    Name: "IdentityPool::Id"
  4. singledigit revised this gist May 1, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cognito.yaml
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    AWSTemplateFormatVersion: '2010-09-09'
    Description: External Resources for FAWS Tracker
    Description: Cognito Stack
    Parameters:
    AuthName:
    Type: String
  5. singledigit renamed this gist May 1, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. singledigit revised this gist May 1, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions description.md
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,6 @@ This creates a starting point for a simple Authentication backend using AWS Cogn

    To get started builing a client...

    https://github.com/aws/amazon-cognito-identity-js
    https://blog.rackspace.com/part-1-building-server-less-architecture-aws
    Identity: https://github.com/aws/amazon-cognito-identity-js

    Serverless: https://blog.rackspace.com/part-1-building-server-less-architecture-aws
  7. singledigit revised this gist May 1, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions description.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    This creates a starting point for a simple Authentication backend using AWS Cognito. With this you can create everything you need for the backend to register, login, and access AWS Lambda and other services.

    To get started builing a client...

    https://github.com/aws/amazon-cognito-identity-js
    https://blog.rackspace.com/part-1-building-server-less-architecture-aws
  8. singledigit renamed this gist May 1, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. singledigit created this gist May 1, 2017.
    149 changes: 149 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,149 @@
    AWSTemplateFormatVersion: '2010-09-09'
    Description: External Resources for FAWS Tracker
    Parameters:
    AuthName:
    Type: String
    Description: Unique Auth Name for Cognito Resources

    Resources:

    # Creates a role that allows Cognito to send SNS messages
    SNSRole:
    Type: "AWS::IAM::Role"
    Properties:
    AssumeRolePolicyDocument:
    Version: "2012-10-17"
    Statement:
    - Effect: "Allow"
    Principal:
    Service:
    - "cognito-idp.amazonaws.com"
    Action:
    - "sts:AssumeRole"
    Policies:
    - PolicyName: "CognitoSNSPolicy"
    PolicyDocument:
    Version: "2012-10-17"
    Statement:
    - Effect: "Allow"
    Action: "sns:publish"
    Resource: "*"

    # Creates a user pool in cognito for your app to auth against
    # This example requires MFA and validates the phone number to use as MFA
    # Other fields can be added to the schema
    UserPool:
    Type: "AWS::Cognito::UserPool"
    Properties:
    UserPoolName: !Sub ${AuthName}-user-pool
    AutoVerifiedAttributes:
    - phone_number
    MfaConfiguration: "ON"
    SmsConfiguration:
    ExternalId: !Sub ${AuthName}-external
    SnsCallerArn: !GetAtt SNSRole.Arn
    Schema:
    - Name: name
    AttributeDataType: String
    Mutable: true
    Required: true
    - Name: email
    AttributeDataType: String
    Mutable: false
    Required: true
    - Name: phone_number
    AttributeDataType: String
    Mutable: false
    Required: true
    - Name: slackId
    AttributeDataType: String
    Mutable: true

    # Creates a User Pool Client to be used by the identity pool
    UserPoolClient:
    Type: "AWS::Cognito::UserPoolClient"
    Properties:
    ClientName: !Sub ${AuthName}-client
    GenerateSecret: false
    UserPoolId: !Ref UserPool

    # Creates a federeated Identity pool
    IdentityPool:
    Type: "AWS::Cognito::IdentityPool"
    Properties:
    IdentityPoolName: !Sub ${AuthName}Identity
    AllowUnauthenticatedIdentities: true
    CognitoIdentityProviders:
    - ClientId: !Ref UserPoolClient
    ProviderName: !GetAtt UserPool.ProviderName

    # Create a role for unauthorized acces to AWS resources. Very limited access. Only allows users in the previously created Identity Pool
    CognitoUnAuthorizedRole:
    Type: "AWS::IAM::Role"
    Properties:
    AssumeRolePolicyDocument:
    Version: "2012-10-17"
    Statement:
    - Effect: "Allow"
    Principal:
    Federated: "cognito-idp.amazonaws.com"
    Action:
    - "sts:AssumeRoleWithWebIdentity"
    Condition:
    StringEquals:
    "cognito-identity.amazonaws.com:aud": !Ref IdentityPool
    "ForAnyValue:StringLike":
    "cognito-identity.amazonaws.com:amr": unauthenticated
    Policies:
    - PolicyName: "CognitoUnauthorizedPolicy"
    PolicyDocument:
    Version: "2012-10-17"
    Statement:
    - Effect: "Allow"
    Action:
    - "mobileanalytics:PutEvents"
    - "cognito-sync:*"
    Resource: "*"

    # Create a role for authorized acces to AWS resources. Control what your user can access. This example only allows Lambda invokation
    # Only allows users in the previously created Identity Pool
    CognitoAuthorizedRole:
    Type: "AWS::IAM::Role"
    Properties:
    AssumeRolePolicyDocument:
    Version: "2012-10-17"
    Statement:
    - Effect: "Allow"
    Principal:
    Federated: "cognito-idp.amazonaws.com"
    Action:
    - "sts:AssumeRoleWithWebIdentity"
    Condition:
    StringEquals:
    "cognito-identity.amazonaws.com:aud": !Ref IdentityPool
    "ForAnyValue:StringLike":
    "cognito-identity.amazonaws.com:amr": authenticated
    Policies:
    - PolicyName: "CognitoAuthorizedPolicy"
    PolicyDocument:
    Version: "2012-10-17"
    Statement:
    - Effect: "Allow"
    Action:
    - "mobileanalytics:PutEvents"
    - "cognito-sync:*"
    - "cognito-identity:*"
    Resource: "*"
    - Effect: "Allow"
    Action:
    - "lambda:InvokeFunction"
    Resource: "*"

    # Assigns the roles to the Identity Pool
    IdentityPoolRoleMapping:
    Type: "AWS::Cognito::IdentityPoolRoleAttachment"
    Properties:
    IdentityPoolId: !Ref IdentityPool
    Roles:
    authenticated: !GetAtt CognitoAuthorizedRole.Arn
    unauthenticated: !GetAtt CognitoUnAuthorizedRole.Arn