# SAM and Serverless Framework, on the same flat

> Both of them write CloudFormation for you. Two weekends porting the stack from one to the other, and the difference that mattered was IAM.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2021-08-01
- Tags: AWS, SAM, Serverless Framework, CloudFormation, Infrastructure as Code, IAM, Serverless
- URL: https://deyanpeev.com/posts/sam-and-serverless-framework-on-the-same-flat

I spent a Saturday finding out what SAM hands CloudFormation, and the obvious
next question is whether the other tool that writes CloudFormation for you
writes better CloudFormation. So I ported the flat to `serverless.yml` (five
functions, the device registry, the IoT rule, the schedules, the alarms) and
deployed it beside the real stack under a different stage.

This isn't a runtime comparison. Both of them produce a CloudFormation template
and hand it to CloudFormation. You can see Serverless Framework's with
`serverless package`, which leaves it in
`.serverless/cloudformation-template-update-stack.json`. Same service, same
change sets, same eventual consistency, same three-in-the-morning rollback
behaviour. So everything below is about the authoring layer and the CLI,
there is not much else for them to differ about.

# One role or five

SAM attaches policies per function. The sensor ingest function gets CRUD on one
table and that's all it gets, because that is where its `Policies:` block is:

```yaml
SensorIngestFunction:
  Type: AWS::Serverless::Function
  Properties:
    Policies:
      - DynamoDBCrudPolicy:
          TableName: !Ref DeviceRegistryTable
```

Serverless Framework, by default, gives the service a single role:

```yaml
provider:
  name: aws
  runtime: python3.8
  region: eu-west-1
  iamRoleStatements:
    - Effect: Allow
      Action: [dynamodb:GetItem, dynamodb:PutItem, dynamodb:UpdateItem]
      Resource: !GetAtt DeviceRegistryTable.Arn
    - Effect: Allow
      Action: [iot:Publish]
      Resource: '*'
```

Every function in the service can do everything in that list. The function that
reads a temperature can publish to IoT, and the one that answers Alexa can
write to the table. Nothing in my flat is going to be attacked over this, but
the way the file is laid out by default pushes you towards one union of every
permission any function needs, and that list only ever gets longer.

There is a plugin, `serverless-iam-roles-per-function`, and it works. But
per-function IAM being a plugin and not the default tells you what the tool
considers normal. SAM's per-function policies plus the policy template list
is the strongest thing it has, I think, and this was the point where I felt it.

# Variables, where Serverless Framework is simply better

CloudFormation has parameters, mappings and `!Sub`. Serverless Framework has a
resolver that reads whatever you point it at:

```yaml
custom:
  stage: ${opt:stage, self:provider.stage}
  config: ${file(./config/${self:custom.stage}.yml)}

provider:
  environment:
    SKILL_ID: ${ssm:/home-automation/skill-id}
    TABLE: ${self:custom.config.tableName}
```

The SAM equivalent of that SSM lookup is a template parameter typed
`AWS::SSM::Parameter::Value<String>` plus a line in `samconfig.toml`, and the
equivalent of the per-stage config file is a `Mappings:` block, a lookup
table pretending to be a data structure. It's not close on this one.
Serverless Framework treats the deployment as something with inputs and SAM
treats it as a document with parameters, and you notice it every single time
you want the dev stack to point somewhere else.

# Stages

`sls deploy --stage dev` does the obvious thing. Every resource name carries the
stage, so two stacks coexist without you thinking about it. SAM's answer is a
`samconfig.toml` with an environment table and `--parameter-overrides`, and
resource naming you do yourself with `!Sub '${AWS::StackName}-...'` on every
resource that needs a name.

I only have one stage here, so this cost me nothing. At work it would have
cost me a lot.

# The deploy loop

One thing I didn't expect to care about is single-function deploys:

```bash
sls deploy function -f sensorIngest
```

That uploads a zip and calls `UpdateFunctionCode`, and it takes about five
seconds. SAM has no equivalent. `sam deploy` is a change set every time, and for
this stack that's somewhere between forty and ninety seconds depending on what
moved. Over a weekend of fiddling with one handler, that is the difference
between staying in the problem and going to make tea.

The downside is that `sls deploy function` skips CloudFormation (that's how it
gets to be fast), so the stack and the deployed code drift apart until the next
full deploy. Fine for one person. On a team it stops being fine.

SAM's answer to the same problem is `sam local invoke` against an event file.
I use it constantly, and for a stack like mine it's better than the framework's
equivalent, because `serverless-offline` is built around HTTP APIs and there is
no HTTP anywhere in this flat.

# Plugins are the ecosystem and the bill

Everything Serverless Framework doesn't do natively, it does with a plugin.
State machines are `serverless-step-functions`. Vendored Python dependencies
with native extensions are `serverless-python-requirements` with
`dockerizePip: true`. Both work well.

But both also mean that a Python project on a machine with no Node.js installed
now has a `package.json`, a `node_modules`, and a version matrix between the
framework and each plugin that upgrades independently of it. SAM ships one
binary from AWS and its state machine support is a resource type.

In fairness, the plugin list is also why Serverless Framework can do things SAM
has no story for at all. If I needed one of those tomorrow I'd install it
without much soul-searching.

# A company behind the CLI

Serverless Framework is a product with a company behind it. There's a
dashboard, there are `org` and `app` keys in the file, the CLI would like you to
log in, and it phones home unless told not to. I don't think any of that is
unreasonable. But it puts a third party in the path between my laptop and my own
AWS account, for a stack that turns lamps on.

SAM is a CloudFormation transform and a CLI, both from the vendor whose API I'm
calling anyway. For this project that decides it. For a project with a delivery
deadline I accept it would decide nothing at all.

# Where I landed

The flat stays on SAM. Per-function IAM by default, no second vendor, and the
local invoke loop is the one I use anyway.

If I were starting something with three stages and four people, I'd want
Serverless Framework's variables and its stage handling badly enough to take
the plugin sprawl, and I would install `serverless-iam-roles-per-function` on
day one before anyone had the chance to grow the shared role.

What nags at me after two weekends is that both files are YAML with a
preprocessor in front. Every argument above is about whose preprocessor is
better. What I keep wanting is a loop. Four schedules are four near-identical
blocks in both tools. That's a different post, and I think I am going to have
to write the stack a third time to have it.
