Skip to content

Instantly share code, notes, and snippets.

@rjozefowicz
Last active October 30, 2024 10:43
Show Gist options
  • Save rjozefowicz/db6776af6821e7b92b5532a6202b48a6 to your computer and use it in GitHub Desktop.
Save rjozefowicz/db6776af6821e7b92b5532a6202b48a6 to your computer and use it in GitHub Desktop.
ZETO - FaaS szkolenie

Zeto - FaaS szkolenie 06.06.2022

Aplikacja my-notes

{
  "labels": [
    "Building",
    "Cottage",
    "Countryside",
    "House",
    "Housing",
    "Nature",
    "Outdoors",
    "Person",
    "Roof",
    "Shelter"
  ],
  "noteId": "e7466188-c0e9-4cd4-b9fc-533561c25498",
  "s3Location": "fd3be9de-bd0d-44c9-85e5-bf24a4c5503d/e7466188-c0e9-4cd4-b9fc-533561c25498/krzykowdom.jpg",
  "size": "123165",
  "timestamp": "1563820937739",
  "title": "krzykowdom.jpg",
  "text": "my note text",
  "type": "IMAGE",
  "userId": "fd3be9de-bd0d-44c9-85e5-bf24a4c5503d"
}

Architektura

alt text

File upload

alt text

File download

alt text

@rjozefowicz
Copy link
Author

aws lambda list-functions
aws lambda invoke --function-name welcome-stacja-it /dev/stdout
aws lambda invoke --function-name welcome-stacja-it nazwa_pliku
aws iam get-user
aws s3 ls / aws s3api list-buckets
aws s3api list-objects --bucket rj-stacja-it-demo
aws s3api get-object --bucket rj-stacja-it-demo --key my-notes.zip my-notes.zip
aws lambda invoke --function-name uuid-generator /dev/stdout --payload '{"prefix":"lbj"}' --cli-binary-format raw-in-base64-out

@rjozefowicz
Copy link
Author

serverless create --template aws-nodejs --name uuid-generator

serverless invoke local --function hello

@rjozefowicz
Copy link
Author

rjozefowicz commented Jun 6, 2022

UUID-generator:

handler.js

'use strict';

const { v4 } = require("uuid");

module.exports.uuidGenerator = async (event) => {
  return v4();
};

serverless.yml

service: uuid-generator
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-west-1

functions:
  uuidGenerator:
    handler: handler.uuidGenerator
    events:
    - http:
        method: get
        path: /uuid
        cors: true

@rjozefowicz
Copy link
Author

const { v4 } = require("uuid");
const AWS = require("aws-sdk");
const documentClient = new AWS.DynamoDB.DocumentClient();

module.exports.func = async(event, context) => {

    const note = JSON.parse(event.body);
    note.userId = "23";
    note.noteId = v4();

    await documentClient.put({
        Item: note,
        TableName: process.env.TABLE_NAME
    }).promise();

    return response(200);
};

@rjozefowicz
Copy link
Author

my-notes table

resources:
  Resources:
    MyNotesTable: 
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: my-notes-${opt:stage}
        AttributeDefinitions:
        - AttributeName: userId
          AttributeType: S
        - AttributeName: noteId
          AttributeType: S
        KeySchema:
        - AttributeName: userId
          KeyType: HASH
        - AttributeName: noteId
          KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

@rjozefowicz
Copy link
Author

list-notes:
handler.js

const AWS = require("aws-sdk");
const documentClient = new AWS.DynamoDB.DocumentClient();

module.exports.func = async (event, context) => {

    const notes = await documentClient.query({
        TableName: process.env.TABLE_NAME,
        KeyConditionExpression: "userId = :userId",
        ExpressionAttributeValues: {
            ":userId": "23"
        }
    }).promise();

    return response(200, {
        elements: notes.Items
    });
};

serverless.yml

functions:
  list-notes:
    handler: functions/list-notes/handler.func
    role: ${self:custom.functionRoleArn}
    environment:
      TABLE_NAME: my-notes-${opt:stage}
    events:
      - http:
          path: notes
          method: get
          cors: true
    package:
      individually: true
      patterns:
      - '!./**'
      - functions/list-notes/**

@rjozefowicz
Copy link
Author

static website hosting

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::my-notes-website-<PREFIX>/*"
        }
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment