-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.yaml
118 lines (110 loc) · 4.06 KB
/
solution.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
AWSTemplateFormatVersion: 2010-09-09
Description: This template will Terminate ElasticBeanstalk Environments daily.
Resources:
LambdaRoleForTerminatingElasticBeanstalk:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
RoleName: Stop-LambdaRole-DO-NOT-DELETE-FOR-ELASTICBEANSTALK
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AdministratorAccess-AWSElasticBeanstalk'
Path: /
Policies:
- PolicyName: Stop-EB
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'ec2:DescribeRegions'
- 'ec2:DescribeTags'
- 'sts:GetCallerIdentity'
Resource: '*'
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
LamabdaForTerminatingEB:
Type: 'AWS::Lambda::Function'
Properties:
Description: This Function is used terminate the EB env
Runtime: python3.6
Timeout: 300
FunctionName: ElasticBeanstalk-Stop-Lambda-DO-NOT-DELETE
Handler: index.lambda_handler
Role: !GetAtt LambdaRoleForTerminatingElasticBeanstalk.Arn
Tags:
- Key: auto-delete
Value: 'no'
Code:
ZipFile: |
import boto3
def lambda_handler(event, context):
account_client = boto3.client('sts')
client = boto3.client('ec2')
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
print(regions)
for region in regions:
client = boto3.client('elasticbeanstalk' , region_name= region)
print("region is " +region)
envs = client.describe_environments(
MaxRecords=123
)
envlist=[]
print('terminating these environments-')
try:
for env in envs['Environments']:
if env['Status']!='Terminated' and env['Status']!= 'Terminating':
delete=True
env_arn=env['EnvironmentArn']
tags=client.list_tags_for_resource(ResourceArn=env_arn)['ResourceTags']
for tag in tags:
if( tag['Key'] =='AutoDelete' and tag ['Value'] == 'false'):
delete=False
print(env['EnvironmentName'])
if(delete==True):
print("will delelete this ")
envlist.append(env['EnvironmentName'])
print(envlist)
for env in envlist:
try:
response = client.terminate_environment(
EnvironmentName=env,
TerminateResources=True
)
except Exception as e:
print(e)
except Exception as e:
print(e)
EBEventRule:
Type: 'AWS::Events::Rule'
Properties:
Name: EB-Terminate-DO-NOT-DELETE
ScheduleExpression: cron(30 13 * * ? *)
State: ENABLED
Targets:
- Arn:
'Fn::GetAtt':
- LamabdaForTerminatingEB
- Arn
Id: Test
Permission:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !Ref LamabdaForTerminatingEB
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceArn:
'Fn::GetAtt':
- EBEventRule
- Arn