https://github.com/alejandro945/cqrs-azure
CQRS stands for Command and Query Responsibility Segregation, a pattern that separates read and update operations for a data store. Implementing CQRS in your application can maximize its performance, scalability, and security.
https://github.com/alejandro945/cqrs-azure
azure-app-service azure-application-insights azure-functions azure-sql-database azure-storage cqrs-pattern terraform
Last synced: 7 months ago
JSON representation
CQRS stands for Command and Query Responsibility Segregation, a pattern that separates read and update operations for a data store. Implementing CQRS in your application can maximize its performance, scalability, and security.
- Host: GitHub
- URL: https://github.com/alejandro945/cqrs-azure
- Owner: alejandro945
- Created: 2023-02-26T02:32:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-26T03:45:14.000Z (over 3 years ago)
- Last Synced: 2024-12-27T02:21:32.675Z (over 1 year ago)
- Topics: azure-app-service, azure-application-insights, azure-functions, azure-sql-database, azure-storage, cqrs-pattern, terraform
- Language: HCL
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# CQRS Cloud Design pattern 💥
CQRS separates reads and writes into different models, using commands to update data, and queries to read data.
- Commands should be task-based, rather than data centric. ("Book hotel room", not "set ReservationStatus to Reserved").
- Commands may be placed on a queue for asynchronous processing, rather than being processed synchronously.
- Queries never modify the database. A query returns a DTO that does not encapsulate any domain knowledge.
The models can then be isolated, as shown in the following diagram, although that's not an absolute requirement.
## Automatizing using Terraform
### Install the Azure CLI tool
```bash
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
```
### Authenticate using the Azure CLI
```bash
az login
az login --use-device-code
```
Your browser will open and prompt you to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. Also, you can experiment a localhost redirection that will be blocked by firewall policies so in this case use device code command instead.
```json
[
{
"cloudName": "AzureCloud",
"homeTenantId": "XXXX",
"id": "XXXX",
"isDefault": true,
"managedByTenants": [],
"name": "Azure for Students",
"state": "Enabled",
"tenantId": "XXXX",
"user": {
"name": "XXXX@u.icesi.edu.co",
"type": "user"
}
}
]
```
Set the account with the Azure CLI.
```bash
az account set --subscription "<>"
```
### Create a Service Principal
A Service Principal is an application within Azure Active Directory with the authentication tokens Terraform needs to perform actions on your behalf.
```bash
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/<>"
```
```json
{
"appId": "XXXX",
"displayName": "XXXX",
"password": "XXXX",
"tenant": "XXXX"
}
```
### Set your environment variables
HashiCorp recommends setting these values as environment variables rather than saving them in your Terraform configuration.
In your terminal, set the following environment variables. Be sure to update the variable values with the values Azure returned in the previous command.
```bash
export ARM_CLIENT_ID="<>"
export ARM_CLIENT_SECRET="<>"
export ARM_SUBSCRIPTION_ID="<>"
export ARM_TENANT_ID="<>"
```
### Write configuration
#### Terraform Block
The terraform {} block contains Terraform settings, including the required providers Terraform will use to provision your infrastructure. For each provider, the source attribute defines an optional hostname, a namespace, and the provider type. Terraform installs providers from the Terraform Registry by default. In this example configuration, the azurerm provider's source is defined as hashicorp/azurerm, which is shorthand for registry.terraform.io/hashicorp/azurerm.
You can also define a version constraint for each provider in the required_providers block. The version attribute is optional, but we recommend using it to enforce the provider version. Without it, Terraform will always use the latest version of the provider, which may introduce **breaking changes**.
#### Providers
The ```provider``` block configures the specified provider, in this case azurerm. A provider is a plugin that Terraform uses to create and manage your resources. You can define multiple provider blocks in a Terraform configuration to manage resources from different providers.
#### Resource
Use ```resource``` blocks to define components of your infrastructure. A resource might be a physical component such as a server, or it can be a logical resource such as a Heroku application.
### Initialize your Terraform configuration
Initialize your learn-terraform-azure directory in your terminal. The terraform commands will work with any operating system.
```bash
terraform init
```
### Apply and destroy commands 🚦
```bash
terraform apply
terraform destroy
```
### Creation of SQL Sync service
1. In each database create the share schema of the entities
```sql
CREATE TABLE Persons(
ID int NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
```
2. Go to your hub or source database (Write database) [Tutorial](https://learn.microsoft.com/en-us/azure/azure-sql/database/sql-data-sync-sql-server-configure?view=azuresql#add-sync-members).
###Â Upload your code to azure function
```bash
func azure functionapp publish cqrs-command-varela #Inside command app folder
func azure functionapp publish cqrs-query-varela #Inside query app folder
```