AWS DevOps & Developer Productivity Blog
Tracking the Cost of Your AWS CloudFormation Stack
With cost allocation tagging and the AWS Cost Explorer, you can see the cost of operating each of your AWS CloudFormation stacks.
Here’s how it works. AWS CloudFormation automatically tags each stack resource. For example, if you have a stack that creates an Amazon EC2 instance, AWS CloudFormation automatically tags the instance with the following key-value pairs:
- aws:cloudformation:stack-name
The name of the stack, such as myTestStack.
- aws:cloudformation:stack-id
The full stack ID, such as arn:aws:cloudformation:us-east-1:123456789012:stack/myTestStack/2ac98f30-5bdd-11e4-949b-50fa5262a838.
- aws:cloudformation:logical-id
The logical ID of a resource that is defined in the stack template, such as myInstance.
To obtain the costs by stack, all you do is set up a billing report to include the AWS CloudFormation tags. Then you can filter your report in the AWS Cost Explorer to see the costs of items tagged with a specific stack name, stack ID, or logical ID. With Cost Explorer, you can see the costs associated with one or more stacks or view how much of a stack’s cost is from a particular service, such as Amazon EC2 or Amazon RDS.
Note: Before you can use the AWS billing tools, you need the permissions that are described in the Billing and Cost Management Permissions Reference.
Configuring billing reports
- Go to billing preferences.
- Select Receive Billing Reports, and specify an existing Amazon S3 bucket to store your billing reports.
- Click Verify to ensure that your bucket exists and has the required permissions.
You can use the AWS sample bucket policy to set the appropriate permissions. Copy and paste the sample policy into your bucket’s policy. - Select the Detailed billing report with resources and tags, and then click Save preferences.
With this report, you can view a detailed bill with the report tags that you have included. Later, you will add AWS CloudFormation tags so that you can view costs for each AWS CloudFormation stack.
Note: The current month’s data will be available for viewing in about 24 hours.
Configuring cost allocation tags
- Under the Report section, click Manage report tags.
- Select the AWS CloudFormation tags and then click Save.
Your billing report will now include three additional columns for each AWS CloudFormation tag. For example, if you created a stack named myTestStack, all resources in that stack will have the value myTestStack for the aws:cloudformation:stack-name column.
Analyzing costs in Cost Explorer
- From your billing dashboard, click Cost Explorer and then click Launch Cost Explorer.
Note: If you just enabled reporting, data will be available for viewing in about 24 hours.
- Select the Tags filter to view billing information about a particular stack or resource.
- Select an AWS CloudFormation tag key to refine the filter.
- Select the aws:cloudformation:stack-id or stack-name tag to view information about a particular stack.
- Select the aws:cloudformation:logical-id tag to view information about a specific resource
- Select one or more values for the tag key that you selected, and then click Apply.
Cost Explorer displays billing information for your selected stacks or resources. For example, the following graph shows Amazon EC2 and Amazon RDS costs for a particular stack.
With these few simple steps, you can start analyzing the costs of your stacks and resources. Also, to help you estimate costs before you create a stack, you can use the AWS Simple Monthly Calculator. When you use the AWS CloudFormation console to create a stack, the create stack wizard provides a link to the calculator.