Serverless framework cheat sheet

TL;DR - Summarizing serverless syntax which is often used, yet easily forgotten..

Time and time again when working in a project with the Serverless framework I find myself googling for the same things. I decided to once and for all list the syntactic sugar I so often use.

Cheat sheet

  • Create a new project starting out from a template:

    serverless create --template aws-python3
  • Serverless framework template outline:

    service: name: plugins: - serverless-iam-roles-per-function provider: name: aws runtime: python3.8 functions: MyFirstFunction: handler: handler.handle events: resources: - Resources: SomeBucket: Type: AWS::S3::Bucket Outputs: UsersTableArn:
  • Installing plugins

    npm install --save some-plugin # In your serverless.yml file add the plugin plugins: - some-plugin
  • Define IAM roles per function:

    # add plugin plugins: - serverless-iam-roles-per-function # add IAM role on function level fuctions: MyFirstFunction: ... iamRoleStatements: - Effect: "Allow" Action: - s3:PutObject Resource: 'your-bucket-arn'
  • Referencing variables from the same template:

    provider: name: aws region: ${opt:region, 'eu-west-1'} # Overwrite the default region used. Default is us-east-1 deploymentBucket: name: my-own-bucket-in-${self:provider.region}
  • Giving a default variable for an option:

    # In this case the default for stage is 'test' provider: name: aws runtime: python3.8 stage: ${opt:stage, 'test'}
  • Pseudo parameters references: pseudo parameters are parameters that are predefined by CloudFormation eg. AccountId or Region
# Install plugin plugins: - serverless-pseudo-parameters # Use with # functions: MyFirstFunction: .. events: - s3: bucket: my-bucket-#{AWS::AccountId} event: s3:ObjectRemoved:*
  • Defining other resources in a separate file:

    # In order to define resources in your serverless.yml AND other files use this syntax resources: - ${file(resources/dynamodb.yml)} - Resources: SomeBucket: Type: AWS::S3::Bucket
  • Using the intrinsic function Fn::Join to join multiple values into one string separated by a denominator

    # For example to define the iamRoleStatement we mentioned abouve functions: MyFirstFunction: ... iamRoleStatements: - Effect: "Allow" Action: - s3:PutObject Resource: Fn::Join: - "" - - "arn:aws:s3:::" - Ref: "SomeBucket" - "/*"
  • Referring to another resource in the same stack

    # For example as an environment variable functions: MyFirstFunction: ... environment: BUCKET_NAME: Ref: SomeBucket
  • Using the intrinsic function Fn::GetAtt: to get an attribute of a resource in the same stack

    # either use this: BucketARN: 'Fn::GetAtt': [SomeBucket, Arn] # or this: BucketARN: { Fn::GetAtt: [ SomeBucket, Arn ] }
  • Package functions individually

    service: ... package: individually: true functions: MyFirstFunction: handler: ... package: artifact: hello.jar

    Resources:

  • Serverless framework reference: https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/
  • https://theburningmonk.com/2019/05/cloudformation-protip-use-fnsub-instead-of-fnjoin/
  • Serverless environment variables: https://adamdelong.com/serverless-environment-variables/