{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "The AWS CloudFormation template for this Serverless application",
  "Resources": {
    "ServerlessDeploymentBucket": {
      "Type": "AWS::S3::Bucket"
    },
    "Testfct1LogGroup": {
      "Type": "AWS::Logs::LogGroup",
      "Properties": {
        "LogGroupName": "/aws/lambda/sls-test-project-dev-testfct1"
      }
    },
    "TestauthLogGroup": {
      "Type": "AWS::Logs::LogGroup",
      "Properties": {
        "LogGroupName": "/aws/lambda/sls-test-project-dev-testauth"
      }
    },
    "IamRoleLambdaExecution": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Policies": [
          {
            "PolicyName": {
              "Fn::Join": [
                "-",
                [
                  "dev",
                  "sls-test-project",
                  "lambda"
                ]
              ]
            },
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "logs:CreateLogStream"
                  ],
                  "Resource": [
                    {
                      "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/sls-test-project-dev-testfct1:*"
                    },
                    {
                      "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/sls-test-project-dev-testauth:*"
                    }
                  ]
                },
                {
                  "Effect": "Allow",
                  "Action": [
                    "logs:PutLogEvents"
                  ],
                  "Resource": [
                    {
                      "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/sls-test-project-dev-testfct1:*:*"
                    },
                    {
                      "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/sls-test-project-dev-testauth:*:*"
                    }
                  ]
                }
              ]
            }
          }
        ],
        "Path": "/",
        "RoleName": {
          "Fn::Join": [
            "-",
            [
              "sls-test-project",
              "dev",
              "us-east-1",
              "lambdaRole"
            ]
          ]
        }
      }
    },
    "Testfct1LambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "S3Bucket": {
            "Ref": "ServerlessDeploymentBucket"
          },
          "S3Key": "serverless/sls-test-project/dev/1496754891214-2017-06-06T13:14:51.214Z/sls-test-project.zip"
        },
        "FunctionName": "sls-test-project-dev-testfct1",
        "Handler": "handlers/testfct1/handler.handle",
        "MemorySize": 512,
        "Role": {
          "Fn::GetAtt": [
            "IamRoleLambdaExecution",
            "Arn"
          ]
        },
        "Runtime": "nodejs4.3",
        "Timeout": 15,
        "Description": "Echo function echoes alias",
        "Environment": {
          "Variables": {
            "SERVERLESS_PROJECT_NAME": "sls-test-project",
            "SERVERLESS_PROJECT": "sls-test-project",
            "SERVERLESS_STAGE": "dev",
            "SERVERLESS_REGION": "us-east-1",
            "TEST_TABLE_NAME": {
              "Ref": "TestDynamoDbTable"
            }
          }
        }
      },
      "DependsOn": [
        "Testfct1LogGroup",
        "IamRoleLambdaExecution"
      ]
    },
    "Testfct1LambdaVersionrvZ7KY7UgzQ2OKbOvZoG1zLgodltc7toF3qYeORU": {
      "Type": "AWS::Lambda::Version",
      "DeletionPolicy": "Retain",
      "Properties": {
        "FunctionName": {
          "Ref": "Testfct1LambdaFunction"
        },
        "CodeSha256": "rvZ7KY7UgzQ2OKbOvZoG1zLgodltc7+to/F3q+YeORU=",
        "Description": "Echo function echoes alias"
      }
    },
    "TestauthLambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "S3Bucket": {
            "Ref": "ServerlessDeploymentBucket"
          },
          "S3Key": "serverless/sls-test-project/dev/1496754891214-2017-06-06T13:14:51.214Z/sls-test-project.zip"
        },
        "FunctionName": "sls-test-project-dev-testauth",
        "Handler": "handlers/testauth/handler.handle",
        "MemorySize": 512,
        "Role": {
          "Fn::GetAtt": [
            "IamRoleLambdaExecution",
            "Arn"
          ]
        },
        "Runtime": "nodejs4.3",
        "Timeout": 15,
        "Description": "A custom authorizer",
        "Environment": {
          "Variables": {
            "SERVERLESS_PROJECT_NAME": "sls-test-project",
            "SERVERLESS_PROJECT": "sls-test-project",
            "SERVERLESS_STAGE": "dev",
            "SERVERLESS_REGION": "us-east-1",
            "TEST_TABLE_NAME": {
              "Ref": "TestDynamoDbTable"
            }
          }
        }
      },
      "DependsOn": [
        "TestauthLogGroup",
        "IamRoleLambdaExecution"
      ]
    },
    "TestauthLambdaVersionrvZ7KY7UgzQ2OKbOvZoG1zLgodltc7toF3qYeORU": {
      "Type": "AWS::Lambda::Version",
      "DeletionPolicy": "Retain",
      "Properties": {
        "FunctionName": {
          "Ref": "TestauthLambdaFunction"
        },
        "CodeSha256": "rvZ7KY7UgzQ2OKbOvZoG1zLgodltc7+to/F3q+YeORU=",
        "Description": "A custom authorizer"
      }
    },
    "ApiGatewayRestApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "dev-sls-test-project"
      }
    },
    "ApiGatewayResourceFunc1": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "ParentId": {
          "Fn::GetAtt": [
            "ApiGatewayRestApi",
            "RootResourceId"
          ]
        },
        "PathPart": "func1",
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        }
      }
    },
    "ApiGatewayResourceFunc2": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "ParentId": {
          "Fn::GetAtt": [
            "ApiGatewayRestApi",
            "RootResourceId"
          ]
        },
        "PathPart": "func1",
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        }
      }
    },
    "ApiGatewayMethodFunc1Get": {
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "HttpMethod": "GET",
        "RequestParameters": {},
        "ResourceId": {
          "Ref": "ApiGatewayResourceFunc1"
        },
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "AuthorizationType": "CUSTOM",
        "AuthorizerId": {
          "Ref": "TestauthApiGatewayAuthorizer"
        },
        "Integration": {
          "IntegrationHttpMethod": "POST",
          "Type": "AWS_PROXY",
          "Uri": {
            "Fn::Join": [
              "",
              [
                "arn:aws:apigateway:",
                {
                  "Ref": "AWS::Region"
                },
                ":lambda:path/2015-03-31/functions/",
                {
                  "Fn::GetAtt": [
                    "Testfct1LambdaFunction",
                    "Arn"
                  ]
                },
                "/invocations"
              ]
            ]
          }
        },
        "MethodResponses": []
      },
      "DependsOn": "TestauthApiGatewayAuthorizer"
    },
    "ApiGatewayMethodFunc2Get": {
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "HttpMethod": "GET",
        "RequestParameters": {},
        "ResourceId": {
          "Ref": "ApiGatewayResourceFunc2"
        },
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "AuthorizationType": "CUSTOM",
        "AuthorizerId": {
          "Ref": "PseudoParamCustomAuthApiGatewayAuthorizer"
        },
        "Integration": {
          "IntegrationHttpMethod": "POST",
          "Type": "AWS_PROXY",
          "Uri": {
            "Fn::Join": [
              "",
              [
                "arn:aws:apigateway:",
                {
                  "Ref": "AWS::Region"
                },
                ":lambda:path/2015-03-31/functions/",
                {
                  "Fn::GetAtt": [
                    "Testfct1LambdaFunction",
                    "Arn"
                  ]
                },
                "/invocations"
              ]
            ]
          }
        },
        "MethodResponses": []
      },
      "DependsOn": "PseudoParamCustomAuthApiGatewayAuthorizer"
    },
    "TestauthApiGatewayAuthorizer": {
      "Type": "AWS::ApiGateway::Authorizer",
      "Properties": {
        "AuthorizerResultTtlInSeconds": 0,
        "IdentitySource": "method.request.header.Authorization",
        "Name": "testauth",
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "AuthorizerUri": {
          "Fn::Join": [
            "",
            [
              "arn:aws:apigateway:",
              {
                "Ref": "AWS::Region"
              },
              ":lambda:path/2015-03-31/functions/",
              "arn:aws:lambda:us-east-1:",
              {
                "Ref": "AWS::AccountId"
              },
              ":function:custom-auth",
              "/invocations"
            ]
          ]
        },
        "Type": "TOKEN"
      }
    },
    "PseudoParamCustomAuthApiGatewayAuthorizer": {
      "Type": "AWS::ApiGateway::Authorizer",
      "Properties": {
        "AuthorizerResultTtlInSeconds": 0,
        "IdentitySource": "method.request.header.Authorization",
        "Name": "testauth",
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "AuthorizerUri": {
          "Fn::Join": [
            "",
            [
              "arn:aws:apigateway:",
              {
                "Ref": "AWS::Region"
              },
              ":lambda:path/2015-03-31/functions/",
              {
                "Fn::Sub": "arn:aws:lambda:us-east-1:${AWS::AccountId}:function:custom-auth"
              },
              "/invocations"
            ]
          ]
        },
        "Type": "TOKEN"
      }
    },
    "TestauthApiGatewayRequestAuthorizer": {
      "Type": "AWS::ApiGateway::Authorizer",
      "Properties": {
        "AuthorizerResultTtlInSeconds": 0,
        "IdentitySource": "method.request.header.Authorization",
        "Name": "testauthrequest",
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "AuthorizerUri": {
          "Fn::Join": [
            "",
            [
              "arn:aws:apigateway:",
              {
                "Ref": "AWS::Region"
              },
              ":lambda:path/2015-03-31/functions/",
              {
                "Fn::GetAtt": [
                  "TestauthLambdaFunction",
                  "Arn"
                ]
              },
              "/invocations"
            ]
          ]
        },
        "Type": "REQUEST"
      }
    },
    "CognitoTestApiGatewayAuthorizer": {
      "Type": "AWS::ApiGateway::Authorizer",
      "Properties": {
        "AuthorizerResultTtlInSeconds": 0,
        "IdentitySource": "method.request.header.Authorization",
        "Name": "cognitoauth",
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "ProviderARNs": [
          "arn:aws:cognito-idp:us-west-2:xxxxx:userpool/us-west-xxxx"
        ],
        "Type": "COGNITO_USER_POOLS"
      }
    },
    "ApiGatewayDeployment1496754891256": {
      "Type": "AWS::ApiGateway::Deployment",
      "Properties": {
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "StageName": "dev"
      },
      "DependsOn": [
        "ApiGatewayMethodFunc1Get"
      ]
    },
    "Testfct1LambdaPermissionApiGateway": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Fn::GetAtt": [
            "Testfct1LambdaFunction",
            "Arn"
          ]
        },
        "Action": "lambda:InvokeFunction",
        "Principal": "apigateway.amazonaws.com",
        "SourceArn": {
          "Fn::Join": [
            "",
            [
              "arn:aws:execute-api:",
              {
                "Ref": "AWS::Region"
              },
              ":",
              {
                "Ref": "AWS::AccountId"
              },
              ":",
              {
                "Ref": "ApiGatewayRestApi"
              },
              "/*/*"
            ]
          ]
        }
      }
    },
    "pathmapping": {
      "Type": "AWS::ApiGateway::BasePathMapping",
      "Properties": {
        "BasePath": "(none)",
        "DomainName": "example.com", 
        "RestApiId": { "Fn::ImportValue": "ApiGatewayRestApi" },
        "Stage": "test"
      }
    },
    "TestauthLambdaPermissionApiGateway": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": "arn:aws:lambda:us-east-1:${AWS::AccountId}:function:custom-auth",
        "Action": "lambda:InvokeFunction",
        "Principal": "apigateway.amazonaws.com"
      }
    }
  },
  "Outputs": {
    "ServerlessDeploymentBucketName": {
      "Value": {
        "Ref": "ServerlessDeploymentBucket"
      }
    },
    "Testfct1LambdaFunctionQualifiedArn": {
      "Description": "Current Lambda function version",
      "Value": {
        "Ref": "Testfct1LambdaVersionrvZ7KY7UgzQ2OKbOvZoG1zLgodltc7toF3qYeORU"
      }
    },
    "TestauthLambdaFunctionQualifiedArn": {
      "Description": "Current Lambda function version",
      "Value": {
        "Ref": "TestauthLambdaVersionrvZ7KY7UgzQ2OKbOvZoG1zLgodltc7toF3qYeORU"
      }
    },
    "ServiceEndpoint": {
      "Description": "URL of the service endpoint",
      "Value": {
        "Fn::Join": [
          "",
          [
            "https://",
            {
              "Ref": "ApiGatewayRestApi"
            },
            ".execute-api.us-east-1.amazonaws.com/dev"
          ]
        ]
      }
    }
  }
}
