Skip to content

Instantly share code, notes, and snippets.

@yai333
Created December 22, 2019 03:56
Show Gist options
  • Save yai333/79cb02c0b21aca2fef92e8195b6b220e to your computer and use it in GitHub Desktop.
Save yai333/79cb02c0b21aca2fef92e8195b6b220e to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 60
Parameters:
Stage:
Type: String
Default: dev
BucketName:
Type: String
Default: aiyi.demo.textract
Resources:
TextractSNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: !Sub "textract-sns-topic"
TopicName: !Sub "textract-sns-topic"
Subscription:
- Protocol: lambda
Endpoint: !GetAtt TextractEndFunction.Arn
TextractSNSTopicPolicy:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref TextractEndFunction
Principal: sns.amazonaws.com
Action: lambda:InvokeFunction
SourceArn: !Ref TextractSNSTopic
TextractEndFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: handler.textractEndHandler
Runtime: nodejs12.x
Role: !GetAtt TextractRole.Arn
Policies:
- AWSLambdaExecute
- Statement:
- Effect: Allow
Action:
- "s3:PutObject"
Resource: !Join [":", ["arn:aws:s3::", !Ref BucketName]]
TextractStartFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
TEXT_EXTRACT_ROLE: !GetAtt TextractRole.Arn
SNS_TOPIC: !Ref TextractSNSTopic
Role: !GetAtt TextractRole.Arn
CodeUri: src/
Handler: handler.textractStartHandler
Runtime: nodejs12.x
Events:
PDFUploadEvent:
Type: S3
Properties:
Bucket: !Ref S3Bucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: ".pdf"
TextractRole:
Type: AWS::IAM::Role
Properties:
RoleName: "TextractRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "textract.amazonaws.com"
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/AWSLambdaExecute"
Policies:
- PolicyName: "TextractRoleAccess"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "sns:*"
Resource: "*"
- Effect: Allow
Action:
- "textract:*"
Resource: "*"
GetTextractResult:
Type: AWS::Serverless::Function
Properties:
Role: !GetAtt TextractRole.Arn
CodeUri: src/
Handler: handler.getTextractResult
Runtime: nodejs12.x
Events:
TextExactStart:
Type: HttpApi
Properties:
Path: /textract
Method: post
MyHttpApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: !Ref Stage
Cors:
AllowMethods: "'OPTIONS,POST,GET'"
AllowHeaders: "'Content-Type'"
AllowOrigin: "'*'"
S3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment