mikaelvesavuori/catalogist

View on GitHub
serverless.yml

Summary

Maintainability
Test Coverage
service: catalogist

provider:
  name: aws
  runtime: nodejs18.x
  architecture: arm64
  stage: ${opt:stage, 'prod'}
  region:  ${opt:region, 'eu-north-1'}
  memorySize: ${opt:memory, 1024}
  timeout: ${opt:timeout, 10}
  logRetentionInDays: ${opt:logRetentionInDays, 7}
  versionFunctions: false
  httpApi:
    cors: false
    disableDefaultEndpoint: true
    authorizers:
      Authorizer:
        functionName: Authorizer
        resultTtlInSeconds: ${self:custom.config.aws.apiGatewayCachingTtl.${self:provider.stage}, '0'}
        identitySource:
          - $request.header.Authorization
        type: request
        enableSimpleResponses: true
  deploymentBucket:
    blockPublicAccess: true
    maxPreviousDeploymentArtifacts: 5
    serverSideEncryption: AES256
  stackTags:
    Usage: ${self:service}
  tags:
    Usage: ${self:service}
  apiGateway:
    minimumCompressionSize: 1024

plugins:
  - serverless-esbuild
  - serverless-offline
  - serverless-iam-roles-per-function

package:
  individually: true

custom:
  config:
    apiKey: ${opt:apiKey, 'jdhop8dyn98aHJGa873hljajs'} # Add your desired valid API key here or use the default
    awsAccountNumber: ${opt:awsAccountNumber, '123412341234'} # Set this to your value if you want to use a fallback value
    tableName: ${self:service}-${self:provider.stage}
  aws:
    databaseArn: arn:aws:dynamodb:${aws:region}:${self:custom.config.awsAccountNumber}:table/${self:custom.config.tableName}
    apiGatewayCachingTtl:
      prod: 30
      dev: 0
      test: 0
    apiGatewayCachingTtlValue: ${self:custom.aws.apiGatewayCachingTtl.${self:provider.stage}, self:custom.aws.apiGatewayCachingTtl.test} # See: https://forum.serverless.com/t/api-gateway-custom-authorizer-caching-problems/4695
  esbuild:
    bundle: true
    minify: true

functions:
  Authorizer:
    handler: src/infrastructure/authorizers/Authorizer.handler
    description: ${self:service} authorizer
    environment:
      API_KEY: ${self:custom.config.apiKey}
  CreateRecord:
    handler: src/infrastructure/adapters/web/CreateRecord.handler
    description: Create record
    events:
      - httpApi:
          method: POST
          path: /record
          authorizer:
            name: Authorizer
    iamRoleStatements:
      - Effect: "Allow"
        Action:
          - dynamodb:PutItem
        Resource: ${self:custom.aws.databaseArn}
    environment:
      REGION: ${aws:region}
      TABLE_NAME: ${self:custom.config.tableName}
  GetRecord:
    handler: src/infrastructure/adapters/web/GetRecord.handler
    description: Get record
    events:
      - httpApi:
          method: GET
          path: /record
          authorizer:
            name: Authorizer
    iamRoleStatements:
      - Effect: "Allow"
        Action:
          - dynamodb:Query
        Resource: ${self:custom.aws.databaseArn}
    environment:
      REGION: ${aws:region}
      TABLE_NAME: ${self:custom.config.tableName}

resources:
  Resources:
    # DynamoDB configuration
    CatalogistTable:
      Type: AWS::DynamoDB::Table
      DeletionPolicy: Retain
      UpdateReplacePolicy: Retain
      Properties:
        TableName: ${self:custom.config.tableName}
        AttributeDefinitions:
          - AttributeName: pk
            AttributeType: S
          - AttributeName: sk
            AttributeType: S
        KeySchema:
          - AttributeName: pk
            KeyType: HASH
          - AttributeName: sk
            KeyType: RANGE
        TimeToLiveSpecification:
          AttributeName: expiresAt
          Enabled: true
        BillingMode: PAY_PER_REQUEST
        PointInTimeRecoverySpecification:
          PointInTimeRecoveryEnabled: true