aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
169 stars 56 forks source link

amplify.yml backend phase in monorepo #2095

Open scott-elgaard opened 3 days ago

scott-elgaard commented 3 days ago

Environment information

System:
  OS: Windows 11 10.0.22631
  CPU: (16) x64 13th Gen Intel(R) Core(TM) i7-13620H
  Memory: 2.14 GB / 31.68 GB
Binaries:
  Node: 20.14.0 - C:\Program Files\nodejs\node.EXE
  Yarn: undefined - undefined
  npm: 10.8.1 - C:\Program Files\nodejs\npm.CMD
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.0
  @aws-amplify/backend: 1.2.1
  @aws-amplify/backend-auth: 1.1.3
  @aws-amplify/backend-cli: 1.2.5
  @aws-amplify/backend-data: 1.1.3
  @aws-amplify/backend-deployer: 1.1.0
  @aws-amplify/backend-function: 1.4.0
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.1
  @aws-amplify/backend-secret: 1.1.1
  @aws-amplify/backend-storage: 1.1.2
  @aws-amplify/cli-core: 1.1.2
  @aws-amplify/client-config: 1.3.0
  @aws-amplify/deployed-backend-client: 1.4.0
  @aws-amplify/form-generator: 1.0.1
  @aws-amplify/model-generator: 1.0.5
  @aws-amplify/platform-core: 1.1.0
  @aws-amplify/plugin-types: 1.2.1
  @aws-amplify/sandbox: 1.2.1
  @aws-amplify/schema-generator: 1.2.1
  aws-amplify: 6.6.0
  aws-cdk: 2.156.0
  aws-cdk-lib: 2.156.0
  typescript: 5.5.4
AWS environment variables:
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
  AWS_STS_REGIONAL_ENDPOINTS = regional
No CDK environment variables

Describe the bug

When running a build in amplify my backend phase never triggers from this amplify.yml

version: 1
applications:
  - appRoot: .
    backend:
      phases:
        preBuild:
          commands:
            - echo "Backend preBuild step started"
            - amplify --version
            - echo "Backend preBuild step finished"
        build:
          commands:
            - echo "Backend build step started"
            - amplify status
            - amplifyPush --simple
            - amplify codegen models
            - mkdir -p cloud/lib/amplify/lib
            - amplify export --out cloud/lib/amplify/lib
            - echo "Backend build step completed"
        postBuild:
          commands:
            - echo "Custom postBuild step completed"
      artifacts:
        baseDirectory: cloud/lib/amplify
        files:
          - '**/*'
  - appRoot: ui/corvision
    frontend:
      phases:
        preBuild:
          commands:
            - echo "Frontend preBuild step started"
            - export FLUTTER_ROOT=$HOME/flutter
            - mkdir -p $FLUTTER_ROOT
            - curl -o flutter.tar.xz https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_3.24.2-stable.tar.xz
            - tar xf flutter.tar.xz -C $HOME
            - export PATH="$PATH:$FLUTTER_ROOT/bin"
            - flutter precache
            - flutter --version
            - echo "Frontend preBuild step completed"
        build:
          commands:
            - echo "Frontend build step started"
            - flutter pub get
            - flutter build web --release --verbose
            - echo "Frontend build step completed"
      artifacts:
        baseDirectory: build/web
        files:
          - '**/*'

Not sure if there is something wrong with my amplify.yml or what but I've tried a bunch of different commands and formats in the backend phase and none of them trigger/get called.

appID: d1gwep07t2uubt Region: use-east-2

Reproduction steps

try to deploy in amplify

Jay2113 commented 1 day ago

Hi @scott-elgaard 👋 , thanks for reaching out. I observed that the AMPLIFY_MONOREPO_APP_ROOT environment variable is set to ui/corvision for the app d1gwep07t2uubt. Thus, the commands that are present under the appRoot: ui/corvision defined in the amplify.yml file will be executed i.e. the frontend build phase.

To ensure the backend build phase is executed, you will need to update the amplify.yml file as follows:

version: 1
applications:
  - appRoot: ui/corvision
    backend:
      phases:
        preBuild:
          commands:
            - echo "Backend preBuild step started"
            - amplify --version
            - echo "Backend preBuild step finished"
        build:
          commands:
            - echo "Backend build step started"
            - amplify status
            - amplifyPush --simple
            - amplify codegen models
            - mkdir -p cloud/lib/amplify/lib
            - amplify export --out cloud/lib/amplify/lib
            - echo "Backend build step completed"
        postBuild:
          commands:
            - echo "Custom postBuild step completed"
    frontend:
      phases:
        preBuild:
          commands:
            - echo "Frontend preBuild step started"
            - export FLUTTER_ROOT=$HOME/flutter
            - mkdir -p $FLUTTER_ROOT
            - curl -o flutter.tar.xz https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_3.24.2-stable.tar.xz
            - tar xf flutter.tar.xz -C $HOME
            - export PATH="$PATH:$FLUTTER_ROOT/bin"
            - flutter precache
            - flutter --version
            - echo "Frontend preBuild step completed"
        build:
          commands:
            - echo "Frontend build step started"
            - flutter pub get
            - flutter build web --release --verbose
            - echo "Frontend build step completed"
      artifacts:
        baseDirectory: build/web
        files:
          - '**/*'

Please note that if your amplify directory exists at the root, you can run cd command to adjust/switch the directories.

You can learn more about the monorepo yaml syntax here: https://docs.aws.amazon.com/amplify/latest/userguide/monorepo-configuration.html#monorepo-yml-syntax.