pull/310/head
Frostebite 2021-12-30 03:29:25 +00:00
parent 26c15cbb76
commit 08afc9dc26
2 changed files with 20 additions and 20 deletions

View File

@ -18,7 +18,7 @@ Parameters:
Type: String Type: String
Default: development Default: development
Description: 'Your deployment environment: DEV, QA , PROD' Description: 'Your deployment environment: DEV, QA , PROD'
BUILDID: buildGuid:
Type: String Type: String
Default: '' Default: ''
StackName: StackName:
@ -34,16 +34,16 @@ Resources:
DeleteCFNLambda: DeleteCFNLambda:
Type: "AWS::Lambda::Function" Type: "AWS::Lambda::Function"
Properties: Properties:
FunctionName: !Join [ "", [ 'DeleteCFNLambda', !Ref BUILDID ] ] FunctionName: !Join [ "", [ 'DeleteCFNLambda', !Ref buildGuid ] ]
Code: Code:
ZipFile: | ZipFile: |
import boto3 import boto3
import os import os
import json import json
stack_name = os.environ['stackName'] stack_name = os.environ['stackName']
delete_stack_name = os.environ['deleteStackName'] delete_stack_name = os.environ['deleteStackName']
def delete_cfn(stack_name): def delete_cfn(stack_name):
try: try:
cfn = boto3.resource('cloudformation') cfn = boto3.resource('cloudformation')
@ -51,8 +51,8 @@ Resources:
stack.delete() stack.delete()
return "SUCCESS" return "SUCCESS"
except: except:
return "ERROR" return "ERROR"
def handler(event, context): def handler(event, context):
print("Received event:") print("Received event:")
print(json.dumps(event)) print(json.dumps(event))
@ -66,7 +66,7 @@ Resources:
Handler: "index.handler" Handler: "index.handler"
Runtime: "python3.6" Runtime: "python3.6"
Timeout: "5" Timeout: "5"
Role: Role:
'Fn::ImportValue': !Sub '${EnvironmentName}:DeleteCFNLambdaExecutionRole' 'Fn::ImportValue': !Sub '${EnvironmentName}:DeleteCFNLambdaExecutionRole'
DeleteStackEventRule: DeleteStackEventRule:
DependsOn: DependsOn:
@ -74,27 +74,27 @@ Resources:
- GenerateCronExpression - GenerateCronExpression
Type: "AWS::Events::Rule" Type: "AWS::Events::Rule"
Properties: Properties:
Name: !Join [ "", [ 'DeleteStackEventRule', !Ref BUILDID ] ] Name: !Join [ "", [ 'DeleteStackEventRule', !Ref buildGuid ] ]
Description: Delete stack event Description: Delete stack event
ScheduleExpression: !GetAtt GenerateCronExpression.cron_exp ScheduleExpression: !GetAtt GenerateCronExpression.cron_exp
State: "ENABLED" State: "ENABLED"
Targets: Targets:
- -
Arn: !GetAtt DeleteCFNLambda.Arn Arn: !GetAtt DeleteCFNLambda.Arn
Id: 'DeleteCFNLambda' Id: 'DeleteCFNLambda'
PermissionForDeleteCFNLambda: PermissionForDeleteCFNLambda:
Type: "AWS::Lambda::Permission" Type: "AWS::Lambda::Permission"
DependsOn: DependsOn:
- DeleteStackEventRule - DeleteStackEventRule
Properties: Properties:
FunctionName: !Join [ "", [ 'DeleteCFNLambda', !Ref BUILDID ] ] FunctionName: !Join [ "", [ 'DeleteCFNLambda', !Ref buildGuid ] ]
Action: "lambda:InvokeFunction" Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com" Principal: "events.amazonaws.com"
SourceArn: !GetAtt DeleteStackEventRule.Arn SourceArn: !GetAtt DeleteStackEventRule.Arn
GenerateCronExpLambda: GenerateCronExpLambda:
Type: "AWS::Lambda::Function" Type: "AWS::Lambda::Function"
Properties: Properties:
FunctionName: !Join [ "", [ 'GenerateCronExpressionLambda', !Ref BUILDID ] ] FunctionName: !Join [ "", [ 'GenerateCronExpressionLambda', !Ref buildGuid ] ]
Code: Code:
ZipFile: | ZipFile: |
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -102,7 +102,7 @@ Resources:
import logging import logging
import json import json
import cfnresponse import cfnresponse
def deletion_time(ttl): def deletion_time(ttl):
delete_at_time = datetime.now() + timedelta(minutes=int(ttl)) delete_at_time = datetime.now() + timedelta(minutes=int(ttl))
hh = delete_at_time.hour hh = delete_at_time.hour
@ -113,7 +113,7 @@ Resources:
# minutes hours day month day-of-week year # minutes hours day month day-of-week year
cron_exp = "cron({} {} {} {} ? {})".format(mm, hh, dd, month, yyyy) cron_exp = "cron({} {} {} {} ? {})".format(mm, hh, dd, month, yyyy)
return cron_exp return cron_exp
def handler(event, context): def handler(event, context):
print('Received event: %s' % json.dumps(event)) print('Received event: %s' % json.dumps(event))
status = cfnresponse.SUCCESS status = cfnresponse.SUCCESS
@ -132,12 +132,12 @@ Resources:
Handler: "index.handler" Handler: "index.handler"
Runtime: "python3.6" Runtime: "python3.6"
Timeout: "5" Timeout: "5"
Role: Role:
'Fn::ImportValue': !Sub '${EnvironmentName}:DeleteCFNLambdaExecutionRole' 'Fn::ImportValue': !Sub '${EnvironmentName}:DeleteCFNLambdaExecutionRole'
GenerateCronExpression: GenerateCronExpression:
Type: "Custom::GenerateCronExpression" Type: "Custom::GenerateCronExpression"
Version: "1.0" Version: "1.0"
Properties: Properties:
Name: !Join [ "", [ 'GenerateCronExpression', !Ref BUILDID ] ] Name: !Join [ "", [ 'GenerateCronExpression', !Ref buildGuid ] ]
ServiceToken: !GetAtt GenerateCronExpLambda.Arn ServiceToken: !GetAtt GenerateCronExpLambda.Arn
ttl: !Ref 'TTL' ttl: !Ref 'TTL'

View File

@ -30,7 +30,7 @@ Parameters:
Type: Number Type: Number
Default: 2048 Default: 2048
Description: How much memory in megabytes to give the container Description: How much memory in megabytes to give the container
BUILDID: buildGuid:
Type: String Type: String
Default: '' Default: ''
Command: Command: