S3 bucket name based on stage name, but prod being unique

I was reading up on serverless conditions here:

But I can’t quite figure out how to implement my specific scenario.

It’s pretty straight forward, I think:
In prod, the bucket name should be foobar.example.com.
But in all other environments, it should be foobar-XXX.example.com, where XXX is the name of the stage.

I was thinking of passing it in as a command line arg, where the default is the name of the stage:

custom:
  # Command line args to serverless:
  bucket_prefix: ${opt:bucket_prefix, ${self:provider.stage}}
  stage: ${opt:stage, self:provider.stage}

But it seems weird referring to one command line arg to another.

I was also thinking of doing a mappings under “resources”, but not sure how to do “wildcards”:

  Mappings:
    Bucket_Prefix:
      prod:
        bucket_prefix: ""
      *:
        bucket_prefix: "-${self:provider.stage}"

How can I get the bucket name to be foobar.example.com for prod, and foobar-XXX.example.com for all other environments?

Thanks!

Should be doable if I understand correctly… it might looks something like this…

serverless.yml (top)

service: klick-com
frameworkVersion: ">=1.9.0 <2.0.0"

custom:
  vars: ${file(vars.yml):${opt:stage, self:provider.stage}, file(vars.yml):dev}

vars.yml

default: &default
  REGION: ${opt:region, self:provider.region}
  STAGE: ${opt:stage, "dev"}
  STACKNAME: ${self:service}-${self:custom.vars.STAGE}
  BUCKET_PREFIX: foobar-${self:custom.vars.STAGE}
  BUCKET_NAME: ${self:custom.vars.BUCKET_PREFIX}.example.com

dev:
  <<: [*default]

prod:
  <<: [*default]
  BUCKET_PREFIX: foobar

serverless.yml (bottom)

resources:
  Description: Klick Site
  Resources:
    DefaultBucket:
      Type: "AWS::S3::Bucket"
      Properties:
        AccessControl: Private
        BucketName: ${self:custom.vars.BUCKET_NAME}

huh. I’m wasn’t familiar with the syntax of <<: [*default]

This is awesome! Thanks!

Yup. You should still be able to do what you want without that merge syntax… but it was next level when I first discovered it. It’s important to know though that it only merges first level things. It’s not a deepmerge.