|
| 1 | +# Amazon API Gateway API using Lambda Token Authorizer and Mapping Template to add custom header |
| 2 | + |
| 3 | +The SAM template deploys an Amazon API Gateway API endpoint that uses a Lambda Token Authorizer for access control as well as added logic in the Authorizer function to enrich the request with additional data. |
| 4 | + |
| 5 | +This aproach can be used to inject data that downstream legacy systems expect and that is dependent on data that is available to authorizer. |
| 6 | + |
| 7 | +* If the request to the endpoint does not include a 'authorizationToken' header, the Lambda Authorizer will not be invoked and API Gateway will return a 401 Forbidden. |
| 8 | +* If the request to the endpoint includes a 'authorizationToken' header, the Lambda Authorizer will be invoked and its response will depend on the value of the 'authorizationToken' header. |
| 9 | +* If the value of 'authorizationToken' header is 'unauthorized', API Gateway will return a 401 Unauthorized error. |
| 10 | +* If the value of 'authorizationToken' header is 'deny', API Gateway will return a 403 error. |
| 11 | +* Only if the value of 'authorizationToken' header is 'allow', API Gateway will successfully call the HTTP backend and return a 200. |
| 12 | +* For any other case, API Gateway will return a 403 error. |
| 13 | + |
| 14 | +TODO: Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/apigw-lambda-authorizer](https://serverlessland.com/patterns/apigw-lambda-authorizer) |
| 15 | + |
| 16 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 21 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 22 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 23 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 24 | + |
| 25 | +## Deployment Instructions |
| 26 | + |
| 27 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 28 | + ``` |
| 29 | + git clone https://github.com/aws-samples/serverless-patterns |
| 30 | + ``` |
| 31 | +2. Change directory to the pattern directory: |
| 32 | + ``` |
| 33 | + TODO cd apigw-lambda-authorizer |
| 34 | + ``` |
| 35 | +3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 36 | + ``` |
| 37 | + sam deploy -g |
| 38 | + ``` |
| 39 | +1. During the prompts: |
| 40 | + * Enter a stack name |
| 41 | + * Select the desired AWS Region |
| 42 | + * Allow SAM to create roles with the required permissions if needed. |
| 43 | +
|
| 44 | + Once you have run guided mode once, you can use `sam deploy` in future to use these defaults. |
| 45 | +
|
| 46 | +1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 47 | +
|
| 48 | +## Testing |
| 49 | +
|
| 50 | +The stack will output the **api endpoint**. Use *curl* to make a HTTP request to the API Gateway that includes a header with the authorization token to test the Resource Lambda Token Authorizer. |
| 51 | + |
| 52 | +``` |
| 53 | +curl -i https://12345abcde.execute-api.{region}.amazonaws.com/Prod -H "authorizationToken: allow" |
| 54 | +``` |
| 55 | +
|
| 56 | +will successfully return a 200 HTTP code and the request from API Gateway to the HTTP endpoint in the response body. You should see a header named `enrichmentHeader` with a string provided from the Lambda Token Authorizer `This data comes from Lambda Authorizer` |
| 57 | +
|
| 58 | +
|
| 59 | +## Cleanup |
| 60 | + |
| 61 | +1. Delete the stack |
| 62 | + ```bash |
| 63 | + sam delete |
| 64 | + ``` |
| 65 | +1. Confirm the stack has been deleted |
| 66 | + ```bash |
| 67 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" |
| 68 | + ``` |
| 69 | +---- |
| 70 | +
|
| 71 | +## Author bio |
| 72 | +Shaun Guo |
| 73 | +https://www.linkedin.com/in/shaun-guo/ |
| 74 | +Senior Technical Account Manager |
0 commit comments