Environment Manager - Cost Estimation - Rafay Product Documentation

Cost Estimation

Cost Estimation for infrastructure resources can be included in Environment Manager through the use of Infracost as a hook within EM Resource Templates.


Create Image

You will first need to create a custom Infracost container image that will be run within your Environment Manager Agent during Environment build operations. This container will be used to estimate the infrastructure costs of the EM environment resources. This image is based off of the official Infracost image.

Save the following script as cost.sh

cd /tmp
curl -H "X-Engine-Helper-Token:$DOWNLOAD_TOKEN" -o ./job.tar.zst $DOWNLOAD_URL
unzstd ./job.tar.zst
mkdir terraform
tar -xf job.tar -C ./terraform
ls -al ./terraform
infracost breakdown --path ./terraform/$TF_PATH

Save the following docker file as Dockerfile

FROM infracost/infracost:ci-0.10

COPY cost.sh /  
RUN chmod +x /cost.sh

RUN apk add zstd

ENTRYPOINT ["ash","/cost.sh"]

Use the Docker CLI to build the image

docker build -t <docker hub username>/infracost:1.0 .

Push the image to a repository. The below command pushes to Docker Hub

docker push <docker hub username>/infracost:1.0

Create Infracost Context

You will need to create a configuration context in Environment Manager to store your Infracost API key. The API key will be stored securely and passed to the Infracost container as needed.

In the Rafay console:

Your API key can be obtained under Org Settings in your Infracost account.


Configure Hooks

You will add a hook to each resource in the environment where cost estimation is needed

In the Rafay console:

if #status.container.exitCode == 0 {
                            success: true
                        }
                        if #status.container.exitCode != 0  {
                            failed: true
                        }

Verify Configuration

You can view the cost estimation of a resource by viewing the Infracost activity of the resource in the run of an environment the resource is part of. An illustrative example is shown below.