Serverless-next.js: Error with the sls remove command

Created on 13 May 2020  ·  7Comments  ·  Source: serverless-nextjs/serverless-next.js

Firstly I`m very gratefull for work done in this Serverless Component.

Describe the bug

I`m facing an issue when I try to remove the AWS components created following this example:

Executing the command sls remove --stage=staging --debug, I`m getting the error below:

DEBUG ─ Flushing template state and removing all components.
  DEBUG ─ Clearing bucket my-bucket-name contents.
  DEBUG ─ Starting Domain component removal.
  DEBUG ─ Getting the Hosted Zone ID for the domain my-domain.
  DEBUG ─ Removing domain my-domain from CloudFront.
  DEBUG ─ Deleting bucket my-bucket from region us-east-1.
  DEBUG ─ Bucket my-bucket was successfully deleted from region us-east-1.
  DEBUG ─ Removing CloudFront DNS records for domain my-domain

  error:
  PreconditionFailed: The request failed because it didn't meet the preconditions in one or more request-header fields.
    at Request.extractError (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\protocol\rest_xml.js:53:29)
    at Request.callListeners (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
    at Request.emit (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\request.js:683:14)
    at Request.transition (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\request.js:38:9)
    at Request.<anonymous> (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\request.js:685:12)
    at Request.callListeners (C:\Users\pedro\.serverless\components\registry\npm\[email protected]\node_modules\serverless-next.js\node_modules\aws-sdk\lib\sequential_executor.js:116:18) {
  message: "The request failed because it didn't meet the preconditions in one or more request-header fields.",
  code: 'PreconditionFailed',
  time: 2020-05-13T13:54:39.479Z,
  requestId: '4e981a1d-487f-48e6-aa03-be515f224694',
  statusCode: 412,
  retryable: false,
  retryDelay: 0.8905779686132709
}

To Reproduce

serverless.yml

myNextApplication:
  component: serverless-next.js
  inputs:
    domain:
      - ${env.SUB_DOMAIN}
      - ${env.DOMAIN}
    bucketName: ${env.BUCKET_NAME}
    name:
      defaultLambda: ${env.DEFAULT_LAMBDA}

serverless.js

const { Component } = require('@serverless/core')

/**
 * Will deploy the serverless component
 */
class Deploy extends Component {
  async default(inputs = {}) {
    const { stage } = inputs

    if (!(stage === 'staging' || stage === 'production')) {
      this.context.log(
          'No environment defined... Choices are staging or production'
      );
      return
    }

    // Will load .env.${stage} as environment variables
    require('dotenv').config({ path: `${__dirname}/.env.${stage}` })

    const template = await this.load('@serverless/template', stage)

    return await template({ template: 'serverless.yml' })
  }

  // Remove will allow the stage to be set on the command line
  // run like so: serverless remove --stage=staging
  async remove(inputs = {}) {
    const { stage } = inputs;

    if (!(stage === 'staging' || stage === 'production')) {
      return
    }

    const template = await this.load('@serverless/template', stage);

    return await template.remove();
  }
}

module.exports = Deploy;

.env.staging

SUB_DOMAIN=my-subdomain
DOMAIN=my-domain
BUCKET_NAME=my-bucket
DEFAULT_LAMBDA=my-lambda

Dependency versions

    "@serverless/core": "^1.1.2",
    "@serverless/template": "^1.1.3",
    "dotenv": "^8.2.0",
    "dotenv-webpack": "^1.8.0",
    "serverless": "^1.70.1",
    "serverless-next.js": "^1.12.0-alpha.0",

Expected behavior

All provisioned infrasctructure created in my AWS Account must be removed without errors.

Desktop

  • OS: Windows Enterprise
  • Version: 10

Additional context

The sls --stage=staging --debug command works as expected, without any errors.

Most helpful comment

Also getting this.

⋊> ~/W/R/p/p/p/f/serverless-dev on feature/PH-1089 ⨯ env STAGE=dev npx serverless remove   

 error:
  PreconditionFailed: The request failed because it didn't meet the preconditions in one or more request-header fields.
    at Request.extractError (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/protocol/rest_xml.js:53:29)
    at Request.callListeners (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:690:12)
    at Request.callListeners (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
  message: "The request failed because it didn't meet the preconditions in one or more request-header fields.",
  code: 'PreconditionFailed',
  time: 2020-07-23T13:54:26.969Z,
  requestId: 'd01417f0-9d21-4cdc-b909-62ca555ac29e',
  statusCode: 412,
  retryable: false,
  retryDelay: 4.636794991065507
}

  4s › myNextApp › PreconditionFailed: The request failed because it didn't meet the preconditions in one or more request-header fields.

All 7 comments

@danielcondemarin We observed these same errors intermittently on remove at different parts of the remove process. We were able to resolve the issue by reverting a change in a prior commit; on the remove() function we changed the Promise.all back to three separate await(s).

The reason for this is that upon reviewing the @serverless/domain package, it makes calls to getDistributionConfigByDomain - and this would sometimes fail if the Distribution removal had already started. Apologies in advance if I'm way off base, but we didn't see how the three .remove() calls in this project could happen asynchronously. After changing it back to three await(s) from before the commit, the remove process succeeds as expected.

I didn't open a PR because I assumed that if no one had complained about this since the change in October, it was probably something in our usage. Still might be. 🤷‍♂️

I'm facing the same issue; using @sls-next/[email protected] on Mac - with a similar setup as above multi-stage deployment scenario. Is there any known workaround for this issue?

Also getting this.

⋊> ~/W/R/p/p/p/f/serverless-dev on feature/PH-1089 ⨯ env STAGE=dev npx serverless remove   

 error:
  PreconditionFailed: The request failed because it didn't meet the preconditions in one or more request-header fields.
    at Request.extractError (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/protocol/rest_xml.js:53:29)
    at Request.callListeners (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:690:12)
    at Request.callListeners (/Users/mdell/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
  message: "The request failed because it didn't meet the preconditions in one or more request-header fields.",
  code: 'PreconditionFailed',
  time: 2020-07-23T13:54:26.969Z,
  requestId: 'd01417f0-9d21-4cdc-b909-62ca555ac29e',
  statusCode: 412,
  retryable: false,
  retryDelay: 4.636794991065507
}

  4s › myNextApp › PreconditionFailed: The request failed because it didn't meet the preconditions in one or more request-header fields.

I have now found that if I run it 3 times it works, getting a different error message each time. 🤷🏻‍♂️

same here.

$ serverless remove

  error:
  PreconditionFailed: The request failed because it didn't meet the preconditions in one or more request-header fields.
    at Request.extractError (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/protocol/rest_xml.js:53:29)
    at Request.callListeners (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/request.js:690:12)
    at Request.callListeners (/Users/riky/.serverless/components/registry/npm/@sls-next/[email protected]/node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
  code: 'PreconditionFailed',
  time: 2020-08-03T18:06:14.758Z,
  requestId: '718c356a-1c43-4f4c-b756-1f4f647d07f9',
  statusCode: 412,
  retryable: false,
  retryDelay: 25.537051155504198
}

  5s › masho-web › PreconditionFailed: The request failed because it didn't meet the preconditions in one or more request-header fields.

"devDependencies": {
    "serverless-next.js": "^1.11.5"
  }

any ideas?

@codeurge I was facing the same issue and reached the same solution. I have opened a PR, removing Promise.all while removing the resources.

Still seeing this error in some cases. There were similar issues in Terraform that were fixed in this PR: https://github.com/hashicorp/terraform-provider-aws/pull/7794

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikdatdogru picture mikdatdogru  ·  7Comments

vishwasnavadak picture vishwasnavadak  ·  4Comments

robsonkades picture robsonkades  ·  3Comments

danielcondemarin picture danielcondemarin  ·  6Comments

mekwall picture mekwall  ·  5Comments