amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

Add 'native' triggers for define storage

Open orgtom78 opened this issue 7 months ago • 6 comments

Environment information

System:
  OS: macOS 13.7.1
  CPU: (4) x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
  Shell: /bin/zsh
Binaries:
  Node: 21.5.0 - ~/.nvm/versions/node/v21.5.0/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 10.2.4 - ~/.nvm/versions/node/v21.5.0/bin/npm
  pnpm: 10.6.5 - ~/.../node_modules/.bin/pnpm
NPM Packages:
  @aws-amplify/ai-constructs: Not Found
  @aws-amplify/auth-construct: 1.7.0
  @aws-amplify/backend: 1.15.0
  @aws-amplify/backend-ai: Not Found
  @aws-amplify/backend-auth: 1.6.0
  @aws-amplify/backend-cli: 1.6.0
  @aws-amplify/backend-data: 1.5.0
  @aws-amplify/backend-deployer: 2.0.0
  @aws-amplify/backend-function: 1.13.0
  @aws-amplify/backend-output-schemas: 1.5.0
  @aws-amplify/backend-output-storage: 1.2.0
  @aws-amplify/backend-secret: 1.3.0
  @aws-amplify/backend-storage: 1.3.0
  @aws-amplify/cli-core: 2.0.0
  @aws-amplify/client-config: 1.6.0
  @aws-amplify/data-construct: 1.15.1
  @aws-amplify/data-schema: 1.20.1
  @aws-amplify/deployed-backend-client: 1.6.0
  @aws-amplify/form-generator: 1.1.0
  @aws-amplify/model-generator: 1.1.0
  @aws-amplify/platform-core: 1.7.0
  @aws-amplify/plugin-types: 1.9.0
  @aws-amplify/sandbox: 2.0.0
  @aws-amplify/schema-generator: 1.3.0
  aws-amplify: 6.13.6
  aws-cdk: 2.1006.0
  aws-cdk-lib: 2.185.0
  typescript: 5.5.4
No AWS environment variables
No CDK environment variables

Describe the feature

When configuring 2 buckets as follows:

import { defineFunction, defineStorage } from '@aws-amplify/backend'

export const storage = defineStorage({
  name: 'testdrive',
  isDefault: true,
  triggers: {
    onUpload: defineFunction({
      entry: '../functions/handler/handler.ts',
      resourceGroupName: 'storage'
    })
  },
  access: allow => ({
    'public/*': [allow.guest.to(['read', 'write', 'delete'])]
  })
})

export const outreachbucket = defineStorage({
  name: 'testdrive2',
  access: allow => ({
    'campaign/*': [
      allow.guest.to(['read', 'write', 'delete']),
      allow.entity('identity').to(['write', 'delete', 'read'])
    ]
  })
})

It deploys without any issues and as expected.

When defining triggers on both buckets as follows:

import { defineFunction, defineStorage, secret } from '@aws-amplify/backend'

export const storage = defineStorage({
  name: 'testdrive',
  isDefault: true,
  triggers: {
    onUpload: defineFunction({
      entry: '../functions/handler/handler.ts',
      resourceGroupName: 'storage',
    })
  },
  access: allow => ({
    'public/*': [allow.guest.to(['read', 'write', 'delete']), allow.entity('identity').to(['write', 'delete', 'read'])]
  })
})

export const outreachbucket = defineStorage({
  name: 'testdrive2',
  triggers: {
    onUpload: defineFunction({
      entry: '../functions/handler2/handler.ts',
      resourceGroupName: 'storage',
    })
  },
  access: allow => ({
    'campaign/*': [
      allow.guest.to(['read', 'write', 'delete']),
      allow.entity('identity').to(['write', 'delete', 'read'])
    ]
  })
})

We will run into:

9:35:09 AM [ERROR] [UnknownFault] Error: There is already a Construct with name 'handler' in NestedStack [storage]
9:35:09 AM [ERROR]   ∟ Caused by: [Error] There is already a Construct with name 'handler' in NestedStack [storage]

Use case

Not crucial since it works when following the alternative approach as outlined here: https://docs.amplify.aws/react/build-a-backend/storage/lambda-triggers/

orgtom78 avatar Apr 08 '25 07:04 orgtom78