aws / aws/serverless-application-model

Function specific ApiKeyRequired Auth property does not override the global API default

Open
#1,514 6 comments 0 reactions 0 assignees View on GitHub
area/resource/api maintainer/need-followup stage/needs-feedback type/feature
Dominant language
Python
Stars
9.6k
Forks
2.5k
Avg merge
1d 11h
Merged PRs (30d)
7

Description

The [documentation](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#function-auth-object) states that if you have specified `ApiKeyRequired: true` globally on the API and want to make a specific Function public, you can override it with the following in the Function's event source:
```Auth:
ApiKeyRequired: false
```
However, this does not seem to function as stated.

**Steps to reproduce the issue:**
Here's the template I used.
```
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app

Sample SAM Template for sam-app

Globals:
Function:
Timeout: 3
Runtime: nodejs12.x

Resources:
MyAPI:
Type: AWS::Serverless::Api
Properties:
Name: MyAPI
StageName: Default
EndpointConfiguration: REGIONAL
Auth:
ApiKeyRequired: true
DefinitionBody:
openapi: 3.0.0
x-amazon-apigateway-api-key-source: "HEADER"
paths:
/public:
get:
x-amazon-apigateway-integration:
type: aws_proxy
httpMethod: POST
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PublicFunction.Arn}/invocations
/private:
get:
x-amazon-apigateway-integration:
type: aws_proxy
httpMethod: POST
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PrivateFunction.Arn}/invocations

PublicFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PublicFunction
Role: !GetAtt LambdaRole.Arn
Handler: src/public.handler
Events:
API:
Type: Api
Properties:
Path: /public
Method: ANY
RestApiId:
Ref: MyAPI
Auth:
ApiKeyRequired: false

PrivateFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PrivateFunction
Role: !GetAtt LambdaRole.Arn
Handler: src/private.handler
Events:
API:
Type: Api
Properties:
Path: /private
Method: ANY
RestApiId:
Ref: MyAPI

LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

ApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Name: !Join [ "-", [ MyAPI, "ApiKey" ]]
Description: "APIkey"
Enabled: true
GenerateDistinctId: false

ApiUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn: MyAPIDefaultStage
Properties:
ApiStages:
- ApiId: !Ref MyAPI
Stage: Default
Description: !Join [ "-", [ MyAPI, "UsagePlan" ] ]
UsagePlanName: !Join [ "-", [ MyAPI, "UsagePlan" ] ]

ApiUsagePlanKey:
Type: AWS::ApiGateway::UsagePlanKey
Properties:
KeyId: !Ref ApiKey
KeyType: API_KEY
UsagePlanId: !Ref ApiUsagePlan

Outputs:

APi:
Description: "API Gateway endpoint URL"
Value: !Sub "https://${MyAPI}.execute-api.${AWS::Region}.amazonaws.com/Default/public"
```
#
**Observed result:**
API Key Still required

#
**Expected result:**
The `/public` route should have API Key Required = `False`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.