# Cloud Costs (Out of Cluster Costs)

The **Cloud Cost** functionality allows Kubernetes admins to allocate out of cluster costs to Kubernetes environments.&#x20;

Out of cluster costs e.g. costs for cloud services like S&#x33;**,** RDS or Lambda can be allocated to individual Kubernetes artefacts like **Namespaces** as well as **Labels**.

The ability to allocate out of cluster costs to Kubernetes artefacts provides a more accurate picture of the costs of that Kubernetes artefact and by extension of the entire Kubernetes environment. &#x20;

The **Cloud Cost** functionality supports **AWS** and **Azure** services as of now. Additionally, out of cluster costs can be allocated to Kubernetes Namespaces and Labels. Support for other cloud providers and allocating costs to other Kubernetes artefacts like clusters, deployments, services etc is on the roadmap. &#x20;

Below we will walkthrough the process of allocating cloud service costs for both AWS and Azure to Kubernetes Namespaces and Labels.

Let's start off with AWS.

### Allocating AWS Service Costs

To allocate AWS service costs you first need to add AWS billing credentials to your Replex account. Follow [this](https://docs.replex.io/settings/billing-credentials) guide to add billing credentials for AWS.\
\
Please add the following permission to the policy:

&#x20;\
`{`\
&#x20;   `"Version": "2012-10-17",`\
&#x20;   `"Statement": [`\
&#x20;       `{`\
&#x20;           `"Sid": "VisualEditor1",`\
&#x20;           `"Effect": "Allow",`\
&#x20;           `"Action": [`\
&#x20;               `...`\
&#x20;               `"ce:GetTags"`\
&#x20;           `],`\
&#x20;           `"Resource": "*"`\
&#x20;       `},`\
&#x20;       `...`

{% hint style="info" %}
In addition, the following permission must be added for the AWS cost allocation tags to work:\
CostExplorer Permission: **ce:GetTags**
{% endhint %}

Once the billing integration is complete we are ready to allocate AWS service costs to Kubernetes Namespaces and Labels.

#### Allocating AWS Service Costs to Namespaces

Below we walkthrough an example of tagging S3 on AWS and allocating it's costs to a Namespace in our Kubernetes environment. Costs for other AWS services like RDS, Lambda and ElasticCache can also be allocated by following the steps outlined below.

For more advanced tagging practices please refer to [this](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html) AWS tagging guide.

#### Tag Resources/Services

To enable the allocation of external AWS service costs to Kubernetes artefacts the cloud cost functionality utilizes the AWS tagging mechanism.&#x20;

To apply the relevant tag, browse to the S3 dashboard on your AWS console and click on the bucket name you want to add the tag to. Next click on the **Properties** tab in the S3 bucket screen, scroll down to the **Tag** section and click **Edit**.

On the next screen click **Add Tag**.&#x20;

Enter the following value in the tag **Key** field:

**CLOUDCOST\_TAG\_KEY\_NAMESPACE**&#x20;

Enter the name of the Namespace you want to add the S3 costs to in the tag **Value** field.

Click **Save changes**.

![](https://4068579783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGA2pJIFrMdGZ5i84m%2F-MYjgGmRFyUv-6r0DG8O%2F-MYjgTtQhN-3K9B8sfHZ%2FS3-Management-Console.png?alt=media\&token=18e049e6-4f4d-4b88-a2a1-c91275bde2e8)

#### Enable User Defined Cost Allocation Tags

The tags applied in the previous step need to be activated in the AWS console. To do this browse to the **AWS Billing & Cost Management Dashboard**.&#x20;

Click on **Cost allocation tags** in the panel on the left hand side. In the next screen, under the User-defined cost allocation tags section, choose the tag added in the previous step and click on **Activate**.

Please be aware that tags added in the previous step may take several hours to populate in the User-defined cost allocation tags section.

#### Allocating AWS Service Costs to Labels

In this section we walkthrough an example of tagging S3 on AWS and allocating its costs to a **Label** in our Kubernetes environment.&#x20;

Follow the steps outlined in the Namespaces example above to navigate to the **"Edit Bucket Tagging"** screen for the S3 bucket you want to allocate costs for.&#x20;

Enter the following in the tag **Key** field:

**CLOUDCOST\_TAG\_KEY\_LABEL\_\<K8s\_Label\_Key>**

and

**\<K8s\_Label\_Value>**

in the tag **Value** field.

{% hint style="info" %}
**K8s\_Label\_Key** and **K8s\_Label\_Value** in the example above depend on the Kubernetes Label that you want to allocate the costs to.

The following tag **Key** and **Value** will be used for the Kubernetes Label **"app" : "postgres":**

Tag Key: **CLOUDCOST\_TAG\_KEY\_LABEL\_app**

Tag Value: **postgres**

Similarly for **"tier" : "frontend"** the following tag **Key** and **Value** will be used:

Tag Key: **CLOUDCOST\_TAG\_KEY\_LABEL\_tier**

Tag Value: **frontend**
{% endhint %}

For our example we use the "app" : "postgres" label.

![](https://4068579783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGA2pJIFrMdGZ5i84m%2F-McPMUVtejTmUiRUF2Du%2F-McPNW0zwWKD10bUJOE2%2FS3-Management-Console%20\(1\).png?alt=media\&token=083cb6a4-6f71-4112-ba65-d32b3eca2321)

Click **Save changes**.

Next activate the tag we just added by navigating to the **User-defined cost allocation tags** section of the **AWS Billing & Cost Management Dashboard.**

### Allocating Azure Service Costs

As with AWS, the first step in allocating Azure service costs is to add Azure billing credentials to your Replex account. Follow [this](https://docs.replex.io/settings/billing-credentials) guide to add billing credentials for Azure.

Once the billing integration is complete we are ready to allocate Azure service costs to Kubernetes Namespaces and Labels.

#### Allocating Azure Service Costs to Namespaces

Below we walkthrough an example of tagging an Azure SQL database and allocating it's costs to a Namespace in our Kubernetes environment. Costs for other Azure services can also be allocated by following the steps outlined below.

#### Tag Resources/Services

Navigate to the relevant SQL database on your Azure console and click on the database name you want to add the tag to.&#x20;

Next click on **tags** in the left hand panel and enter the following in the **Name** field:

**CLOUDCOST\_TAG\_KEY\_NAMESPACE**&#x20;

Enter the name of the Namespace you want to allocate the SQL costs to in the tag **Value** field.

Click **Apply**.

![Tag SQL Database](https://4068579783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGA2pJIFrMdGZ5i84m%2F-McTuJpS3LU_NmZrXqtP%2F-McU77mzguayPgv6zR00%2FCloudCostsTest-cloudcost-testserver-CloudCostsTest-Microsoft-Azure.png?alt=media\&token=0b0873b7-feb8-452f-8284-497c6dc13c88)

#### Activate Azure Tag

The tags applied in the previous step need to be activated in the Azure console. To do this navigate to the **Azure Cost Management + Billing** service.&#x20;

In the **Azure Cost Management + Billing** dashboard click on **Cost Management** in the left hand panel. In the next screen, scroll down to the **Settings** section and click on **Configuration**.&#x20;

In the next screen click on **Tags** and add the tag we created in the previous step.&#x20;

Click **Apply**.

![Activate Azure Tag](https://4068579783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGA2pJIFrMdGZ5i84m%2F-McU7EIEtP6pEZDvF-9M%2F-McU96KGPUdhKOBI3GzL%2FTags-Microsoft-Azure.png?alt=media\&token=5a19c41b-a829-4f50-a5a4-86b3eea2010f)

#### Allocating Azure Service Costs to Kubernetes Labels

In this section we walkthrough an example of tagging an Azure SQL database and allocating its costs to a **Label** in our Kubernetes environment.&#x20;

Follow the steps outlined in the example above to navigate to the **Tags** section of the SQL database you want to allocate costs for.&#x20;

Enter the following in the tag **Key** field:

**CLOUDCOST\_TAG\_KEY\_LABEL\_\<K8s\_Label\_Key>**

and

**\<K8s\_Label\_Value>**

in the tag **Value** field.

Click **Apply**.

Activate the tag we just added by navigating to the **Configuration** section of the **Azure Cost Management + Billing** dashboar&#x64;**.**

### Viewing Allocated Cloud Costs

Once tagged the costs for the Kubernetes **Namespaces** or **Labels** will populate in the Replex UI.&#x20;

External cloud costs can be viewed in the **Namespaces Overview** screen of the **Cluster Dashboard**.&#x20;

External cloud costs are accumulated in both the **Allocated Cost Combined** and **Allocated Cost Detailed** charts of the **Namespaces Overview**.&#x20;

Additionally cloud costs for each namespace are provided in the **Cost Details** section of the **Namespace Overview** in a separate **External Cost** column.

![External Cloud Costs Namespace Overview](https://4068579783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGA2pJIFrMdGZ5i84m%2F-MYJpV3J9Nbdpca5vJsw%2F-MYK-V5AW3b8ufl3z9n4%2Fexternal-cloud-costs-1.png?alt=media\&token=deaf7c29-ba1a-4b3b-9548-daaa21c7ef5a)

The **Detailed Namespace View** also provides a cost breakdown for individual out of cluster resources allocated to a Namespace. It outlines both the **Resource Type** as well as its cost for the time period chose&#x6E;**.**&#x20;

Cost breakdowns for out of cluster resources can be viewed by clicking on **Show External Resources** in the **Detailed Namespace View.**

![Cost Breakdown for Out of Cluster Resources](https://4068579783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGA2pJIFrMdGZ5i84m%2F-MYK0__nvF3Kwjzb22td%2F-MYK1Cp_cOTgJtziTJSK%2Fexternal-cloud-costs-2.png?alt=media\&token=4d83f4cd-c30a-4add-8434-1ff3ae94b533)

External cloud costs can also be viewed on the [Teams Dashboard](https://docs.replex.io/getting-started/teams-dashboard). Each individual Team dashboard outlines external cloud costs under the External tab.

The **External** tab breaks down cloud costs by Resource Type as well as the Namespace/Label they are assigned to.

![External Cloud Costs Teams Dashboard](https://4068579783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFGA2pJIFrMdGZ5i84m%2F-McUYU3KxcdA4BY2Onck%2F-McUZkEmVAhMIm-gNKsb%2FTest-Team-Replex-cost-allocation.png?alt=media\&token=4d41a1e6-37ad-4109-8144-81ad7e4a5f42)
