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

Lambda cloudformation json changes between env pushes

Open hisham opened this issue 7 years ago • 33 comments

Describe the bug Everytime I switch envs and do a push the *-cloudformation-template.json of my lambdas (in amplify/backend/function) changes. These files are not in .gitignore (and I believe shouldn't).

Screenshot of the diff with change highlighted:

dry-eye-frontend — vim ◂ git difftool — 214×90 2019-04-02 19-46-44

To Reproduce Steps to reproduce the behavior:

  1. Create function in one env
  2. Checkout another env and push
  3. See what files changed

Expected behavior Random changes like this should be in .gitignore files.

Desktop (please complete the following information):

  • OSX

Additional context Running amplify cli v1.4

hisham avatar Apr 03 '19 01:04 hisham

@hisham Each time there is a change to the Lambda function source code, for Cloudformation(CFN) to re-deploy the lambda function, the S3 bucket key needs to change otherwise Cloudformation wouldn't know that there is a change to the Lambda function and won't re-deploy your functions and that's why you see the diffs in the CFN files. Do you have any specific suggestions as to how we can get around this?

kaustavghosh06 avatar Apr 03 '19 04:04 kaustavghosh06

I'm no cloudformation expert, but maybe there's a way to extract the s3 bucket info into a parameters file or nested stack of some sort that can be added to .gitignore. It seems you do something similar in the graphql nested stacks stuff where the s3 bucket info is just a variable:

        "RequestMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers...

hisham avatar Apr 03 '19 15:04 hisham

The other annoying thing here is when I switch back to the first env and do amplify status, it shows that my functions need updating, which is quite annoying, because they don't really need updating and it sort of freaks me out at first glance because I'm wondering what changed that requires it to update.

hisham avatar Apr 03 '19 17:04 hisham

We're also having trouble with this. Because we use the CLI in CI, the init function seems to fail because of this. To pass it as a parameter would make more sense to me aswell.

thde avatar Apr 29 '19 08:04 thde

@kaustavghosh06 We are also experiencing this same issue. It's messing up our multi-env deployment and it's pretty aggravating. I agree with @hisham that this should be parameterized so that the template itself is unaltered when you switch between backend environments using "amplify env checkout <env_name>" We also noticed, as @hisham mentioned, that a parameterized technique is used for the graphql stack (see his comment above).

When no lambda code has changed, and no resources have been altered, just switching environments with "amplify env checkout" should not cause both git and amplify status to report changes. When the only thing that has been done is to switch backend environments, git should report not changes and amplify status should report no changes (other than that you are now checked out on a different backend environment).

When no changes have been made to your function, simply switching environments with "amplify env checkout" causes "amplify status" to report that your function is altered. Git also reports changes to your function's cloud formation template. Also, and worse, it's possible to get into a bad state where the S3 bucket and key are pointing to the wrong backend environment than the one that is currently checked out with "amplify env checkout". We have a highly-customized multi-env deployment and everything is working now with Amplify CLI except for this one issue. Ug.

EDIT: We are experiencing the above issues with amplify cli version 1.6.10

OtterFlip avatar May 29 '19 17:05 OtterFlip

I wanted to add, we run into this today. I'd like to know how everyone is getting around this other than handling git conflicts?

I also wanted to add, that it's not possible to just put the files in .gitignore if you're using amplify console for continuous deployment because it fails in the build process.

This thread seems to be the same issue and suggests it's safe to remove so it's confusing: https://github.com/aws-amplify/amplify-cli/issues/968

ghost avatar Jun 21 '19 18:06 ghost

Also affect my project, hitting git merge conflicts and just some confusion.

Any update on if the CLI team is working on improving this?

houmark avatar Jul 02 '19 22:07 houmark

@houmark Are you using the latest version of the CLI? With the latest version of CLI, which was merged as a part of this PR - #1668 we now use the hash of your lambda function source-code and the package-lock to determine the S3 keys, so that they don't change on every deployment (which was previously using deployment timestamps causing merge conflicts). Please feel free to re-open or comment on this thread if you're still facing this issue.

kaustavghosh06 avatar Jul 03 '19 07:07 kaustavghosh06

I am using the latest CLI 1.7.8.

Not sure what's going on exactly, but it may be related to also using Amplify Console to deploy which seems to create even more issues in the CLI because it thinks there are updates to do, when there are not. I am about to submit a question here to clarify how best usage is of both tools.

houmark avatar Jul 03 '19 13:07 houmark

image

@kaustavghosh06 are you sure? I am using amplify 1.7.8 The S3Key on a lambda function changed after doing amplify push. I tried it on a project that existed with the "old" aws-amplify cli, maybe that is the issue?

slaat avatar Jul 03 '19 15:07 slaat

Yes, if you see this behavior with a consequent push with the new CLI, without making any change to your Lambda code, let me know.

kaustavghosh06 avatar Jul 03 '19 15:07 kaustavghosh06

Ohhh I get it. Yeah I will let you know if I see any issues. If this works out it would be super helpful. Thank you!

slaat avatar Jul 03 '19 15:07 slaat

@kaustavghosh06 for people using yarn it seems like this issue still exists because you're using package-lock which is npm specific...

hisham avatar Jul 11 '19 18:07 hisham

I'm using npm and I have not had issues with merging develop into master since my last comment.

houmark avatar Jul 11 '19 18:07 houmark

I'm experiencing this issue on 4.9.0 as well. Every time I push to a different environment, the following gets set on all of my functions

				"Code": {
					"S3Bucket": "******-{someEnv}-****-deployment",
					"S3Key": "amplify-builds/****.zip"
				}

The s3 bucket and s3 key are environment specific. This is infuriating. It's one bug after the other.

dyangelo-grullon avatar Dec 27 '19 23:12 dyangelo-grullon

@kekkler The S3Key needs to change per env and it gets generated at runtime for every push, so that Cloudformation is aware of the change in your source code and re-deploys your Lambda function. Did you see any discrepancies in the deployed Lambda or did the push fail?

kaustavghosh06 avatar Dec 27 '19 23:12 kaustavghosh06

amplify/backend/function/**AuthPostConfirmation/**AuthPostConfirmation-cloudformation-template.json

is where these s3 keys are set. Under the resource:

"Resources": {
		"LambdaFunction": {
			"Type": "AWS::Lambda::Function",
			"Metadata": {
				"aws:asset:path": "./src",
				"aws:asset:property": "Code"
			},

dyangelo-grullon avatar Dec 28 '19 00:12 dyangelo-grullon

This should be passed into the template as a parameter, so that the template remains agnostic to the environment.

dyangelo-grullon avatar Dec 28 '19 21:12 dyangelo-grullon

Right off the bat with a new project using 4.12.0 and merging changes from dev env to master env starts a cycle of conflicts for function S3 bucket names were the env suffix is the dif. Why?

willrust avatar Jan 14 '20 23:01 willrust

This ticket needs to re-opened

dyangelo-grullon avatar Jan 21 '20 06:01 dyangelo-grullon