Prerequisites - AWS credentials must exist in ~/.aws/credentials
or a path that needs to be referred to when the application is deployed. Here is how you can manage AWS credentials
An AWS CloudFormation template is created from the serverless.yml
file. In case a stack has not been created then it would be created when a deployment is requested, using only s3 resources where all the code will be zipped and stored.
Functions are been defined under the functions
key. For example - the function being called hello
as the key should be defined in a file handler.rb
with the name so_long
. AWS allows using layers to share code/libraries across multiple functions. This particular example keeps gem code in layers that can be used across functions.
service: aws-ruby # NOTE: update this with your service name
provider:
name: aws
stage: dev
runtime: ruby2.5
timeout: 300
functions:
hello:
handler: handler.so_long
layers:
- { Ref: GemlayerLambdaLayer }
environment:
GEM_PATH: /opt/2.5.0
layers:
gemlayer:
path: ruby
Use command sls deploy
to deploy the application to AWS. To use a different credential profile than the default, use sls deploy --aws-profile private
. Here, the credentials from the profile private
will be used.
The name of the service is appended before the function name as in the picture and the YAML file. This service name is editable. The stage of the function is also part of the name in the list.
Serverless lets you deploy functions on different stages
. This is analogous to environments (test/staging/production). You can have a different set of environment variables (or stage variables) for each such stage. The deploy and invoke command both accept the stage
parameter for example - sls deploy --stage dummy --aws-profile private
and sls invoke --function hello --stage dummy --aws-profile private
. The stages have been highlighted in the image above. Environment variables can be managed by configuring the YAML file; refer to the docs here.