Skip to content

Commit

Permalink
finished PoC lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
t04glovern committed Aug 16, 2019
1 parent 025d268 commit 8b71818
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.venv/
.vscode/
1 change: 1 addition & 0 deletions image-handler/.gitignore
Expand Up @@ -17,6 +17,7 @@ var/
*.egg
node_modules/
.venv/
__pycache__/

# Serverless directories
.serverless
3 changes: 3 additions & 0 deletions image-handler/README.md
Expand Up @@ -12,6 +12,7 @@ serverless config credentials --provider aws --key <ACCESS KEY ID> --secret <SEC
```bash
serverless plugin install -n serverless-python-requirements
serverless plugin install -n serverless-domain-manager
serverless plugin install -n serverless-pseudo-parameters
```

Add the following to the `serveress.yml` file
Expand All @@ -20,6 +21,7 @@ Add the following to the `serveress.yml` file
plugins:
- serverless-python-requirements
- serverless-domain-manager
- serverless-pseudo-parameters

custom:
pythonRequirements:
Expand Down Expand Up @@ -56,3 +58,4 @@ serverless invoke -f selfie --path test_data.json

* [How to set up a custom domain name for Lambda & API Gateway with Serverless](https://serverless.com/blog/serverless-api-gateway-domain/)
* [Custom domain in AWS API Gateway](https://medium.com/@maciejtreder/custom-domain-in-aws-api-gateway-a2b7feaf9c74)
* [maciejtreder/serverless-apigw-binary](https://github.com/maciejtreder/serverless-apigw-binary)
54 changes: 37 additions & 17 deletions image-handler/handler.py
@@ -1,26 +1,46 @@
import json
import base64
import boto3
import os
import time

bucket_name = os.environ['BUCKET_NAME']
queue_name = os.environ['QUEUE_NAME']


def selfie(event, context):
body = {
"message": "Go Serverless v1.0! Your function executed successfully!",
"input": event
}
body = event['body']
email = body['email']
image = base64.b64decode(body['image'])

# Create S3 client and store image
s3 = boto3.client('s3')
file_name = 'archive/image-'+time.strftime("%Y%m%d-%H%M%S")+'.jpg'
response = s3.put_object(Body=image, Bucket=bucket_name, Key=file_name)

# Create SQS client and send data for processing
sqs = boto3.client('sqs')
response = sqs.get_queue_url(QueueName=queue_name)
queue_url = response['QueueUrl']

# Send message to SQS queue
response = sqs.send_message(QueueUrl=queue_url, MessageBody=body['image'], MessageAttributes={
'Email': {
'StringValue': email,
'DataType': 'String'
}
})

response = {
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
},
"statusCode": 200,
"body": json.dumps(body)
"body": {
"image": "https://s3.amazonaws.com/" + bucket_name + "/" + file_name,
"message_md5": response['MD5OfMessageBody'],
"email": email
}
}

print(event)

return response

# Use this code if you don't use the http event with the LAMBDA-PROXY
# integration
"""
return {
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": event
}
"""
6 changes: 6 additions & 0 deletions image-handler/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions image-handler/package.json
Expand Up @@ -5,6 +5,7 @@
"dependencies": {},
"devDependencies": {
"serverless-domain-manager": "^3.3.0",
"serverless-pseudo-parameters": "^2.4.0",
"serverless-python-requirements": "^5.0.0"
}
}
3 changes: 2 additions & 1 deletion image-handler/requirements.txt
@@ -1 +1,2 @@
boto3
boto3
Pillow
35 changes: 34 additions & 1 deletion image-handler/serverless.yml
Expand Up @@ -3,6 +3,7 @@ service: selfie2anime-image-handler
plugins:
- serverless-python-requirements
- serverless-domain-manager
- serverless-pseudo-parameters

custom:
pythonRequirements:
Expand All @@ -13,6 +14,9 @@ custom:
basePath: analysis
stage: ${self:provider.stage}
createRoute53Record: true
environment:
BUCKET_NAME: selfie2anime
QUEUE_NAME: selfie2anime

package:
exclude:
Expand All @@ -24,11 +28,40 @@ provider:
runtime: python3.7
stage: dev
region: us-east-1
iamRoleStatements:
- Effect: Allow
Action:
- s3:PutObject
- s3:PutObjectAcl
- s3:GetObject"
- s3:DeleteObject
Resource:
- "arn:aws:s3:::${self:custom.environment.BUCKET_NAME}/*"
- Effect: Allow
Action:
- sqs:SendMessage
Resource:
- arn:aws:sqs:::${self:custom.environment.QUEUE_NAME}
environment:
BUCKET_NAME: ${self:custom.environment.BUCKET_NAME}
QUEUE_NAME: ${self:custom.environment.QUEUE_NAME}

functions:
selfie:
handler: handler.selfie
events:
- http:
path: selfie
method: post
method: post
cors: true

resources:
Resources:
S3Bucketselfie2anime:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.environment.BUCKET_NAME}
Queueselfie2anime:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.environment.QUEUE_NAME}
6 changes: 4 additions & 2 deletions image-handler/test_data.json

Large diffs are not rendered by default.

0 comments on commit 8b71818

Please sign in to comment.