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
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
This post is so interactive and informative.keep updating more information...
ReplyDeleteAWS Training In Mumbai
AWS Training In Ahmedabad
AWS Training In Kochi
AWS Training In Trivandrum
AWS Training In Kolkata
Awesome Post!!! Thanks for sharing this great post with us.
ReplyDeleteWhy is Java Popular?
How Popular is Java?
ReplyDeleteThanks for taking your valuable time to share this awesome article with us. This is really informative. Looking forward to learn more from you.
Cloud Computing Training in Chennai
Cloud Computing Online Training
Cloud Computing Course in Coimbatore