ตัวอย่าง
ต้องการ Deploy โปรเจค ASP.NET Core ชื่อ MyWeb
แบบ Container บน Heroku ในการสร้าง App บน Heroku จะใช้ Terraform แทนการ Log in ไปสร้าง App ผ่านหน้าเว็บ
Terraform จะสร้าง App ชื่อ myweb-101
โดยเลือก Stack เป็น container
และ Region เป็น us
ขั้นตอน
- ติดตั้ง CLI
Heroku
และTerraform
$ brew install heroku
$ brew install terraform
2. สร้างโปรเจคชื่อ MyWeb
$ dotnet new webapi --language C# --output src/MyWeb
3. เพิ่ม Dockerfile
สำหรับ Build และรันโปรเจค โดยใช้ .NET SDK เวอร์ชัน 3.1
4. เพิ่มไฟล์ heroku.yml
โดยระบุ Dockerfile
และ Entry point
4. เพิ่มโค้ด Terraform ชื่อ main.tf
5. Log in เข้า Heroku ผ่าน Command line
$ heroku login
6. สร้าง App และ Deploy ด้วย Terraform ผ่านคำสั่ง terraform apply
$ terraform init
$ terraform apply -auto-approveterraform apply -auto-approve
heroku_app.myweb: Refreshing state... [id=myweb-101]
heroku_build.myweb: Creating...
heroku_build.myweb: Still creating... [10s elapsed]
heroku_build.myweb: Still creating... [20s elapsed]
heroku_build.myweb: Still creating... [30s elapsed]
heroku_build.myweb: Still creating... [40s elapsed]
heroku_build.myweb: Still creating... [50s elapsed]
heroku_build.myweb: Still creating... [1m0s elapsed]
heroku_build.myweb: Still creating... [1m10s elapsed]
heroku_build.myweb: Still creating... [1m20s elapsed]
heroku_build.myweb: Still creating... [1m30s elapsed]
heroku_build.myweb: Still creating... [1m40s elapsed]
heroku_build.myweb: Still creating... [1m50s elapsed]
heroku_build.myweb: Still creating... [2m0s elapsed]
heroku_build.myweb: Creation complete after 2m1s [id=9a54b892-f494-40b5-9c61-a798d68d0e1b]Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Outputs:app_url = https://myweb-101.herokuapp.com
7. จะได้ผลลัพธ์ คือ app_url
โดยสามารถเช็คว่าโปรแกรมทำงานถูกต้องหรือไม่ ผ่าน Default Controller ของ ASP.NET คือ https://myweb-101.herokuapp.com/WeatherForecast
8. เมื่อ Log in เข้า dashboard.heroku.com จะพบว่ามี App ใหม่ชื่อ myweb-101
โดยมีรายละเอียดตรงกับที่ระบุไว้ในไฟล์ Terraform main.tf
9. ถ้าไม่ต้องการลบ App สามารถใช้คำสั่ง terraform destroy
$ terraform destroy -auto-approve