title: How to attach authorization/api key to the sam cli generated api?
body: <p>I used sam cli , to create a project. when I package this and deploy , it creates the lambda and also the api gateway with stage and prod stages, policy, roles e.t.c by default, without having to explicitly define in the cloudformation template ( see code below) . as it generates the api gateway automatically, how do i add/attach say if i wanted to add a api key or some kind of authorization for my api generated by template below?</p>
  <pre><code>AWSTemplateFormatVersion: '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  Description: &gt;
    simple-node-api
    Sample SAM Template for simple-node-api
  
  Globals:
    Function:
      Timeout: 3
  
  Resources:
   ServerlessHttpApi:
      Type: AWS::Serverless::Api
      Properties:
        StageName: Prod
        Auth:
          ApiKeyRequired: true # sets for all methods
        DefinitionBody:
          swagger:2.0
          paths:
            &quot;/myresource&quot;:
                post:
                   x-amazon-apigateway-integration
                      httpMethod: post
                      type: aws_proxy
                      uri: ...
  
   ApiKey: 
      Type: AWS::ApiGateway::ApiKey
      Properties: 
        Name: !Join [&quot;&quot;, [{&quot;Ref&quot;: &quot;AWS::StackName&quot;}, &quot;-apikey&quot;]]
        Description: &quot;CloudFormation API Key V1&quot;
        Enabled: true
        GenerateDistinctId: false
        Value: abcdefg123456
        StageKeys:
          - RestApiId: !Ref ServerlessHttpApi
            StageName: Prod
  
    ApiUsagePlan:
      Type: &quot;AWS::ApiGateway::UsagePlan&quot;
      Properties:
        ApiStages: 
          - ApiId: !Ref ServerlessHttpApi
            Stage: Prod
        Description: !Join [&quot; &quot;, [{&quot;Ref&quot;: &quot;AWS::StackName&quot;}, &quot;usage plan&quot;]]
        Quota:
          Limit: 1000
          Period: MONTH
        UsagePlanName: !Join [&quot;&quot;, [{&quot;Ref&quot;: &quot;AWS::StackName&quot;}, &quot;-usage-plan&quot;]]
  
    ApiUsagePlanKey:
      Type: &quot;AWS::ApiGateway::UsagePlanKey&quot;
      DependsOn: 
        - ServerlessHttpApi
      Properties:
        KeyId: !Ref ApiKey
        KeyType: API_KEY
        UsagePlanId: !Ref ApiUsagePlan
  
    HelloWorldfunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: hello-world/
        Handler: app.lambdaHandler
        Runtime: python3.7
        Events:
          HelloWorld:
            Type: Api
            Properties:
              RestApiId: !Ref ServerlessHttpApi
              Path: /hello
              Method: get
  
  Outputs:
    ServerlessHttpApi:
      Description: API Gateway endpoint URL for Prod stage for Hello World function
      Value:
        Fn::Sub: https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/&quot;
    HelloWorldfunction:
      Description: Express Backend Lambda Function ARN
      Value: !Sub HelloWorldfunction.Arn
    HelloWorldFunctionIamRole:
      Description: Implicit IAM Role created for Hello World function
      Value: !Sub HelloWorldFunctionRole.Arn
  </code></pre>