yeroc

yeroc

Creating .env file for secret keys

Hi all!

Working on setting S3 bucket credentials for file uploads with Waffle and ExAws-

I don’t quite understand how to set the variables correctly for the secret keys for the S3 bucket. For example-

config.exs

config :waffle,
  storage: Waffle.Storage.S3,
  bucket: {:system, "AWS_BUCKET_NAME"},
  asset_host: {:system, "ASSET_HOST"}

config :ex_aws,
  json_codec: Jason,
  access_key_id: "123456789abcdef",
  secret_access_key: "987654321asdfghj ",
  region: "US somewhere"

Following along with this example, I can theoretically set env variable to be something like-

export(AWS_ACCESS_KEY_ID = "123456789abcdef")
export(AWS_SECRET_ACCESS_KEY = "987654321asdfghj ")
export(AWS_REGION = "US somewhere")
export(AWS_BUCKET_NAME = "bucket")

Where / how do I create the .env file? Im not understanding that.

Thanks for any help!

Marked As Solved

cmo

cmo

export should be run at the bash (or whatever) prompt. You set the environment variables, then start elixir, which will read them.

That post is telling you to create a file called .env in your project directory.

.env

export AWS_ACCESS_KEY_ID = "123456789abcdef"
export AWS_SECRET_ACCESS_KEY = "987654321asdfghj " 
export AWS_REGION = "US somewhere"
export AWS_BUCKET_NAME = "bucket"

Bash prompt:

source .env
mix phx.server

Edit: I should say that the .env file from that post you linked is not the typical format. See below.

Also Liked

arcanemachine

arcanemachine

1000% yes. Never commit secrets to your git repo.

derpycoder

derpycoder

Here’s how I do it:

I use dotenvy package to read environment variables in. See: GitHub & Hex Package.

In mix.ex:

defp deps do
    [
       ...
       {:dotenvy, "~> 0.6.0"},
       ...
    ]
end

In runtime.exs

# ==============================================================================
# config/runtime.exs is executed for all environments, including
# during releases. It is executed after compilation and before the
# system starts, so it is typically used to load production configuration
# and secrets from environment variables or elsewhere. Do not define
# any compile-time configuration in here, as it won't be applied.
# ==============================================================================
import Config
import Dotenvy

source!(["config/.env.#{config_env()}", System.get_env()])

# ==============================================================================
# ExAws Configuration
# ==============================================================================
debug_requests = env!("DEBUG_REQUESTS", :boolean, false)
aws_access_key_id = env!("AWS_ACCESS_KEY_ID", :string)
aws_secret_access_key = env!("AWS_SECRET_ACCESS_KEY", :string)
max_attempts = env!("MAX_ATTEMPTS", :integer)
base_backoff_in_ms = env!("BASE_BACKOFF_IN_MS", :integer)
max_backoff_in_ms = env!("MAX_BACKOFF_IN_MS", :integer)
s3_host = env!("S3_HOST", :string)

config :ex_aws,
  debug_requests: debug_requests,
  access_key_id: aws_access_key_id,
  secret_access_key: aws_secret_access_key,
  http_client: DerpyCoder.ExAwsHttpClient, # HTTP Client, so ExAws can use Finch instead of Hackney!!
  json_codec: Jason

config :ex_aws, :s3,
  scheme: "https://",
  host: s3_host

config :ex_aws, :retries,
  max_attempts: max_attempts,
  base_backoff_in_ms: base_backoff_in_ms,
  max_backoff_in_ms: max_backoff_in_ms

In .env.dev:

# ExAws Config
AWS_ACCESS_KEY_ID=derp
AWS_SECRET_ACCESS_KEY=wubalubadubdub
MAX_ATTEMPTS=10
BASE_BACKOFF_IN_MS=10
MAX_BACKOFF_IN_MS=10_000
S3_HOST=s3.derpycoder.site
DEBUG_REQUESTS=true

My Start command:

elixir --sname derpycoder --cookie wubalubadubdub -S mix phx.server


P.S. I can include code for the ExAwsHttpClient, that allows usage of Finch here, if you want.

P.P.S. I would suggest that for small files or images, that you do not want to process in the server, you should upload directly to S3 from the front end.
And for larger files, use a TUS server to upload file to the server, and after processing it, upload it to S3 bucket.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement