Skip to content

Instantly share code, notes, and snippets.

@parkeyparker
Created December 1, 2021 09:08
Show Gist options
  • Save parkeyparker/2fe24488de22f923e6cf3e64a7c1f9be to your computer and use it in GitHub Desktop.
Save parkeyparker/2fe24488de22f923e6cf3e64a7c1f9be to your computer and use it in GitHub Desktop.
HttpApi Authoriser CloudFormation
ApiGateway:
Type: AWS::Serverless::HttpApi
Properties:
StageName: '$default'
Description: 'API'
Auth:
Authorizers:
LambdaAuthorizer:
AuthorizerPayloadFormatVersion: "1.0"
FunctionArn:
Fn::GetAtt:
- JwtAuthorizerLambda
- Arn
Identity:
Headers:
- Authorization
ReauthorizeEvery: 30
DefaultAuthorizer: LambdaAuthorizer
AccessLogSettings:
DestinationArn:
Fn::GetAtt:
- ApiGatewayLogGroup
- Arn
Format: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","routeKey":"$context.routeKey", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength", "authorizerError":"$context.authorizer.error", "authorizerPrincipalId":"$context.authorizer.principalId", "integrationError":"$context.integration.error", "errorMessage":"$context.error.message" }'
CorsConfiguration:
AllowCredentials: true
AllowHeaders:
- Authorization
- Content-Type
AllowOrigins: !Ref CORSOrigins
AllowMethods:
- GET
- POST
- OPTIONS
MaxAge: 600
ApiGatewayCORSOptionsRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref ApiGateway
RouteKey: 'OPTIONS /{proxy+}'
AuthorizationType: NONE
JwtAuthorizerLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "api-jwtAuthorizer-${Environment}"
CodeUri: dist/api
Handler: lambda.doAuthorize
Runtime: nodejs14.x
Timeout: 5
Policies:
- DynamoDBReadPolicy:
TableName: !Ref UsersDynamoDBTable
Environment:
Variables:
NODE_ENV: !Ref Environment
DDB_TABLE_NAME: !Ref UsersDynamoDBTable
JWT_SECRET: !Ref JWTSecret
JwtAuthorizerLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !Ref JwtAuthorizerLambda
Principal: apigateway.amazonaws.com
SourceArn: !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiGateway}/authorizers/*'
ApiDoLoginLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "api-doLogin-${Environment}"
CodeUri: dist/api
Handler: lambda.doLogin
Runtime: nodejs14.x
Timeout: 5
Events:
doLogin:
Type: HttpApi
Properties:
ApiId: !Ref ApiGateway
Method: POST
Path: /api/login
Auth:
Authorizer: NONE
Policies:
- DynamoDBReadPolicy:
TableName: !Ref UsersDynamoDBTable
Environment:
Variables:
NODE_ENV: !Ref Environment
DDB_TABLE_NAME: !Ref UsersDynamoDBTable
JWT_SECRET: !Ref JWTSecret
ApiGetDataLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "api-getData-${Environment}"
CodeUri: dist/api
Handler: lambda.getData
Runtime: nodejs14.x
Timeout: 10
Events:
getChatLink:
Type: HttpApi
Properties:
ApiId: !Ref ApiGateway
Method: GET
Path: /api/data
Policies:
- DynamoDBReadPolicy:
TableName: !Ref UsersDynamoDBTable
Environment:
Variables:
NODE_ENV: !Ref Environment
DDB_TABLE_NAME: !Ref UsersDynamoDBTable
JWT_SECRET: !Ref JWTSecret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment