-
Notifications
You must be signed in to change notification settings - Fork 14
/
definitions.py
196 lines (180 loc) · 5.96 KB
/
definitions.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import logging
from .base import ComponentFile
logger = logging.getLogger(__name__)
lambda_base = '''description: {description}
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:ListBucket"
- "s3:GetObject"
- "s3:GetBucketLocation"
Resource: "*"
- Effect: "Allow"
Action:
- "s3:PutObject"
Resource:
- !Join
- ''
- - 'arn:aws:s3:::'
- ${{self:provider.environment.CIRRUS_DATA_BUCKET}}
- '*'
- Effect: "Allow"
Action: secretsmanager:GetSecretValue
Resource:
- arn:aws:secretsmanager:#{{AWS::Region}}:#{{AWS::AccountId}}:secret:cirrus*
environment: {{}}
'''.format
lambda_lambda = '''lambda:
memorySize: 128
timeout: 60
handler: lambda_function.lambda_handler
pythonRequirements:
include: []
'''.format
lambda_batch = '''batch:
resources:
Resources:
{name}ComputeEnvironment:
Type: AWS::Batch::ComputeEnvironment
Properties:
Type: MANAGED
ComputeResources:
MaxvCpus: 1000
SecurityGroupIds: ${{self:custom.batch.SecurityGroupIds}}
Subnets: ${{self:custom.batch.Subnets}}
InstanceTypes:
- optimal
Type: SPOT
AllocationStrategy: BEST_FIT_PROGRESSIVE
SpotIamFleetRole:
Fn::GetAtt: [ EC2SpotRole, Arn ]
MinvCpus: 0
InstanceRole:
Fn::GetAtt: [ BatchInstanceProfile, Arn ]
Tags: {{"Name": "Batch Instance - #{{AWS::StackName}}"}}
DesiredvCpus: 0
State: ENABLED
{name}JobQueue:
Type: AWS::Batch::JobQueue
Properties:
ComputeEnvironmentOrder:
- Order: 1
ComputeEnvironment: !Ref {name}ComputeEnvironment
State: ENABLED
Priority: 1
{name}JobRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Service:
- 'ecs-tasks.amazonaws.com'
Action:
- 'sts:AssumeRole'
Condition:
ArnLike:
aws:SourceArn: "arn:aws:ecs:#{{AWS::Region}}:#{{AWS::AccountId}}:*"
StringEquals:
aws:SourceAccount: "#{{AWS::AccountId}}"
Path: '/'
Policies:
- PolicyName: 'Cirrus'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: "Allow"
Action:
- "s3:PutObject"
Resource:
- !Join
- ''
- - 'arn:aws:s3:::'
- ${{self:provider.environment.CIRRUS_DATA_BUCKET}}
- '*'
- !Join
- ''
- - 'arn:aws:s3:::'
- ${{self:provider.environment.CIRRUS_PAYLOAD_BUCKET}}
- '*'
- Effect: "Allow"
Action:
- "s3:ListBucket"
- "s3:GetObject"
- "s3:GetBucketLocation"
Resource: "*"
- Effect: "Allow"
Action: secretsmanager:GetSecretValue
Resource:
- arn:aws:secretsmanager:#{{AWS::Region}}:#{{AWS::AccountId}}:secret:cirrus*
- Effect: "Allow"
Action:
- lambda:GetFunction
Resource:
- arn:aws:lambda:#{{AWS::Region}}:#{{AWS::AccountId}}:function:#{{AWS::StackName}}-*
{name}AsBatchJob:
Type: "AWS::Batch::JobDefinition"
Properties:
JobDefinitionName: '#{{AWS::StackName}}-{name}'
Type: Container
Parameters:
url: ""
ContainerProperties:
Command:
- run
- ${{self:service}}-${{self:provider.stage}}-{name}
- Ref::url
Environment: []
Memory: 128
Vcpus: 1
Image: 'cirrusgeo/run-lambda:0.2.1'
JobRoleArn: !Fn::GetAtt {name}JobRole.Arn
RetryStrategy:
Attempts: 1
'''.format
default_workflow = '''name: ${{self:service}}-${{self:provider.stage}}-{name}
definition:
Comment: {description}
StartAt: publish
States:
publish:
Type: Task
Resource:
Fn::GetAtt: [publish, Arn]
End: True
Retry:
- ErrorEquals: ["Lambda.TooManyRequestsException", "Lambda.Unknown"]
IntervalSeconds: 1
BackoffRate: 2.0
MaxAttempts: 5
Catch:
- ErrorEquals: ["States.ALL"]
ResultPath: $.error
Next: failure
failure:
Type: Fail
'''.format
class BaseDefinition(ComponentFile):
def __init__(self, *args, name='definition.yml', **kwargs):
super().__init__(*args, name=name, **kwargs)
@staticmethod
def content_fn(component) -> str:
return ''
class LambdaDefinition(BaseDefinition):
@staticmethod
def content_fn(component) -> str:
content = lambda_base(description=component.description)
if getattr(component, 'lambda_enabled', True):
content += '\n' + lambda_lambda()
if getattr(component, 'batch_enabled', False):
content += '\n' + lambda_batch(name=component.name)
return content
class StepFunctionDefinition(BaseDefinition):
@staticmethod
def content_fn(component) -> str:
return default_workflow(
name=component.name,
description=component.description,
)