aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

How to control resources conditional on which environment Amplify is deploying to #13860

Open mishabruml opened 1 month ago

mishabruml commented 1 month ago

Amplify CLI Version

12.12.4

Question

Hi there,

I've got a multi-environment project built with Amplify 12.12.4, Gen 1. Each environment (dev, sandbox, prod) deploys into it's own AWS account.

The project is basically hosting an s3-and-cloudfront site with cognito auth. The project is hooked up to be using amplify hosting/CI just like in this guide https://docs.amplify.aws/gen1/react/deploy-and-host/deployment/deploy-static-site-github/#deploy-your-app-to-aws-amplify

We have a requirement for a cognito pre-authentication lambda trigger to run some custom check before sign-in, but we only need this in prod.

I have successfully configured the lambda, but on all 3 environments; I only need it on prod. I can't find an straightforward way of configuring this through code.

It seems like based on answers such as https://github.com/aws-amplify/amplify-cli/issues/4875 that the answer relies on running amplify commands with the --restore flag, but this obviously isn't a persistent, source-control-backed change that will survive through CI etc.

How can this environment-specific behvaiour be represented through code?

TL;DR: How can I configure, through commitable code, for cognito auth to have a trigger lambda, but in one amplify environment only

Thanks

mishabruml commented 1 month ago

related: https://github.com/aws-amplify/amplify-cli/issues/1115

ykethan commented 4 weeks ago

Hey, this is currently not supported on Amplify CLI. But the workaround would be to utilize the ENV environment variable that Amplify deploy on the function by default and separate the config.

export const handler = (event) => {
  if (process.env.ENV) {
    return event
  }
  // your prod env logic
}

Marking as feature request

mishabruml commented 3 weeks ago

Thanks for the reply, thats really helpful to know. I ended up at this conclusion and had already implemented pretty much exactly what you suggested. Seems like if this functionality doesn't already exist then its a huge, core-level change so I should not expect anything in the immediate future?

Is there no way to manually modify the underlying cloudformation in order to acheive this?