DEV Community

Cover image for Monitor the CO2 emissions of your AWS application with Cloud Carbon Footprint 🌱
Thomas Aribart for Kumo

Posted on • Updated on

Monitor the CO2 emissions of your AWS application with Cloud Carbon Footprint 🌱

I think I don’t need to remind you that climate change is a big deal 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.

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

At Kumo, we develop Serverless applications (which, by the way, seem to be inherently greener...). 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

So we’ve searched for alternatives, and found Cloud Carbon Footprint 🎉

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

Feel free to test the online demo 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, and contribute to their Github project or Google Group.

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 to run queries on a S3 containing the data of Cost & Usage Reports made by AWS Billing. It’s a mouthful, so let’s draw it:

Cloud Carbon Footprint architecture diagram

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 just for you ❤️

To use it:

  • Download the JSON template from my Github repository
  • Open the Cloudformation service 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

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

Select template

  • Choose a stack name (like cloud-carbon-footprint) and select Next until you reach the “Review” page

Cloudformation stack name

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

Create the stack

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), the paying account should enable Cost & Usage Reports first in the AWS Billing Console

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


🚀 Launch Cloud Carbon Footprint

Once a first Cost & Usage Report is available (it will be visible in the AWS Billing Console > check the “Data last refreshed” column), you are ready to launch Cloud Carbon Footprint!

  • Open the IAM Console 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
  • Configure a new profile and set it as your default user
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>
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
  • 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!

Top comments (0)