Last active
December 1, 2018 02:31
-
-
Save stympy/6961d5780ca31726e3edd90c5b5c35f8 to your computer and use it in GitHub Desktop.
Example project for using Ruby on AWS Lambda via the Serverless Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM lambci/lambda:build-ruby2.5 | |
WORKDIR /var/task | |
CMD bundle install --path vendor/bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk-dynamodb' | |
require 'aws-sdk-s3' | |
DDB_ClIENT = Aws::DynamoDB::Client.new | |
S3_CLIENT = Aws::S3::Client.new | |
def process(event:, context:) | |
event['Records'].each do |event_record| | |
JSON.parse(event_record['Sns']['Message'])['Records'].each do |record| | |
object = { bucket: record['s3']['bucket']['name'], key: record['s3']['object']['key'] } | |
begin | |
metadata = S3_CLIENT.head_object(object).metadata | |
rescue Aws::S3::Errors::Forbidden | |
metadata = {} | |
puts "Error while getting #{ object.inspect }" | |
end | |
resp = DDB_ClIENT.put_item({ | |
item: { | |
path: "#{ object[:bucket] }/#{ object[:key] }", | |
metadata: metadata | |
}, | |
table_name: ENV['TABLE_NAME'], | |
}) | |
puts "Stored #{ metadata.inspect } in item #{ object[:bucket] }/#{ object[:key] }" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service: log-s3-metadata | |
custom: | |
bucket: your-bucket-name | |
provider: | |
name: aws | |
runtime: ruby2.5 | |
iamRoleStatements: | |
- Effect: "Allow" | |
Action: | |
- "s3:ListBucket" | |
Resource: "arn:aws:s3:::${self:custom.bucket}" | |
- Effect: "Allow" | |
Action: | |
- "s3:HeadObject" | |
- "s3:GetObject" | |
Resource: "arn:aws:s3:::${self:custom.bucket}/*" | |
- Effect: "Allow" | |
Action: | |
- "dynamodb:PutItem" | |
Resource: | |
Fn::GetAtt: | |
- MetadataTable | |
- Arn | |
functions: | |
dynamo: | |
handler: handler.process | |
events: | |
- sns: arn:aws:sns:us-east-1:XXX:s3-creations | |
environment: | |
TABLE_NAME: !Ref MetadataTable | |
# you can add CloudFormation resource templates here | |
resources: | |
Resources: | |
MetadataTable: | |
Type: AWS::DynamoDB::Table | |
Properties: | |
BillingMode: PAY_PER_REQUEST | |
AttributeDefinitions: | |
- AttributeName: path | |
AttributeType: S | |
KeySchema: | |
- AttributeName: path | |
KeyType: HASH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment