Monitor the CO2 emissions of your AWS application with Cloud Carbon Footprint 🌱
Tags: aws, greenit, cloud, sustainability
Published: 2022-04-06 08:48:26

I think I don’t need to remind you that [climate change is a big deal](https://www.theguardian.com/environment/ng-interactive/2021/oct/14/climate-change-happening-now-stats-graphs-maps-cop26) and that humanity must urgently reduce its CO2 emissions.

We, web developers, must do our part: It has been estimated [data centers produce 2% of the global greenhouse gases emissions](https://www.climateneutralgroup.com/en/news/carbon-emissions-of-data-centers/).

To help us take data-driven actions, cloud providers have started building monitoring dashboards of our cloud usage carbon emissions:

- Microsoft released the [Emissions Impact Dashboard](https://www.microsoft.com/en-us/sustainability/emissions-impact-dashboard) in 2020
- Google released the [Carbon Footprint service](https://cloud.google.com/carbon-footprint) on GCP in 2021
- And AWS just released their [Customer Carbon Footprint Tool](https://aws.amazon.com/aws-cost-management/aws-customer-carbon-footprint-tool/) last month (feb. 2022)

At Kumo, we develop Serverless applications (which, by the way, seem to be [inherently greener](https://www.linkedin.com/pulse/quantifying-greenness-faas-lukasz-mastalerz/)...). So this article will focus on AWS, which has always been ahead regarding Serverless.

The sad thing is that AWS have been quite late regarding CO2 emissions monitoring: Their Customer Carbon Footprint Tool has just been just released (2 years after Azure), and, for now, suffers from strong limitations:

- Results have high granularity (100 kgCO2 equivalent versus 1 for GCP’s Carbon Footprint)
- Only the S3 and EC2 services are available in the emissions breakdown by service : In the case of a Serverless application, most of the emissions will fall... in the “Other” category

![AWS Customer Carbon Footprint Tool](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5x97f6ak4t2kgrcus3z.png)

So we’ve searched for alternatives, and found [Cloud Carbon Footprint](https://www.cloudcarbonfootprint.org/) 🎉

Cloud Carbon Footprint is an open-source application that enables projects to monitor their cloud usage CO2 emissions on GCP, Azure... and AWS! It also offer nice features like breakdowns by region, account or AWS service:

![Cloud Carbon Footprint dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nod6m2b1nw0fi8vwlzw0.png)

Feel free to test the [online demo](https://demo.cloudcarbonfootprint.org/) for more details!

The emissions are derived from billing. The billing-to-CO2 coefficients are debated by the Cloud Carbon Footprint community (and overridable if needed). You can find more infos on the project [methodology page](https://www.cloudcarbonfootprint.org/docs/methodology/), and contribute to their [Github project](https://github.com/cloud-carbon-footprint/cloud-carbon-footprint) or [Google Group](https://groups.google.com/g/cloud-carbon-footprint).

This article explains how set up Cloud Carbon Footprint on your project **in only a few clicks**, so you can start taking actions now! Let’s dive in 🙌

---

## 📖 Create a Cost & Usage Reports

Before setting Cloud Carbon Footprint up, let’s take a quick look at how it works.

Technically, Cloud Carbon Footprint uses [Athena](https://aws.amazon.com/athena) to run queries on a S3 containing the data of [Cost & Usage Reports](https://docs.aws.amazon.com/cur/latest/userguide/what-is-cur.html) made by AWS Billing. It’s a mouthful, so let’s draw it:

![Cloud Carbon Footprint architecture diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6e0jbiptxcgh7u8rnmp.jpg)

One consequence is that you cannot install Cloud Carbon Footprint and have it work out-of-the-box: You’ll have to do some setup in your AWS account first.

Setting up all those resources by hand can be quite long... Thankfully, **you won’t have to do it**, because we made a [CloudFormation template](https://github.com/ThomasAribart/cloud-carbon-footprint-installation-template) just for you ❤️

To use it:

- Download the [JSON template](https://github.com/ThomasAribart/cloud-carbon-footprint-installation-template/blob/main/cloud-carbon-footprint-installation-template.json) from my Github repository
- Open the [Cloudformation service](https://us-east-1.console.aws.amazon.com/cloudformation/home) in the ***us-east-1*** region (we’ll see why later)
- Select ***Create stack*** > ***With new resources*** and ***Template is ready***

![Create a Cloud Formation stack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkmwwn1e02a7rwv79hu0.png)

- Select ***Upload a template file***, upload the JSON template and select ***Next***

![Select template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ald9rhr0uxlbww44ukk.png) 
    
- Choose a stack name (like *cloud-carbon-footprint*) and select ***Next*** until you reach the “Review” page

![Cloudformation stack name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzhov6q7qdata52hb7rp.png)

- In the “Review” page, accept the check boxes and select ***Create stack***

![Create the stack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf52emtksq7ue912yewd.png) 

And voilà! For the curious, this template will deploy:

- 🪣 A S3 Bucket and a S3 Bucket Policy to store the Cost & Usage Report (*”BillingDataBucket”*)
- 📖 The Cost & Usage Report definition (*”CurDefinition”*)
- 🏊‍♀️ The necessary Glue, Lambda and IAM resources to allow Athena to crawl the Cost & Usage Report
- 🪣 A S3 bucket to store Athena query results (*”AthenaQueryResultsBucket”*)
- 🔑 The necessary IAM resources to allow Cloud Carbon Footprint to perform said queries (*”CloudCarbonFootprintUserPolicy”* and *“CloudCarbonFootprintUser”*)

Two important notes:

- It is important to base this stack in the ***us-east-1*** region as the ***AWS::CUR::ReportDefinition*** resource type is only available there (don’t worry, reports are still cross-regions)
- If your account is not the paying account (like a linked account in [AWS Organization](https://console.aws.amazon.com/organizations/)), the paying account should enable Cost & Usage Reports first in the [AWS Billing Console](https://us-east-1.console.aws.amazon.com/billing/home?#/reports)

Once the required resources are deployed, simply wait: It can take up to 24h for AWS Billing to create the first Cost & Usage Report.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/tXL4FHPSnVJ0A/giphy.gif">

---

## 🚀 Launch Cloud Carbon Footprint

Once a first Cost & Usage Report is available (it will be visible in the [AWS Billing Console](https://us-east-1.console.aws.amazon.com/billing/home?#/reports) > check the “*Data last refreshed”* column), you are ready to launch Cloud Carbon Footprint!

- Open the [IAM Console](https://console.aws.amazon.com/iam/home#/users/cloud-carbon-footprint?section=security_credentials) and select ***Create Access Key*** on the *cloud-carbon-footprint* user: Write down the **Access key ID** and the **Secret access key** (you will only see the latter once)
- If you haven’t done it, install the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
- Configure a new profile and set it as your default user

```bash
aws configure --profile <PROFILE_NAME>
# AWS Access Key ID: <ACCESS_KEY_ID>
# AWS Secret Access Key: <SECRET_ACCESS_KEY>
# Default region name: Your default region (ex: eu-west-1)
# Default output format: Your default output format (ex: json)
export AWS_PROFILE=<PROFILE_NAME>
```

- Install the [cloud-carbon-footprint](https://github.com/cloud-carbon-footprint/cloud-carbon-footprint) package via [npx](https://www.npmjs.com/package/npx)

```bash
npx @cloud-carbon-footprint/create-app
# Submit your PROJECT_NAME in the wizard
cd <PROJECT_NAME>
yarn install
yarn guided-install
# Cloud provider: AWS
# AWS account id: <YOUR_PROJECT_ACCOUNT_ID> (e.g. 628019955336)
# AWS account name: <YOUR_PROJECT_NAME> (e.g. "[PRODUCTION] Master Account")
# AWS region: us-east-1 (should be the region of the CUR bucket)
# Ensure your AWS account has the correct permission: Yes
# AWS role name: <ANY_VALUE> (doesn't work)
# Enable the Cost and Usage Reports Billing AWS feature: Yes
# Setup Athena DB to save the Cost and Usage Reports: Yes
# Athena DB name: costandusagereportdatabase
# Athena DB table: costandusagereport
# Athena DB query result location: s3://<STACK_NAME>-athenaqueryresultsbucket-<SOME_HASH>
# (can be found in the s3 service)
# Install the AWS CLI: Yes
# Configure aws credentials locally: No
# Run `aws configure`: Yes
yarn start
```

- And voilà! You should have your dashboard ready to go 🚀

The data will go back up to the oldest data available in the Cost and Usage Report, which should be the beginning of its creation month, and will be refreshed at least daily. It is important not to remove the Report as long as you want to monitor your emissions.

Now you can start taking actions in favor of climate 💪

Cheers!