DEV Community

Cover image for Your Serverless Deploy Role Doesn't Need Admin Rights: Three-Role Setup
Sascha Mirow-Stanitzok
Sascha Mirow-Stanitzok

Posted on Edited on Originally published at sascha-ms.de

Your Serverless Deploy Role Doesn't Need Admin Rights: Three-Role Setup

You did everything right. GitHub Actions now authenticates to AWS via OIDC, the long-lived access keys are gone from your repository secrets, and the security review is happy. Then someone looks at the role the workflow assumes and finds AdministratorAccess attached, with a comment along the lines of “the framework needs a lot of permissions”.

I keep running into this setup in examples, and I understand why: the Serverless Framework docs do not ship a ready-made least-privilege policy. So I built one and documented it in Serverless Framework GitHub Actions: least-privilege deploys. This post sketches the idea.

Why OIDC alone is not enough here

With AWS CDK, the pipeline mostly just assumes roles from the bootstrap stack. The Serverless Framework works differently: it calls CloudFormation and S3 directly with whatever credentials are active. CloudFormation then creates every resource on behalf of that role.

That means the deploy role itself must be able to create Lambda functions and IAM roles. And anyone who can call iam:CreateRole and iam:PutRolePolicy without restrictions can build a role with any permissions they like. A workflow is code, and code can be changed. A compromised build dependency or a carelessly merged pull request is enough to put those permissions into the wrong hands.

Splitting the job across three roles

The approach I describe separates two questions that are usually answered by one policy: who may trigger a deploy, and what may be created along the way.

  1. The deploy role is what GitHub Actions assumes via OIDC. It may update the service’s stack, upload artifacts to a dedicated bucket and pass exactly one role to CloudFormation. It cannot create functions or IAM roles itself.
  2. The CloudFormation service role (called deploymentRole in serverless.yml) actually creates the resources, limited to the service’s name prefix. It may only create IAM roles that carry a specific permissions boundary.
  3. The function role is generated by the framework as usual, but with that boundary attached. Whatever someone writes into serverless.yml, only the intersection with the boundary takes effect.

The glue is a cloudformation:RoleARN condition on the deploy role: stack changes are only allowed when exactly this service role is passed. A deploy that tries to run with the pipeline’s own permissions fails immediately.

On the framework side, the change is small:

provider:
  deploymentBucket:
    name: ${env:DEPLOYMENT_BUCKET}
  iam:
    deploymentRole: ${env:CFN_EXECUTION_ROLE_ARN}
    role:
      permissionsBoundary: ${env:FUNCTION_BOUNDARY_ARN}
Enter fullscreen mode Exit fullscreen mode

If the boundary line is missing, IAM simply rejects the role creation. The safeguard does not depend on anyone remembering it.

A few lessons from building it

  • Plan the service role from the start. According to the AWS documentation, once a stack has a service role you cannot remove it again. Anyone with permissions on the stack can also use that role, so keep it narrow.
  • Leave DeleteStack out on purpose. No workflow should be able to remove production on the side. Short-lived environments deserve their own role and stack prefix.
  • Use a dedicated deployment bucket. Then the deploy role needs no bucket creation rights and no access to bucket policies.
  • Least privilege is about the dangerous actions. I am fine with lambda:* on functions with a fixed prefix. What must be tightly limited is creating roles, passing roles and deleting stacks. Filing every action down to the bare minimum mostly produces broken deploys after each plugin update.

One more weak spot: Serverless Framework v4 requires an access key that is tied to a user. Use a dedicated key for CI, rotate it and scope the secret to a protected GitHub environment.

What the full article covers

  • A complete, cfn-lint-checked CloudFormation bootstrap template with OIDC provider, bucket, boundary, service role and deploy role
  • The matching serverless.yml, a TypeScript handler and a GitHub workflow you can adopt directly
  • An overview of what the deploy role may and may not do, and why
  • Typical error messages such as AccessDenied on CreateStack or a denied iam:PassRole, with precise fixes

If your deploy role still carries admin rights, the full guide on sascha-ms.de walks you through replacing it step by step.

📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.


👉 Next step: Working with AWS CDK? Grab my printable AWS CDK cheat sheet with commands, concepts and a stack skeleton.

📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).

Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en

Top comments (0)