Skip to content

Instantly share code, notes, and snippets.

@HyperBrain
Last active November 10, 2024 19:54
Show Gist options
  • Save HyperBrain/50d38027a8f57778d5b0f135d80ea406 to your computer and use it in GitHub Desktop.
Save HyperBrain/50d38027a8f57778d5b0f135d80ea406 to your computer and use it in GitHub Desktop.
Serverless Lifecycle Cheat Sheet

Serverless plugin author's cheat sheet

This cheat sheet provides a detailed overview of the exposed lifecycle events and available commands (and entrypoints) of the Serverless framework, that can be hooked by plugins (internal and external ones). The document is structured by the commands invoked by the user.

Lifecycle events are shown as the globally available outer events (all providers) and sub lifecycle events that are provider specific in the called order. Currently only the AWS provider is shown. If you have information about the other provider, please let me know and I will add the sub lifecycles accordingly.

The current event system can be improved in the future. Only package, deploy and info provide detailed event lifecycles for now, that make the plugin integration much more fine-grained.

Plugins can spawn any command or entrypoint with this.serverless.pluginManager.spawn() and invoke a sub lifecycle.

Lifecycle events / commands

package

package:cleanup

-> aws:common:validate:validate
-> aws:common:cleanupTempDir:cleanup

package:initialize

package:setupProviderConfiguration

package:createDeploymentArtifacts

package:compileFunctions

package:compileEvents

package:finalize

-> aws:package:finalize:mergeCustomProviderResources
-> aws:package:finalize:saveServiceState
-> aws:common:moveArtifactsToPackage:move

deploy

before:deploy:deploy

-> aws:common:validate:validate
-> aws:common:moveArtifactsToTemp:move

deploy:deploy

-> aws:deploy:deploy:createStack
-> aws:deploy:deploy:checkForChanges (1.17->)
-> aws:deploy:deploy:uploadArtifacts
-> aws:deploy:deploy:validateTemplate
-> aws:deploy:deploy:updateStack

deploy:finalize

-> aws:deploy:finalize:cleanup

deploy function

deploy:function:initialize

deploy:function:packageFunction

-> package:function:package

deploy:function:deploy

-> aws:common:cleanupTempDir:cleanup

deploy list

deploy:list:log

info

info:info

-> aws:info:validate
-> aws:info:gatherData
-> aws:info:displayServiceInfo
-> aws:info:displayApiKeys
-> aws:info:displayEndpoints
-> aws:info:displayFunctions
-> aws:info:displayStackOutputs

remove

remove:remove

rollback

rollback:initialize

rollback:rollback

rollback function

rollback:function:rollback

logs

logs:logs

invoke

invoke:invoke

invoke local

invoke:local:loadEnvVars

invoke:local:invoke

create

create:create

config

config:credentials:config

install

install:install

login

login:login

logout

logout:logout

metrics

metrics:metrics

slstats

slstats:slstats

@HyperBrain
Copy link
Author

HyperBrain commented Jan 10, 2018

Just saw, that I missed the deploy:function lifecycle completely in the document. I will add them soon.
UPDATE: Added "deploy function" and "deploy list" commands.

@bodhiprice
Copy link

@HyperBrain Thank you for this resource! It doesn't look like it's in the official docs, though. Is there a reason? I'm asking because this is super useful information. Would you be interested in having help making a PR?

@falconmick
Copy link

Why isn't this in the docs... This isn't even enough..

@anwarhamr
Copy link

anwarhamr commented Apr 3, 2019

I'm trying to get a file copied to the project directory when the user types sls deploy, can this be done? I've tried so many versions of this

custom:
  scripts:
    hooks:
      'package:initialize': cp ../../deployment_configurations/aura_cloud/${self:service}/env.yml .
      'deploy:createDeploymentArtifacts':  node scripts/replace_stage_in_env.js ${opt:stage, self:provider.stage} && node scripts/createParameters.js ${opt:service, self:service} ${opt:stage, self:provider.stage}

I've tried these but not limited to these...

before:deploy:deploy
before:initialize
deploy:deploy:checkForChanges
deploy:deploy:validateTemplate
...

the second hook works but I've added the cp and get this message when the file does not exist for my env.yml

Serverless Warning --------------------------------------

  A valid file to satisfy the declaration 'file(./env.yml):dev.artifacts.s3ReportingBucket' could not be found.

is it possible to run a script prior to loading the provider object at time of run?

@lielran
Copy link

lielran commented Apr 4, 2019

@anwarhamr you can link your plugin project directly via npm/yarn link

@hayd
Copy link

hayd commented Dec 6, 2019

It reads like before:package:initialize would occur only on the first deploy but actually it occurs every deploy, before:create:create doesn't seem to be triggered. Is there a hook I am missing that would be called on the first deploy only?

@nspeet
Copy link

nspeet commented Sep 14, 2020

Does this list exist for GCP?

@lionheart2013
Copy link

best hooks to use before running a tests?

@joeythomaschaske
Copy link

Adding information on when to use a specific lifecycle hook would be great.

@s1mrankaur
Copy link

s1mrankaur commented Dec 17, 2022

This looks great! I have a quick question.

I am trying to use https://www.serverless.com/framework/docs/guides/output-variables . I have it configured like the

outputs:
  app-version: ${ssm:/app/${self:provider.stage}/versions/${self:service}, ''}

in my serverless.yml file. I update the mentioned SSM parameter using after:aws:deploy:deploy:updateStack hook .

However, the value of the outputs variable that I see on the dashboard is always the last value and not the one that was updated after the deployment. Is there a way to resolve this?

I need to update the SSM value after deployment as the value is the version number that should be updated only after a deployment has gone through.

I thought the serverless dashboard outputs variables could be a good way to keep track of that. Open to suggestions/modifications to make it work.

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