2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ハンズオン Terraform(ECS, Fargate, RDS) × Next.js × Gin(Go)

Last updated at Posted at 2025-04-13

Video to GIF Converter.gif

初めに

今回はTerraform(ECS, Fargate, RDS) と Next.js と Gin(Go)でハンズオンを作成したのでそれを説明していきます!

注意事項

AWSからの請求でおおよそ5$程度かかることに注意が必要です!

  • Route53 で 3$
  • ECS & RDSで 2$程度

その1 ローカル用意

今回のリポジトリをローカルに用意
git clone https://github.com/masaaki-imai/terraform_and_next_and_go

Github

その2 コマンドの用意

必要に応じてAWSアカウントの用意、awsコマンド、terraformコマンドの用意
awsコマンドの設定

brew install awscli
aws configure
brew install terraform

その3 ドメイン取得

AWSのコンソールのRoute53にて適当なドメインを取得してもらえればと思います。
例えば、「tekitou.click」とかがサンプルですが安いものだと3ドル程度で取得できると思います。

Screenshot 2025-04-12 at 14.45.23.png

その4 ECR用意

次の2つを用意

  • myapp-go
  • myapp-next

Screenshot 2025-04-13 at 15.37.31.png

その5

goのアプリケーションをECRにpush

cd go
make login
make deploy
make echo_ecr_url

※(注1)「make echo_ecr_url」のURLは後で使うのでコピーしてください

その6

同じくnext.jsのアプリケーションをECRにpush

cd ..
cd next
make deploy
make echo_ecr_url

※(注2)「make echo_ecr_url」のURLは後で使うのでコピーしてください

その7

parameter storeにて、
/myapp/prod/db_passwordpassword
/myapp/prod/jwt_secret_keyjwt-secret-key

Screenshot 2025-04-13 at 15.43.24.png

Screenshot 2025-04-13 at 15.44.36.png

その8 terraform実行前にproduction.tfvarsの編集

  • s3で適当な名前のバケットを作成
    • 「test_bucket」など

infrastructure-prod.configを編集
※YOU HAVE TO CHANGE THIS!の部分を必要に応じて編集

key="PROD/infrastructure.tfstate"
bucket="test_bucket" # ※YOU HAVE TO CHANGE THIS!
region="ap-northeast-1" # ※YOU HAVE TO CHANGE THIS!

その9 production.tfvarsを編集

ecs_domain_nameに先程 Route53にて取得したドメイン名を入れる
docker_backend_image_urlに(注1)を入れる
docker_frontend_image_urlに(注2)を入れる

その10 terraform実行

cd ..
cd infra
terraform init -backend-config="infrastructure-prod.config"
terraform plan -var-file=production.tfvars
terraform apply -var-file=production.tfvars

その11

https://www.<先程取得したdomain>
でアクセス可能

※例えばドメイン名が「tekitou.click」の場合、次のようになります
https://www.tekitou.click

Screenshot 2025-04-13 at 14.08.43.png

その12

1日あたり600円ほど金額の請求がくるので、検証が終わったら最後に次のコマンドで削除しておく
terraform destroy -var-file=production.tfvars

以上です!

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?