You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to deploy a packaged service (This command can only be run in a Serverless service directory) #4715
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)
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
@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.
Reacted by Victor-Vartan Pambuccian, Jatin Mehrotra and Eric McCormick
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?
Reacted by Justin King, Peter Nagy, fs, Łukasz Podsiadły, Omkar Kanore, Sophie Cooper, Fabien Ruffin, Carmen Spohn, Shawn, Paul Shestakov and 22 more
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.
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
Reacted by Sushant Pradhan, Carmen Spohn, ashley7070, Jason DeRose, Jim Pudar, Tomasz Dudek, Kris Gholson and Eric Staples
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.
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.
Reacted by Frank, Łukasz Podsiadły, Omkar Kanore, Adrian Falleiro, Fabien Ruffin, Dragan C, Tomasz Dudek and Eric Staples
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?
@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.
Reacted by Jaume de Juan Sancho, Ahmed Abdulmoniem, ashley7070 and Jim Pudar
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.
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?
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.
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:
Your CWD has to be set to the directory of the deployment package.
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 ./)
Also make sure the name of the deployment zip matches the name of the service as defined in the serverless.yml.
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.
@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
@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
Activity
HyperBrain commentedon Feb 4, 2018
@jsancho the root problem of your
deployinvocation here is, that is must be executed with a CWD set to the service directory. That's why it bails out withThis 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.ymlis located.jsancho commentedon Feb 4, 2018
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-foldercommand should be then amended to execute the CF stack directly.Am I making sense at all?
Add serverless.yml to workspace
HyperBrain commentedon Feb 6, 2018
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 --packageonly reads the templates and performs the deployment step (maybe using plugins that run on the CF templates, like the alias plugin).In fact
serverless deploywithout options is just a concatenation of the package and deploy phase.However, the persitance of the
serverless.ymlcurrently 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.jsonof the service in the artifact because the deploy will package the dependencies to create the final service zip files.jsancho commentedon Feb 11, 2018
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:
Thanks
HyperBrain commentedon Feb 11, 2018
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.
Justin-DynamicD commentedon Feb 16, 2018
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.
cgspohn commentedon Nov 29, 2018
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?
ahmed-abdulmoniem commentedon Jan 5, 2019
Any fix to that until now?
aidonius commentedon Jan 9, 2019
@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.
jsancho commentedon Jan 10, 2019
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
serverless.ymlto the artifact produced bysls packageserverless-iam-roles-per-functionsls deploy --forceflag because sometimes deployments are unexpectedly skippedI 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 packageis 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.
ahmed-abdulmoniem commentedon Jan 12, 2019
@jsancho I want to understand this sentence:
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?
ahmed-abdulmoniem commentedon Jan 12, 2019
@jsancho Actually I can't make
serverless-iam-roles-per-functionrun even with the normalsls deploycommand.My
iamRoleStatementsunder the function is:Always, only the default role is created. Nothing new is created and no new roles or policies are added!
Any clue?
ahmed-abdulmoniem commentedon Jan 12, 2019
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.
sudosoul commentedon Jul 23, 2019
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:
Your CWD has to be set to the directory of the deployment package.
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 ./)Also make sure the name of the deployment zip matches the name of the service as defined in the serverless.yml.
saumilsdk commentedon Nov 5, 2019
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.
defaultbr commentedon Mar 15, 2020
@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
saumilsdk commentedon Mar 17, 2020
@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
tomaszdudek7 commentedon May 8, 2020
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?
mancuss commentedon Aug 27, 2020
Any plans to make this work?
corydorning53 commentedon Nov 30, 2020
anyone done this when packaging artifacts individually? I've looked at the workarounds but not sure how to do it for multiple zip packages...
aidonius commentedon Nov 30, 2020
For what it is worth, our workaround was switching to SAM. 🤷