# Creating Lambda authorizer for HTTP API?

**URL:** <https://forum.serverless.com/t/creating-lambda-authorizer-for-http-api/12602>\
**Category:** Serverless Framework\
**Created:** [September 20, 2020, 3:27am UTC](https://forum.serverless.com/t/creating-lambda-authorizer-for-http-api/12602 "2020-09-20T03:27:23Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![sgyyz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/sgyyz/32/3981_2.png) [@sgyyz](https://forum.serverless.com/u/sgyyz)\
**Post date:** [September 27, 2020, 1:28am UTC](https://forum.serverless.com/t/creating-lambda-authorizer-for-http-api/12602/2 "2020-09-27T01:28:37Z")

</div>

You can setup it through the AWS console UI, or you can create your own authorizer function. Like:

`serverless.yml`

```auto
functions:
   hello:
     handler: handler.hello
     events:
     - http:
        path: hello
        method: get
        cors: true
        authorizer:
          type: TOKEN
          name: authorizerFun
          identitySource: method.request.header.Authorization
          resultTtlInSeconds: 0

authorizerFun:
  handler: authorizer.authorizerFun

```

`authorizer.authorizerFun`

```auto
const generatePolicy = (user, effect, resource) => {
   const authResponse = {
   principalId: user.id || 'anonymous'
 };

 if (effect && resource) {
   const policyDocument = {
     Version: '2012-10-17',
     Statement: [
       {
         Action: 'execute-api:Invoke',
         Effect: effect,
         Resource: resource
       }
     ]
   };

   authResponse.policyDocument = policyDocument;
 }

authResponse.context = {
  role: user.role
};
return authResponse; };

export const authorizerFun = (event, context) => {
 const authorizationToken = event.authorizationToken;

 console.log(authorizationToken);

 switch (authorizationToken) {
   case 'manager':
     context.succeed(generatePolicy({ id: 1, role: 'MANAGER' }, 'Allow', '*'));
     break;
   case 'tenant':
     context.succeed(generatePolicy({ id: 2, role: 'TENANT' }, 'Allow', '*'));
     break;
   default:
     context.fail('error');
 }
};

```

> [@Custom Authorizer AccessDeniedException](http://forum.serverless.com/t/custom-authorizer-accessdeniedexception/10548/2):
>
> @sgyyz there is a typo in the authorizer that you posted; it says exectue-api:Invoke instead of execute-api:Invoke. What happens when you fix that?

---

_[View the full topic](https://forum.serverless.com/t/creating-lambda-authorizer-for-http-api/12602)._
