Last updated: 2024-04-15
This is a simple repo showcasing how to setup some simple infrastructure in Azure. Namely,
- a
Resource Groupwith aStorage Account. This is created using thecreate_storage_account.shbash script and is intended to be used before using terraform. TheResource Groupwill have all the resources the project might need (each environment –development,production, etc. should have its ownResource Group) and theStorage Accountwill store thetfstate, which is a file that holds the state of the infrastuctureterraformbuilt. Each time a new service is added or a current service is altered/deleted, thetfstatewill be checked to see which infrastructure to change/delete and will update that file accordingly with the changes made. - a
Container Registry, which is used to store Docker Images (this can be the image of an app we are building, for example.). Ideally, this service should be accessed via GitHub Actions (or any other CI/CD provider) and a new version of the image should be added to the registry. - a
Container Instance, which is a service used to run a container. This can be used to run the images stored in theContainer Registryor can directly access theDocker Hub, so that you only need to specify the image and the tag that you wish to use. For example, you may run apostgresqlcontainer and use this for your development database (which should probably be less expensive than instantiating a DB service). - a
Web Appcan be used to host a web application. It can, for example, access youContainer Registryand spin a container with the image you want. You can also deploy, for example, Python code to this service via CI/CD from your repo if you are using Flask/FastAPI. I prefer to use Docker Images, since I've ran into some problems using code deployments (but for simple apps should be enough!).
The repo has the following structure.
.
├── README.md
├── create_storage_account.sh
├── environments
│ ├── development
│ │ ├── main.tf
│ │ ├── provider.tf
│ │ └── variables.tf
│ └── production
└── modules
├── aci
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
├── acr
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
├── app-service
│ ├── main.tf
│ └── variables.tf
└── sql-db
├── main.tf
└── variables.tfIn the modules folder we have several services that we can instantiate. They are reusable and we can change any configurations (CPU, RAM, ports, etc.) that we need to make them more suitable to different environments (development, stage, production, etc.). Each of subfolders inside modules corresponds to a different service.
The environments folder is divided into several environments (for now just production and development) and makes use of the modules in the modules folder to instantiate the infrastructure with the necessary configurations.
If you don't yet have a storage account that serves as backend to store your terraform state files, run sh create_storage_account.sh to create a it. Define the RESOURCE_GROUP_NAME, STORAGE_ACCOUNT_NAME and CONTAINER_NAME .
Then cd to environments/development and run
terraform init
terraform plan
terraform applyThis will create your infrastructure if you haven't already.