Custom authorizer

Creating custom authorizer using SAM


AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  Api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: devtesting
      DefinitionBody:
        swagger: "2.0"
        info:
          title:
            Ref: AWS::StackName
        description: My API that uses custom authorizer
        version: 1.0.0
        paths:
          "/getmesomething":
            get:
              x-amazon-apigateway-integration:
                httpMethod: GET
                type: aws_proxy
                uri:
                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambdaFunction.Arn}/invocations
                responses: {}
              security:
                - CustomAuthorizer: []
        securityDefinitions:
          CustomAuthorizer:
            type: apiKey
            name: Authorization
            in: header
            x-amazon-apigateway-authtype: custom
            x-amazon-apigateway-authorizer:
              type: token
              authorizerUri:
                Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${CustomAuthorizerFunction.Arn}/invocations
              authorizerCredentials:
                Fn::Sub: ${ApiGatewayAuthorizerRole.Arn}
              authorizerResultTtlInSeconds: 60
  MyLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: simpleIndex.handler
      Runtime: nodejs6.10
      Role: "arn:aws:iam::203837324023:role/lambda_basic_execution"
      CodeUri: s3://authorizerbucket/simpleIndex.zip
      Events:
        GetApi:
          Type: Api
          Properties:
            Path: /get
            Method: GET
            RestApiId:
                Ref: Api
  CustomAuthorizerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: simpleIndex.handler
      Runtime: nodejs6.10
      Role: "arn:aws:iam::203837324023:role/lambda_basic_execution"
      CodeUri: s3://authorizerbucket/simpleIndex.zip
  ApiGatewayAuthorizerRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: "Allow"
            Principal:
              Service:
                - "apigateway.amazonaws.com"
            Action:
              - sts:AssumeRole
      Policies:
        -
          PolicyName: "InvokeAuthorizerFunction"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              -
                Effect: "Allow"
                Action:
                  - lambda:InvokeAsync
                  - lambda:InvokeFunction
                Resource:
                  Fn::Sub: ${CustomAuthorizerFunction.Arn}
  CustomAuthorizerFunctionRole:
    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/AWSLambdaFullAccess

Comments

Post a Comment

Popular posts from this blog

Integrate an API with lambda - Part 5

Integrate an API with lambda - Part 2