I am currently learning AWS Serverless Application Model (SAM).
With that objective, I created a simple template that is deploying a Python Lambda function and an API which calls this function. My template is the following :
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
a test with AWS SAM
Globals:
Function:
Timeout: 10
Api:
OpenApiVersion: 3.0.1
Resources:
HelloAPI:
Type: AWS::Serverless::Api
Properties:
StageName: dev
Auth:
ApiKeyRequired: true
UsagePlan:
CreateUsagePlan: PER_API
Description: Usage plan for this API
HelloFunction:
Properties:
CodeUri: some/path/to/hello_function
Handler: hello.lambda_handler
Runtime: python3.8
Events:
Hello:
Type: Api
Properties:
RestApiId: !Ref HelloAPI
Path: /
Method: post
Outputs:
ProdDataEndpoint:
Description: "API dev stage endpoint"
Value: !Sub "https://${HelloAPI}.execute-api.${AWS::Region}.amazonaws.com/dev/"
So the template creates a UsagePlan with an associated API key. I checked within AWS, the ressources are indeed created correcly.
I can test the API locally with :
sam local start-api
sam local generate-event apigateway aws-proxy > local-event.json
curl -d "@local-event.json" -X POST http://127.0.0.1:3000/
And this works. But I don't know how to test it with the API key to check if the access is indeed protected by the key. Does anyone know how to do that?
Thanks !