25
25
ErrInvalidBackedURLForTimeout = fmt .Errorf ("timeout can action can not be set due to backed url pointing to local file. Please use external storage or remote timeout option" )
26
26
)
27
27
28
- func CreateDestroyOperation (ctx * pulumi.Context ,
28
+ func OneTimeDelayedTask (ctx * pulumi.Context ,
29
29
region , prefix , componentID string ,
30
- target string ,
30
+ cmd string ,
31
31
delay string ) error {
32
32
if err := checkBackedURLForServerless (); err != nil {
33
33
return err
@@ -37,11 +37,9 @@ func CreateDestroyOperation(ctx *pulumi.Context,
37
37
return err
38
38
}
39
39
r := & serverlessRequestArgs {
40
- region : region ,
41
- command : fmt .Sprintf ("aws %s destroy --project-name %s --backed-url %s --serverless" ,
42
- target ,
43
- maptContext .ProjectName (),
44
- maptContext .BackedURL ()),
40
+ region : region ,
41
+ command : cmd ,
42
+ scheduleType : OneTime ,
45
43
scheduleExpression : se ,
46
44
prefix : prefix ,
47
45
componentID : componentID ,
@@ -110,7 +108,7 @@ func (a *serverlessRequestArgs) deploy(ctx *pulumi.Context) error {
110
108
if err != nil {
111
109
return err
112
110
}
113
- sRole , err := createSchedulerRole (ctx ,
111
+ sRoleArn , err := getSchedulerRole (ctx ,
114
112
a .prefix ,
115
113
a .componentID )
116
114
if err != nil {
@@ -143,7 +141,7 @@ func (a *serverlessRequestArgs) deploy(ctx *pulumi.Context) error {
143
141
},
144
142
},
145
143
Arn : clusterArn ,
146
- RoleArn : sRole . Arn ,
144
+ RoleArn : sRoleArn ,
147
145
},
148
146
ScheduleExpression : pulumi .String (* se ),
149
147
ScheduleExpressionTimezone : pulumi .String (data .RegionTimezones [a .region ]),
@@ -260,8 +258,24 @@ func createTaskRole(ctx *pulumi.Context, roleName, prefix, componentID string) (
260
258
return r , nil
261
259
}
262
260
261
+ // As part of the runtime for serverless invocation we need a fixed role for task execution the region as so if
262
+ // it exists it will pick the role otherwise it will create and will not be deleted
263
+ func getSchedulerRole (ctx * pulumi.Context , prefix , componentID string ) (* pulumi.StringOutput , error ) {
264
+ roleName := fmt .Sprintf ("%s-%s" , maptServerlessDefaultPrefix , "sch-role" )
265
+ roleArn , err := data .GetRole (roleName )
266
+ if err != nil {
267
+ if role , err := createSchedulerRole (ctx , roleName , prefix , componentID ); err != nil {
268
+ return nil , err
269
+ } else {
270
+ return & role .Arn , nil
271
+ }
272
+ }
273
+ rarn := pulumi .String (* roleArn ).ToStringOutput ()
274
+ return & rarn , nil
275
+ }
276
+
263
277
// https://docs.aws.amazon.com/scheduler/latest/UserGuide/setting-up.html#setting-up-execution-role
264
- func createSchedulerRole (ctx * pulumi.Context , prefix , componentID string ) (* iam.Role , error ) {
278
+ func createSchedulerRole (ctx * pulumi.Context , roleName , prefix , componentID string ) (* iam.Role , error ) {
265
279
trustPolicyContent , err := json .Marshal (map [string ]interface {}{
266
280
"Version" : "2012-10-17" ,
267
281
"Statement" : []map [string ]interface {}{
@@ -279,12 +293,13 @@ func createSchedulerRole(ctx *pulumi.Context, prefix, componentID string) (*iam.
279
293
}
280
294
// Need to creeate policies and attach
281
295
r , err := iam .NewRole (ctx ,
282
- resourcesUtil .GetResourceName (prefix , componentID , "role-sche " ),
296
+ resourcesUtil .GetResourceName (prefix , componentID , "sch-role " ),
283
297
& iam.RoleArgs {
284
- Name : pulumi .String (fmt . Sprintf ( "mapt-sche-%s" , maptContext . RunID ()) ),
298
+ Name : pulumi .String (roleName ),
285
299
AssumeRolePolicy : pulumi .String (string (trustPolicyContent )),
286
300
Tags : maptContext .ResourceTags (),
287
- })
301
+ },
302
+ pulumi .RetainOnDelete (true ))
288
303
if err != nil {
289
304
return nil , err
290
305
}
@@ -336,8 +351,8 @@ func generateOneTimeScheduleExpression(region, delay string) (string, error) {
336
351
}
337
352
// Add the duration to the current time
338
353
futureTime := currentTime .Add (duration )
339
- se := scheduleExpressionByType ( OneTime , futureTime .Format ("2006-01-02T15:04:05" ) )
340
- return * se , nil
354
+ se := futureTime .Format ("2006-01-02T15:04:05" )
355
+ return se , nil
341
356
}
342
357
343
358
func scheduleExpressionByType (st scheduleType , se string ) * string {
0 commit comments