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

Function env vars SSM Parameters missing in PR deploy previews, feature-branch deployments #12950

Open josefaidt opened 1 year ago

josefaidt commented 1 year ago

CLI version 12.2.0-rc.a3b139fb36.0


Hello Team, I see that that the issue was fixed linked with PR. I tested this with Amplify CLI Version - 12.2.0-rc.a3b139fb36.0 and observe similar issue

Steps of Reproduction

  1. created sample react project.
  2. amplify init -y
  3. amplify add function (make sure you add environment variables to the function)
  4. initialize git repo and git push
  5. Deploy this master/main branch using CI/CD on Amplify console. The build would be triggered and deployed successfully.
  6. enable pr preview on amplify console with option "Create new backend environment"
  7. On local -> git checkout -b testbranch
  8. make some changes in index.js of lambda function.
  9. Commit and push to git repo
  10. raise PR with main/master branch which is already hosted on amplify
  11. The PR build will create a new backend environment with random name and would get failed with error shown in build logs below -
    
    2023-07-12T06:13:53.961Z [INFO]: # Retrieving environment cache...
    2023-07-12T06:13:53.993Z [WARNING]: ! Unable to write cache: {"code":"ERR_BAD_REQUEST","message":"Request failed with status code 404"})}
    2023-07-12T06:13:53.993Z [INFO]: ---- Setting Up SSM Secrets ----
    2023-07-12T06:13:53.993Z [INFO]: SSM params {"Path":"/amplify/d1d2qz3ywham00/prg/","WithDecryption":true}
    2023-07-12T06:14:11.144Z [INFO]: # Patching NPM package '@aws-amplify/cli' from 10.5.2 to 12.2.0-rc.a3b139fb36.0...
    2023-07-12T06:14:27.556Z [INFO]: # Done patching NPM package '@aws-amplify/cli' to version 12.2.0-rc.a3b139fb36.0
    2023-07-12T06:14:27.641Z [INFO]: # Retrieving cache...
    2023-07-12T06:14:27.666Z [INFO]: # Retrieved cache
    2023-07-12T06:14:31.785Z [INFO]: ## Starting Backend Build
                                 # Starting phase: build
    2023-07-12T06:14:33.654Z [INFO]: Note: It is recommended to run this command from the root of your app directory
    2023-07-12T06:14:33.958Z [INFO]: Amplify AppID found: d1d2qz3ywham00. Amplify App name is: reactmultipage
    2023-07-12T06:14:34.012Z [INFO]: Adding backend environment prg to AWS Amplify app: d1d2qz3ywham00
    2023-07-12T06:14:39.605Z [INFO]: 
    2023-07-12T06:14:39.610Z [INFO]: CREATE_IN_PROGRESS amplify-reactmultipage-prg-61433 AWS::CloudFormation::Stack Wed Jul 12 2023 06:14:34 GMT+0000 (Coordinated Universal Time) User Initiated
                                 CREATE_IN_PROGRESS UnauthRole                       AWS::IAM::Role             Wed Jul 12 2023 06:14:37 GMT+0000 (Coordinated Universal Time)
                                 CREATE_IN_PROGRESS AuthRole                         AWS::IAM::Role             Wed Jul 12 2023 06:14:37 GMT+0000 (Coordinated Universal Time)
                                 CREATE_IN_PROGRESS DeploymentBucket                 AWS::S3::Bucket            Wed Jul 12 2023 06:14:37 GMT+0000 (Coordinated Universal Time)
                                 CREATE_IN_PROGRESS UnauthRole                       AWS::IAM::Role             Wed Jul 12 2023 06:14:38 GMT+0000 (Coordinated Universal Time) Resource creation Initiated
                                 CREATE_IN_PROGRESS AuthRole                         AWS::IAM::Role             Wed Jul 12 2023 06:14:38 GMT+0000 (Coordinated Universal Time) Resource creation Initiated
                                 CREATE_IN_PROGRESS DeploymentBucket                 AWS::S3::Bucket            Wed Jul 12 2023 06:14:38 GMT+0000 (Coordinated Universal Time) Resource creation Initiated
    2023-07-12T06:14:54.593Z [INFO]: 
    2023-07-12T06:14:54.594Z [INFO]: CREATE_COMPLETE UnauthRole AWS::IAM::Role Wed Jul 12 2023 06:14:51 GMT+0000 (Coordinated Universal Time)
                                 CREATE_COMPLETE AuthRole   AWS::IAM::Role Wed Jul 12 2023 06:14:51 GMT+0000 (Coordinated Universal Time)
    2023-07-12T06:15:04.660Z [INFO]: 
    2023-07-12T06:15:04.867Z [INFO]: 
    2023-07-12T06:15:04.918Z [INFO]: 
    2023-07-12T06:15:04.929Z [WARNING]: - Uploading files.
    2023-07-12T06:15:04.969Z [INFO]: 
    2023-07-12T06:15:04.969Z [INFO]: 
    2023-07-12T06:15:05.020Z [INFO]: 
    2023-07-12T06:15:05.020Z [INFO]: 
    2023-07-12T06:15:05.084Z [INFO]: 
    2023-07-12T06:15:05.135Z [INFO]: 
    2023-07-12T06:15:05.136Z [INFO]: 
    2023-07-12T06:15:05.185Z [INFO]: 
    2023-07-12T06:15:05.189Z [INFO]: 
    2023-07-12T06:15:05.190Z [INFO]: 
    2023-07-12T06:15:05.191Z [WARNING]: - Uploading files.
    2023-07-12T06:15:05.383Z [WARNING]: - Initializing your environment: prg
    2023-07-12T06:15:05.414Z [WARNING]: βœ” Initialized provider successfully.
    2023-07-12T06:15:05.469Z [WARNING]: βœ– There was an error initializing your environment.
    2023-07-12T06:15:05.469Z [INFO]: πŸ›‘ This environment is missing some parameter values.
    2023-07-12T06:15:05.469Z [INFO]: [variable1,variable2] do not have values.
                                 Resolution: Run 'amplify push' interactively to specify values.
                                 Alternatively, manually add values in SSM ParameterStore for the following parameter names:
                                 /amplify/d1d2qz3ywham00/prg/AMPLIFY_function_reactmultipage5cc10a9d_variable1
                                 /amplify/d1d2qz3ywham00/prg/AMPLIFY_function_reactmultipage5cc10a9d_variable2
                                 Learn more at: https://docs.amplify.aws/cli/reference/ssm-parameter-store/#manually-creating-parameters
    2023-07-12T06:15:05.478Z [INFO]: 
    2023-07-12T06:15:05.479Z [INFO]: Session Identifier: 448cca09-c44d-4a5e-ab29-61f47afa8a5e
    2023-07-12T06:15:05.495Z [ERROR]: !!! Build failed
    2023-07-12T06:15:05.495Z [ERROR]: !!! Non-Zero Exit Code detected
    2023-07-12T06:15:05.496Z [INFO]: # Starting environment caching...
    2023-07-12T06:15:05.496Z [INFO]: # Environment caching completed
    Terminating logging...
Amplify.yml

version: 1 backend: phases: build: commands:


Another follow-up query as the random name creates confusion in understanding that which PR is related to which branch - 
- How we can specify the name of backend environment which is initialised during build or is there any way using which we know that random name before deployment.
- Would it be possible if we can replace the random backend environment with the name of git branch from which PR was raised.

_Originally posted by @anivarsh in https://github.com/aws-amplify/amplify-cli/issues/12644#issuecomment-1636370495_
josefaidt commented 1 year ago

Following the steps above I am encountering a slightly different message:

2023-07-14T21:51:31.513Z [INFO]: πŸ›‘ This environment is missing some function environment variable values.
2023-07-14T21:51:31.514Z [INFO]: Function 126446643a84e is missing values for environment variables: my_var
ggorge-etiqa commented 11 months ago

I'm getting the same error. I'm not sure to understand if you fond a solution here @josefaidt

Haakam21 commented 7 months ago

Any plans to fix in the near future? Previews are unusable for us.

charlieforward9 commented 6 months ago

From this linked Issue (comment):

I tried, Previews->Manage preview settings for main branch->Point all Pull Requests for this branch to an existing environment and setting it to the environment I want it to be linked to.

Build failed.

I also tried adding the environment variable directly into the Amplify Dashboard Environment Variables, build failed.

Why is this bug currently not being addressed? I would appreciate to grant my non-engineer teammates test abilities to the our application ahead of a production release.

kemisis commented 4 months ago

I get the same error following theses steps. Previews are unusable with environment variables associated with functions. Do you have any updates on this @josefaidt ?

grfortun commented 1 month ago

HI Facing the same issue when creating new branches, I found that a quick solution would be to pull the new environment down locally and run the amplify push command to manually enter the parameter values.

However, in an environment where there are large amounts of parameters, I found that this is not feasible. I then wrote a Amplify pre-push Hook Script to pull the parameters from a know working branch and create the parameters for the new branch on push, I added some logic to first check if the parameters exist and if not create the ones that do not, this allowed me to move past this issue.

Link to my script: https://github.com/grantf28/Amplify-SSM-Parameters-Hooks-Script/blob/main/hooks/pre-push.js