API key example for AWS SAM
You can control access to your APIs by requiring API keys within your AWS SAM template. To do this, you use the ApiAuth data type.
The following is an example AWS SAM template section for API keys:
Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: ApiKeyRequired: true # sets for all methods MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: . Handler: index.handler Runtime: nodejs12.x Events: ApiKey: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get Auth: ApiKeyRequired: true
For more information about API keys, see Creating and using usage plans with API keys in the API Gateway Developer Guide.