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

**URL:** <https://forum.serverless.com/t/s3-bucket-name-based-on-stage-name-but-prod-being-unique/7930>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [April 8, 2019, 2:43pm UTC](https://forum.serverless.com/t/s3-bucket-name-based-on-stage-name-but-prod-being-unique/7930 "2019-04-08T14:43:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grayaii](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/grayaii/32/2848_2.png) [@grayaii](https://forum.serverless.com/u/grayaii)\
**Post date:** [April 8, 2019, 2:43pm UTC](https://forum.serverless.com/t/s3-bucket-name-based-on-stage-name-but-prod-being-unique/7930/1 "2019-04-08T14:43:54Z")

</div>

I was reading up on serverless conditions here:

> [@Conditional serverless.yml based on stage?](http://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/9):
>
> @alechewitt Not sure what is happening in your example… Is the “self.enabled.other” part a typo? Should it not be… "${self:custom.enabled.other}" also… I didn’t know it was legit to set vars outside of “custom”?

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](http://foobar.example.com).  
But in all other environments, it should be [foobar-XXX.example.com](http://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](http://foobar.example.com) for prod, and [foobar-XXX.example.com](http://foobar-XXX.example.com) for all other environments?

Thanks!

---

<div class="post-metadata">

**Author:** ![iDVB](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/idvb/32/990_2.png) [@iDVB](https://forum.serverless.com/u/iDVB)\
**Post date:** [April 8, 2019, 4:28pm UTC](https://forum.serverless.com/t/s3-bucket-name-based-on-stage-name-but-prod-being-unique/7930/2 "2019-04-08T16:28:20Z")

</div>

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}
```

---

<div class="post-metadata">

**Author:** ![grayaii](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/grayaii/32/2848_2.png) [@grayaii](https://forum.serverless.com/u/grayaii)\
**Post date:** [April 8, 2019, 4:51pm UTC](https://forum.serverless.com/t/s3-bucket-name-based-on-stage-name-but-prod-being-unique/7930/3 "2019-04-08T16:51:32Z")

</div>

> [@iDVB](#):
>
> dev: \<\<: [\*default]

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

This is awesome! Thanks!

---

<div class="post-metadata">

**Author:** ![iDVB](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/idvb/32/990_2.png) [@iDVB](https://forum.serverless.com/u/iDVB)\
**Post date:** [April 8, 2019, 5:26pm UTC](https://forum.serverless.com/t/s3-bucket-name-based-on-stage-name-but-prod-being-unique/7930/4 "2019-04-08T17:26:46Z")

</div>

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.
