# Defining DISCOURSE\_S3\_CDN\_URL links to assets in S3 CDN URL

**URL:** https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798
**Category:** Support
**Created:** [May 3, 2019, 9:44pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798 "2019-05-03T21:44:07Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [March 4, 2020, 5:53pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/21 "2020-03-04T17:53:21Z")

</div>

You can check what variables we set here [https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml](https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml)

These are the variables we set related to S3/CDN

- `DISCOURSE_CDN_URL`
- `DISCOURSE_S3_ACCESS_KEY_ID`
- `DISCOURSE_S3_BACKUP_BUCKET`
- `DISCOURSE_S3_CDN_URL`
- `DISCOURSE_S3_REGION`
- `DISCOURSE_S3_SECRET_ACCESS_KEY`
- `DISCOURSE_S3_UPLOAD_BUCKET`
- `USE_DB_S3_CONFIG: true`

I think `DISCOURSE_S3_BUCKET` was deprecated in favor or `DISCOURSE_S3_UPLOAD_BUCKET` and `DISCOURSE_S3_BACKUP_BUCKET`. You should set these instead.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 6:07pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/22 "2020-03-04T18:07:33Z")

</div>

That’s what I assumed, but I get nowhere unless I set both `DISCOURSE_S3_UPLOAD_BUCKET` and `DISCOURSE_S3_BUCKET`. As you can see from the code snippet posted above `use_s3?` is still looking for `s3_bucket` and not `s3_upload_bucket`.

Setting `DISCOURSE_BACKUP_BUCKET` doesn’t resolve that as far as I can tell.

If I set both bucket environment variables I get one step further, but now I’ve come across a new issue, where Discourse is ignoring my S3 CDN url and trying to load assets from my base path. This is causing CSP errors as the Content Security Policy only includes the asset path from my S3 CDN.

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [March 4, 2020, 6:17pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/23 "2020-03-04T18:17:12Z")

</div>

It should be `DISCOURSE_S3_BACKUP_BUCKET` instead of `DISCOURSE_BACKUP_BUCKET`. Other than that if you are setting all the variables with the two Cloudfront distributions should work. If you see our settings we are not defining `DISCOURSE_S3_BUCKET`

I’m not sure if a rebake is needed too.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [March 4, 2020, 6:26pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/24 "2020-03-04T18:26:14Z")

</div>

@Falco As far as I remember one thing that made this very counterintuitive is that there is a difference in behavior between setting `DISCOURSE_S3_CDN_URL` as an environment variable / GlobalSetting and configuring it as a SiteSetting. Maybe that is something you want to pay attention to in your #howto.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 6:29pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/25 "2020-03-04T18:29:14Z")

</div>

Yep sorry that was a typo just in my previous post. `DISCOURSE_S3_BACKUP_BUCKET` doesn’t set `s3_bucket` in `GlobalSetting` for me. I’m not sure how you’re running that rake task without setting `DISCOURSE_S3_BUCKET`.

I really appreciate your help btw and realise this isn’t your problem to solve, so thanks.

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [March 4, 2020, 6:33pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/26 "2020-03-04T18:33:29Z")

</div>

No worries is not an issue. I forgot to mention we also define `USE_DB_S3_CONFIG: true` in our app.yml file. [https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml#L32](https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml#L32)

And I think you are right since this changes the behaviour of how S3 buckets get defined ([discourse/lib/tasks/s3.rake at 427d54b2b00fa94474c0522eaed750452c4e7f43 · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/427d54b2b00fa94474c0522eaed750452c4e7f43/lib/tasks/s3.rake#L43-L64)) and probably it’s a bug.

Check if setting that does the trick for you.

---

<div class="post-metadata">

### Author: ![ufukayyildiz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ufukayyildiz/32/217157_2.png) [@ufukayyildiz](https://meta.discourse.org/u/ufukayyildiz)
#### Post date: [March 4, 2020, 6:40pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/27 "2020-03-04T18:40:31Z")

</div>

Dear @eatcodetravel thanks for your great post.

i am trying to set up cloudfront as you.

Should i upload asset folder to my S3 bucket or will it be automatically.

What will be happen after a discourse update ? Should i upload theme again if it will not be automatically.

thank you.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 6:40pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/28 "2020-03-04T18:40:32Z")

</div>

Of course.

```plaintext
def ensure_s3_configured!
  unless GlobalSetting.use_s3? || use_db_s3_config
    STDERR.puts "ERROR: Ensure S3 is configured in config/discourse.conf or environment vars"
    exit 1
  end
end

```

`use_db_s3_config` is saving you from having to set that extra variable. That must be a bug in `global_setting.rb` because I should be able to just set `DISCOURSE_S3_UPLOAD_BUCKET` unless there is a difference between that and `DISCOURSE_S3_BUCKET`, but I think you’re right that the latter is supposed to be deprecated.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 6:43pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/29 "2020-03-04T18:43:58Z")

</div>

Irrespective of a bug in `global_setting.rb` I’m still seeing an issue where Discourse is looking for assets in their usual place and not on my S3 CDN, even though I’ve declared all my variables and `DISCOURSE_ENABLE_S3_UPLOADS` is set to true.

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [March 4, 2020, 6:48pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/30 "2020-03-04T18:48:49Z")

</div>

There’s a task for this `bundle exec rake uploads:migrate_to_s3 `. Once you have configured your buckets you should run that task to move uploads to s3. S3/CDN has changed in the recent few months and documentation is not up to date, so make sure you backup and prepare in case something goes wrong.

When I was enabling this for the first time we had some downtime while we figure everything out/

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [March 4, 2020, 6:55pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/31 "2020-03-04T18:55:09Z")

</div>

I guess this is because you are missing `DISCOURSE_CDN_URL`. We have it like this in our config

```plaintext
DISCOURSE_CDN_URL: 'https://d16zv78c963s69.cloudfront.net' # this points to the server
DISCOURSE_S3_CDN_URL: 'https://community-cdn-prod.debtcollective.org' # this points to S3 bucket

```

Stylesheets are fetched from the server so they use `DISCOURSE_CDN_URL`. JavaScript it’s uploaded to the S3 bucket and uses `DISCOURSE_S3_CDN_URL`. @Falco explained this to me here.

> [@Assets not serving from CDN in latest update](https://meta.discourse.org/t/assets-not-serving-from-cdn-in-latest-update/140705/13):
>
> If you inspect this page you will see that we we have two different Cloudfront URLs, so we use what I said above, two distributions, one for the normal CDN and another for S3. The problem from the title of the OP is this: CSS come from the app and JS from S3. You need a CDN that is aware of where every single asset is coming from and can reach the necessary place or two CDNs. That is why we have two different settings after all.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 7:01pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/32 "2020-03-04T19:01:09Z")

</div>

Yep I saw that. I’ve set `DISCOURSE_CDN_URL` but I still get the same issue. However I’m tired so maybe I missed something with all the messing around. Will pick this back up tomorrow. Thanks for the help.

---

<div class="post-metadata">

### Author: ![ufukayyildiz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ufukayyildiz/32/217157_2.png) [@ufukayyildiz](https://meta.discourse.org/u/ufukayyildiz)
#### Post date: [March 4, 2020, 7:01pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/33 "2020-03-04T19:01:19Z")

</div>

Will this upload both upload files as images and assets as js files to S3? And should i use a cloudfront url or S3 is enough ? thanks for you help.

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [March 4, 2020, 7:53pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/34 "2020-03-04T19:53:33Z")

</div>

`bundle exec rake uploads:migrate_to_s3` will upload only attachments to S3, this task needs to be run once. After you enable S3 new uploads will be saved in the S3 buckets.

To upload assets you need to use `bundle exec rake s3:upload_assets` and this needs to run after every rebuild/upgrade.

---

<div class="post-metadata">

### Author: ![ufukayyildiz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ufukayyildiz/32/217157_2.png) [@ufukayyildiz](https://meta.discourse.org/u/ufukayyildiz)
#### Post date: [March 5, 2020, 3:06pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/35 "2020-03-05T15:06:39Z")

</div>

> [@eatcodetravel](#):
>
> DISCOURSE\_S3\_CDN\_URL:

i did what you mentioned and uploads to s3 (images etc ) are successful but but for **assets** i get below error

`ERROR: Ensure S3 is configured in config/discourse.conf or environment vars`

i set both

```
DISCOURSE_CDN_URL: 'https://d16zv78c963s69.cloudfront.net' # this points to the server
DISCOURSE_S3_CDN_URL: 'https://community-cdn-prod.debtcollective.org' # this points to S3 bucket

```

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [March 5, 2020, 3:24pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/36 "2020-03-05T15:24:39Z")

</div>

Probably this is related to `USE_DB_S3_CONFIG: true`. Try setting that variable to see if it works.

Here are all the variables I set related to S3/CDN

> [@Defining DISCOURSE\_S3\_CDN\_URL links to assets in S3 CDN URL](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/21):
>
> You can check what variables we set here [infra/modules/services/discourse/web.yml at master · debtcollective/infra · GitHub](https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml) These are the variables we set related to S3/CDN DISCOURSE\_CDN\_URL DISCOURSE\_S3\_ACCESS\_KEY\_ID DISCOURSE\_S3\_BACKUP\_BUCKET DISCOURSE\_S3\_CDN\_URL DISCOURSE\_S3\_REGION DISCOURSE\_S3\_SECRET\_ACCESS\_KEY DISCOURSE\_S3\_UPLOAD\_BUCKET USE\_DB\_S3\_CONFIG: true I think DISCOURSE\_S3\_BUCKET was deprecated in favor or DISCOURSE\_S3\_UPLOAD\_BUCKET and DISCOURSE\_S3\_BACKUP\_BUCKET. You should set …

---

<div class="post-metadata">

### Author: ![ufukayyildiz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ufukayyildiz/32/217157_2.png) [@ufukayyildiz](https://meta.discourse.org/u/ufukayyildiz)
#### Post date: [March 5, 2020, 6:16pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/37 "2020-03-05T18:16:51Z")

</div>

> [@eatcodetravel](#):
>
> Probably this is related to `USE_DB_S3_CONFIG: true` . Try setting that variable to see if it works.

yeah. thanks 🙂. this fixed the problem.

there is an interesting problem, discourse trying to reach asset files from wrong path. it tries to reach from **brotli\_asset path** but there is no this path in S3 bucket.

Also it tries to reach javascripts and stylesheet files from cdn url. But there are not these files in bucket also.

 ![image](https://global.discourse-cdn.com/meta/original/3X/2/0/201a5a4910f389d6ddebdaf3cc19b40c7e5ca995.png)

 ![image](https://global.discourse-cdn.com/meta/original/3X/d/0/d0a130ac0f4b13555f8263e933a7b9c9971e47dd.png)

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 5, 2020, 8:40pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/38 "2020-03-05T20:40:22Z")

</div>

@ufukayyildiz I believe all the answers to your issues are in this thread. I’ve literally just gone through the same setup.

In short, you need to make sure you’ve uploaded your assets once they’ve been precompiled.

---

<div class="post-metadata">

### Author: ![eextra](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eextra/32/170266_2.png) [@eextra](https://meta.discourse.org/u/eextra)
#### Post date: [March 23, 2020, 9:17pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/39 "2020-03-23T21:17:24Z")

</div>

> [@pfaffman](#):
>
> Here’s what I have in the `env` section:
> 
> ```plaintext
> DISCOURSE_S3_ACCESS_KEY_ID: 'key'
> DISCOURSE_S3_SECRET_ACCESS_KEY: 'lock'
> DISCOURSE_BACKUP_LOCATION: 's3'
> DISCOURSE_S3_BUCKET: 'lc-xyz'
> DISCOURSE_S3_BUCKET_NAME: 'lc-xyz'
> DISCOURSE_S3_BACKUP_BUCKET: 'lc-xyz/backups'
> DISCOURSE_S3_UPLOAD_BUCKET: 'lc-xyz/uploads'
> DISCOURSE_S3_CDN_URL: 'https://lc-rbx.s3.amazonaws.com'
> 
> ```
> 
> When I include the `s3 cdn url` the site breaks because all of the links to the assets are on S3 like

Wait  
I don’t understand one thing.

Why set up this data in ENV ?

I’ll give my example.

By setting the data in admin panels to S3 everything works properly and everything loads to S3. I do not have any settings in ENV. Is that bad ? Is it necessary if everything works ?

The second example is different because I can’t migrate the old data to S3 (only new ones work) But here entering this data in app.yml (DISCOURSE\_S3\_ACCESS\_KEY\_ID: ‘key’ etc) file doesn’t change anything and it still doesn’t work (i.e.

> rake uploads:migrate\_to\_s3

does not work

Can you explain that to me?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [March 23, 2020, 9:20pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/40 "2020-03-23T21:20:31Z")

</div>

> [@eextra](#):
>
> Is that bad ? Is it necessary if everything works ?

Nope, the environment variables provide an extra way to configure this stuff, e.g. before you have access to the admin panel.

[Previous page](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798.md?page=1)

[Next page](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798.md?page=3)
