unity-builder/dist/cloudformation-stack-ttl.yml

137 lines
4.4 KiB
YAML
Raw Normal View History

2021-03-27 22:52:27 +00:00
AWSTemplateFormatVersion: '2010-09-09'
Description: Schedule automatic deletion of CloudFormation stacks
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Input configuration
Parameters:
- StackName
- TTL
ParameterLabels:
StackName:
default: Stack name
TTL:
default: Time-to-live
Parameters:
2021-03-27 23:54:16 +00:00
EnvironmentName:
Type: String
Default: development
Description: 'Your deployment environment: DEV, QA , PROD'
2021-03-27 23:01:06 +00:00
BUILDID:
Type: String
Default: ''
2021-03-27 22:52:27 +00:00
StackName:
Type: String
Description: Stack name that will be deleted.
TTL:
Type: Number
Description: Time-to-live in minutes for the stack.
Resources:
DeleteCFNLambda:
Type: "AWS::Lambda::Function"
Properties:
2021-03-28 20:42:14 +00:00
FunctionName: !Join [ "", [ 'DeleteCFNLambda', !Ref BUILDID ] ]
2021-03-27 22:52:27 +00:00
Code:
ZipFile: |
import boto3
import os
import json
stack_name = os.environ['stackName']
def delete_cfn(stack_name):
try:
cfn = boto3.resource('cloudformation')
stack = cfn.Stack(stack_name)
stack.delete()
return "SUCCESS"
except:
return "ERROR"
def handler(event, context):
print("Received event:")
print(json.dumps(event))
return delete_cfn(stack_name)
Environment:
Variables:
stackName: !Ref 'StackName'
Handler: "index.handler"
Runtime: "python3.6"
Timeout: "5"
2021-03-27 23:49:41 +00:00
Role:
2021-03-27 23:56:28 +00:00
'Fn::ImportValue': !Sub '${EnvironmentName}:DeleteCFNLambdaExecutionRole'
2021-03-27 22:52:27 +00:00
DeleteStackEventRule:
DependsOn:
- DeleteCFNLambda
- GenerateCronExpression
Type: "AWS::Events::Rule"
Properties:
2021-03-28 20:59:40 +00:00
Name: !Join [ "", [ 'DeleteStackEventRule', !Ref BUILDID ] ]
2021-03-27 22:52:27 +00:00
Description: Delete stack event
ScheduleExpression: !GetAtt GenerateCronExpression.cron_exp
State: "ENABLED"
Targets:
-
Arn: !GetAtt DeleteCFNLambda.Arn
Id: 'DeleteCFNLambda'
PermissionForDeleteCFNLambda:
Type: "AWS::Lambda::Permission"
2021-03-28 00:17:05 +00:00
DependsOn:
- DeleteStackEventRule
2021-03-27 22:52:27 +00:00
Properties:
2021-03-28 20:42:14 +00:00
FunctionName: !Join [ "", [ 'DeleteCFNLambda', !Ref BUILDID ] ]
2021-03-27 22:52:27 +00:00
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt DeleteStackEventRule.Arn
GenerateCronExpLambda:
Type: "AWS::Lambda::Function"
Properties:
2021-03-28 20:42:14 +00:00
FunctionName: !Join [ "", [ 'GenerateCronExpressionLambda', !Ref BUILDID ] ]
2021-03-27 22:52:27 +00:00
Code:
ZipFile: |
from datetime import datetime, timedelta
import os
import logging
import json
import cfnresponse
def deletion_time(ttl):
delete_at_time = datetime.now() + timedelta(minutes=int(ttl))
hh = delete_at_time.hour
mm = delete_at_time.minute
yyyy = delete_at_time.year
month = delete_at_time.month
dd = delete_at_time.day
# minutes hours day month day-of-week year
cron_exp = "cron({} {} {} {} ? {})".format(mm, hh, dd, month, yyyy)
return cron_exp
def handler(event, context):
print('Received event: %s' % json.dumps(event))
status = cfnresponse.SUCCESS
try:
if event['RequestType'] == 'Delete':
cfnresponse.send(event, context, status, {})
else:
ttl = event['ResourceProperties']['ttl']
responseData = {}
responseData['cron_exp'] = deletion_time(ttl)
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
except Exception as e:
logging.error('Exception: %s' % e, exc_info=True)
status = cfnresponse.FAILED
cfnresponse.send(event, context, status, {}, None)
Handler: "index.handler"
Runtime: "python3.6"
Timeout: "5"
2021-03-27 23:50:39 +00:00
Role:
2021-03-28 00:12:47 +00:00
'Fn::ImportValue': !Sub '${EnvironmentName}:DeleteCFNLambdaExecutionRole'
2021-03-27 22:52:27 +00:00
GenerateCronExpression:
Type: "Custom::GenerateCronExpression"
Version: "1.0"
Properties:
2021-03-28 20:42:14 +00:00
Name: !Join [ "", [ 'GenerateCronExpression', !Ref BUILDID ] ]
2021-03-27 22:52:27 +00:00
ServiceToken: !GetAtt GenerateCronExpLambda.Arn
ttl: !Ref 'TTL'