DEV Community

Cover image for Deploy Elixir + Phoenix to AWS ECS with GitLab CI and Terraform

Deploy Elixir + Phoenix to AWS ECS with GitLab CI and Terraform

Aleksi Holappa on October 04, 2020

On my first article we created a nice local development environment for Elixir and Phoenix with Docker and docker-compose. Now it's time to write s...
Collapse
 
organicnz profile image
Tarlan Isaev 🍓 • Edited

Hi mate,
thanks for the great and educational article :)
Could you pls help with this issue by any chance? :)

terraform plan

Error: Reference to undeclared resource

on deploy_user.tf line 24, in resource "aws_iam_user_policy" "ci_ecr_access":
24: "Resource": "${aws_ecr_repository.myapp_repo.arn}"

A managed resource "aws_ecr_repository" "myapp_repo" has not been declared in
the root module.

Collapse
 
hlappa profile image
Aleksi Holappa

This basically means that the ECR repository you are referring to in the user policy does not exist. Check the naming of the ECR repository resource.

I fixed one typo from the article, there was unnecessary "_url" in the ECR naming, sorry about that!

Collapse
 
organicnz profile image
Tarlan Isaev 🍓

Thanks for getting back to me, mate :)

Sorry that asking such silly question. Could I fix it easily by adding that ECR repo onto AWS before runnig Terrafrom commands? Unfortunatelly, I haven't touched this ECR service, yet :)

Oh, that's tottaly fine, my typos are beond my understanding lol

Thread Thread
 
hlappa profile image
Aleksi Holappa

Well, you can add it manually through AWS console if you want to. Also, you can just rename the resource and Terraform will destroy the old one and create a new one with the new name. :)

Thread Thread
 
organicnz profile image
Tarlan Isaev 🍓

Thanks bro, sorry, I didn't correct that typo earlier, I thought it's gonna be a next iteration of the following article heh :)

Collapse
 
organicnz profile image
Tarlan Isaev 🍓

Couldn't trace out this error yet when pusshing code to my GitLab repo. Have you seen it before?

$ which elixir
/usr/local/bin/elixir
$ mix --version
Erlang/OTP 22 [erts-10.7.2.4] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1]
Mix 1.10.4 (compiled with Erlang/OTP 22)
$ ln -s /usr/local/bin/mix /usr/bin/mix
$ mix local.rebar --force

  • creating /root/.mix/rebar
  • creating /root/.mix/rebar3 $ mix local.hex --force
  • creating /root/.mix/archives/hex-0.20.5 $ mix deps.get --only test ** (Mix) Could not find a Mix.Project, please ensure you are running Mix in a directory with a mix.exs file Cleaning up file based variables 00:01 ERROR: Job failed: exit code 1
Collapse
 
hlappa profile image
Aleksi Holappa • Edited

Is this output on the CI or on your local machine? If local, what OS are you using?

Collapse
 
organicnz profile image
Tarlan Isaev 🍓 • Edited

Yeah, on CI. I've already pushed that code to my repo :)
pastebin.com/vdbDW8Um
gitlab.com/organicnz/myapp-terraform

Thread Thread
 
hlappa profile image
Aleksi Holappa • Edited

I see your problem! Please place the .gitlab-ci.yml to repository which contains your Elixir + Phoenix application. It should be placed to the root of the repository.

Second thing, never push the state files publicly to any platform. They should be ignored with .gitignore, crypted with git-crypt (or other encrypting tool) or the best solution would be to use remote state.

Thread Thread
 
organicnz profile image
Tarlan Isaev 🍓 • Edited

Would it be appropriate if I use your Elixir application for the CI github.com/hlappa/microservice_exe... or anyone? :)

Jeez, that's right I completely forgot to sanitise that area, already added .gitignore file then will try to figure out how to push it to the remote state on AWS :)

Collapse
 
mstibbard profile image
mstibbard • Edited

Thanks for the fantastic write up!

1) FYI as of 1 October 2020 the GitLab free tier was reduced to 400 minutes of CI/CD.

2) The initial CI/CD run failed for me with below message in the test stage. I haven't figured out the fix for this yet.

** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
    (db_connection 2.3.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Hello.Repo couldn't be created: killed
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1
Enter fullscreen mode Exit fullscreen mode

3) Further down in the tutorial (code block for rds.tf) you've got:

availability_zone = var.aws_default_zone
Enter fullscreen mode Exit fullscreen mode

but you haven't defined it in your variables.tf

Collapse
 
hlappa profile image
Aleksi Holappa

Sorry for a late reply to you comment, good points!

1) Article is now updated. I didn't know GitLab reduced the minutes from 2000 to 400, which is kinda sad :(

2) Make sure you have Postgres as a service for the test job, the DB url is correctly setup and check your config/test.ex that is has proper configuration. Seems like when establishing the connection to DB fails.

3) Added to variables.tf! :)

Collapse
 
neoecos profile image
Sebastian Ortiz V.

Hi @Aleksi! Thanks for the article.

Please update the ecs task with the DATAABASE_URL envnvar.
This works.

{"name": "DATABASE_URL", "value": "ecto://${var.db_username}:${var.db_password}@${aws_db_instance.default.address}:${aws_db_instance.default.port}/${var.db_name}"}