Skip to content

Unable to deploy a packaged service (This command can only be run in a Serverless service directory) #4715

Open

Description

@jsancho

This is a Bug Report

Description

  • What went wrong?
    I have created a vanilla aws-nodejs service.
    Then packaged and tried to deploy the artifacts outwith the source folder.

  • What did you expect should have happened?
    The stack should have been deployed to AWS

  • What was the config you used?

These are the steps to reproduce the issue from scratch

serverless create --template aws-nodejs --path hello-world
cd hello-world
serverless package --package artifacts
cd ..
serverless deploy --package hello-world/artifacts

The following error shows

  This command can only be run in a Serverless service directory
  • What stacktrace or error message from your provider did you see?
Error: This command can only be run in a Serverless service directory
    at getServerlessConfigFile.then (/usr/local/lib/node_modules/serverless/lib/plugins/lib/validate.js:20:23)
From previous event:
    at Deploy.validate (/usr/local/lib/node_modules/serverless/lib/plugins/lib/validate.js:12:51)

I've looked at the source file in the stack trace
gerServerlessConfigFile.js

and it seems to be looking for one of the standard config files

const jsonPath = path.join(servicePath, 'serverless.json');
  const ymlPath = path.join(servicePath, 'serverless.yml');
  const yamlPath = path.join(servicePath, 'serverless.yaml');
  const jsPath = path.join(servicePath, 'serverless.js');

  return BbPromise.props({
    json: fileExists(jsonPath),
    yml: fileExists(ymlPath),
    yaml: fileExists(yamlPath),
    js: fileExists(jsPath),

If I understand this correctly, the point of generating a package was not to rely on having access to the serverless config in source.
So I'd say that there is something with the way the deploy --package command

Similar or dependent issues:

Additional Data

  • Serverless Framework Version you're using: 1.26.0
  • Operating System: MacOs and Linux

thanks very much,

Jaume

Activity

  1. HyperBrain commented on Feb 4, 2018

    @HyperBrain
    Contributor

    @jsancho the root problem of your deploy invocation here is, that is must be executed with a CWD set to the service directory. That's why it bails out with This command can only be run in a Serverless service directory.
    This behavior is unrelated to the package/deploy but is a requirement of Serverless itself. The CWD for the Serverless invocation has to be the directory where your serverless.yml is located.

  2. jsancho commented on Feb 4, 2018

    @jsancho
    Author

    Fair enough, but there's something that I'm not fully understanding here.
    I've come across this issue as I was configuring my CD pipelines.

    When I submit my serverless service to a git repo, I have a build pipeline that creates an output folder.
    This output folder is then stored in my artifacts repository, as an immutable bundle.

    The release pipeline is then automatically triggered, retrieves the artifact, and deploys its contents to a specific environment.

    As a temporary workaround, I am adding the serverless.yml to my output folder and that deploys fine.
    However, it seems that this serverless.yml is being used to re-generate the cloudformation files.
    If that's the case, this behaviour would break the "immutability" of the artifact.

    All summed up, is that my expectation was that the package command would indeed create a package, with all the resources that are required for an independent deployment.

    If the existence of the serverless.yml doesn't re-generate the CF config, then I'd suggest that the package command should also copy the config. (just like I do with my workaround)

    Otherwise, if the config file does regenerate the CF files, it means that we are ignoring the ones that were already available package.
    So the deploy --package path-to-folder command should be then amended to execute the CF stack directly.

    Am I making sense at all?

  3. added a commit that references this issue on Feb 5, 2018
  4. HyperBrain commented on Feb 6, 2018

    @HyperBrain
    Contributor

    If the existence of the serverless.yml doesn't re-generate the CF config, [...]

    Yes, that's the case. Technically the deploy command together with the package option just continues where the package command left off. Package builds the service (and runs the compile plugins) and creates the CF templates, while deploy --package only reads the templates and performs the deployment step (maybe using plugins that run on the CF templates, like the alias plugin).
    In fact serverless deploy without options is just a concatenation of the package and deploy phase.

    However, the persitance of the serverless.yml currently has to be done by the user's build pipeline (CI/CD), because Serverless needs the yml file and the used plugins before it can run any command.
    In general, you also need to persist the package.json of the service in the artifact because the deploy will package the dependencies to create the final service zip files.

  5. jsancho commented on Feb 11, 2018

    @jsancho
    Author

    Could you then accept a feature request to make generated packages immutable?
    IMO the current implementation breaks one of the core tenets of Continuous Delivery, by having to regenerate the CF files when performing a release.

    Essentially, the task would need the following:

    • To include in the package output all source code and its required dependencies
    • Make the deploy command skip any build/generation phase and deploy the CF directly to the provider.

    Thanks

  6. HyperBrain commented on Feb 11, 2018

    @HyperBrain
    Contributor

    These 2 requirements are already the case with the current implementation of the Serverless framework. All packaged/compiled sources are already part of the package and the deploy command does not regenerate the CF files (only if a deploy phase plugin adapts them).

    But, what breaks the core tenant is, that the serverless configuration file is needed to resume and that the stage used to package is hard-coded within the CF files, what means, that a build for stage X only can be deployed to stage X but not stage Y. So the packages are not really universal.

    For these 2 missing features there are already GitHub issues that I created when I implemented the package/deploy mechanism. I separated them, because they need some deeper thinking and a proper and robust solution:

    #3421 - Bootstrapped start for deploy (because the package already contains the complete serverless configuration and its state)
    #3528 - Defer stage resolution to deploy phase (so that the package step creates an universal artifact that can be deployed to different Serverless stages)

    Maybe there is a third one missing: The packaging of the plugins needed to perform the deploy, which should be symmetric to the packaging. It might be enough to package the serverless plugins from the package.json or even only their versions, so that de deploy command can easily restore them by using npm.

    With these 3 things implemented, it should fulfill all requirements for a stable CI/CD deployment.

  7. Justin-DynamicD commented on Feb 16, 2018

    @Justin-DynamicD

    Running into this here as well. The desire is to create a universal/immutable package that can then be versioned and appropriately sent out vs the reality that each stage effectively is a mini pipeline unto itself (I'm rebuilding each time). Obviously workable, but less than ideal.

  8. cgspohn commented on Nov 29, 2018

    @cgspohn

    Also trying to implement CI/CD with inmutable packages here. I have setup the jobs to carry the serverless.yml, package.json and package-lock.json as workaround. But now, we find that if the serverless.yml contains variables using the ${file(...)} things break, since it tries to find the files locally and of course, they are not, they are the packaged zip file. If serverless is working from a package, shouldn't it know to look for files in the zip package?

  9. ahmed-abdulmoniem commented on Jan 5, 2019

    @ahmed-abdulmoniem

    Any fix to that until now?

  10. aidonius commented on Jan 9, 2019

    @aidonius

    @ahmed-abdulmoniem There is a suggested workaround from @bishwash-devops on Medium, but even that requires a complete npm environment with serverless and all plugins installed. It seems like there needs to be a serious conversation of hardening serverless for CI/CD pipelines that is ongoing.

  11. jsancho commented on Jan 10, 2019

    @jsancho
    Author

    It seems like there needs to be a serious conversation of hardening serverless for CI/CD pipelines that is ongoing.

    I couldn't agree more with you @Lanakire .

    It was almost a year ago that I reported this issue.

    I'm currently using the following workarounds

    • add serverless.yml to the artifact produced by sls package
    • add commands to install sls plugins that are required at deploy time, such as serverless-iam-roles-per-function
    • introduce the sls deploy --force flag because sometimes deployments are unexpectedly skipped

    I have also been recently disappointed that all our CD deployments to AWS lambda broke after upgrading to latest version of the framework. The issue has been that sls package is now creating a zip file, whereas it used to simply create a folder that I then packaged myself with the workarounds above.

    Ideally, I would have preferred to keep backwards compatibility, or failing that, to be able to use a flag to keep the old behaviour.

    As much as a I appreciate that the framework is pushing forward with new features, such as coming out with support for WebSockets straight after the re:Invent announcements.

    It is a tad disappointing to not see an interest in getting some more of the basics right.
    This is my personal take, but I cannot possibly think of a fast-moving and reliable development effort these days without first-class support for CI/CD practices.

    I'd be more than happy to discuss this further and help out as much as I possibly can.

  12. ahmed-abdulmoniem commented on Jan 12, 2019

    @ahmed-abdulmoniem

    @jsancho I want to understand this sentence:

    add commands to install sls plugins that are required at deploy time, such as serverless-iam-roles-per-function
    

    I am using the same process as yours but it seems the plugins are not working at all when I deploy. How can I package plugins as well with the artifacts?

  13. ahmed-abdulmoniem commented on Jan 12, 2019

    @ahmed-abdulmoniem

    @jsancho Actually I can't make serverless-iam-roles-per-function run even with the normal sls deploy command.

    My iamRoleStatements under the function is:

      iamRoleStatementsName: ${self:custom.service.name}-hello-role
      iamRoleStatements:
        - Effect: "Allow"        
          Action:
            - events:PutEvents
    

    Always, only the default role is created. Nothing new is created and no new roles or policies are added!

    Any clue?

  14. ahmed-abdulmoniem commented on Jan 12, 2019

    @ahmed-abdulmoniem

    I figured out why now .. it is maybe another glitch in serverless.

    If I added the plugins section in another file and included this file in serverless.yml it won't work. Adding plugins directly into the serverless.yml did the trick.

  15. sudosoul commented on Jul 23, 2019

    @sudosoul

    Ran into this issue myself when I build the package first, and save the deployment package in some other directory. The solution is two part:

    1. Your CWD has to be set to the directory of the deployment package.

    2. The directory also needs to contain the associated serverless.yml file (even though it's not being used since the package has already been built, and you are deploying with serverless deploy --package ./)

    3. Also make sure the name of the deployment zip matches the name of the service as defined in the serverless.yml.

  16. saumilsdk commented on Nov 5, 2019

    @saumilsdk

    I found another way of doing it. Uploaded package.zip needs to be extracted in a directory. Let's say ./project/. Copy package.zip, serverless-state and cloudformation.json files to another folder say ./project/build/. Now run deploy --package build/ from /project directory.

    In this solution only 3 files needs to be uploaded to artifactory which were created in .serverless folder on serverless package command.

  17. defaultbr commented on Mar 15, 2020

    @defaultbr

    @saumilsdk you still need the serverless.yml from source code.

    i think everybody is looking for a way to build the package.zip immutable.

    If it need a serverless yml, so the package command should pack the serverless.yml with the variables and all other configurations inside the .zip file, so the .zip is a completly "immutable".

    So the deploy command read the package.zip and the serverless.yml inside it

  18. saumilsdk commented on Mar 17, 2020

    @saumilsdk

    @defaultbr I couldn't agree with you on deploy command reading yml file from zip. Deploy simply reads serverless.yml in that folder where you run deploy command. So you need to extract the zip and run deploy command inside the extracted directory

  19. tomaszdudek7 commented on May 8, 2020

    @tomaszdudek7

    I just ran into it and I see that more than 2 years have passed. Do we have any official statement from the Serverless Framework team?

  20. mancuss commented on Aug 27, 2020

    @mancuss

    Any plans to make this work?

  21. corydorning53 commented on Nov 30, 2020

    @corydorning53

    anyone done this when packaging artifacts individually? I've looked at the workarounds but not sure how to do it for multiple zip packages...

  22. aidonius commented on Nov 30, 2020

    @aidonius

    For what it is worth, our workaround was switching to SAM. 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions