In this tutorial, we will develop a self destructing cloudformation stack. The stack can ben named harakiri since it will delete not only the resources created but also itself after a specified duration. The stack is best suited for timely
For example, a potential customer wants to try your SaaS before paid subscription and you do not want to be charged for the unused resources.
It would be better have a basic knowledge about CloudFormation.
AWS Cloud Formation is a infrastructure as a code that creates, updates and deletes resources with JSON or YAML configuration files.
Defining Stack
AWS Cloud Formation template has five sections:
- Mapping
- Parameters
- Conditions
- Resources
- Outputs
In the resources section we define the AWS Resources that will be created. I will keep it simple and focus on only the resources section.
Trivial Section
|
|
There is nothing fancy here. Although we are not going to create and EC2 instance, at least one region (the region you will be creating the stack) is required. Also AMI (Amazon Machine Image) can be wrong. Just skip it.
IAM Role for The Lambda
We use lambda for triggering the stack delete process. This IAM role must be priviled to delete all resources created with stack. You would like give full privilege this role but be safe by giving fine grained privilege by giving privilege to this role for only the resources created with stack.
We gave privilege to this role:
- Deleting all cloud formation stacks
- Deleting all IAM roles. (This role will able to delete itself)
- Deleting all lambda functions
- Deleting all cloudwatch events and rules
|
|
Lambda Function
This lambda function will start stack delete process.
- How does this lambda function trigger?
- Cloud Watch Rules.
We will define a cloud watch timer later. The cloud watch timer will trigger every X times but since the lambda function will delete all resources created with stack, probably there would be no second execution.
Boto is a Python package that provides interfaces to Amazon Web Services.
|
|
Source code of lambda function is simple. It requires a simple JSON object with a StackName
property which is the name of the
Cloud Formation stack.
|
|
CloudWatch Timer (Rule)
We will define a cloud watch cron rule that will be executed every thirty minutes but we expect it does not executed twice about five or ten minutes after the first execution, all resources including this rule will be deleted.
We prepare a simple JSON object at line 107, for the lambda by substituting the stack name.
|
|
Final
- Go to AWS Cloud Formation Console: console.aws.amazon.com/cloudformation
- Download the full template from template.yml and change the 30 minutes section to 2-3 minutes.
- Click “Create Stack” button and upload the template.
- Hit the “Next” button and give a name to your stack.
- Watch the events tabs at the bottom.
You will see the stack create process, just after 2-3 minutes, “stack delete in progress” event will appear.
From the CLI you can create the stack by typing:
aws cloudformation deploy \
--stack-name myteststack \
--capabilities CAPABILITY_IAM \
--template-file template.yml
Conclusion
I hope you enjoyed and learnt a lot.
Happy coding.