UNPKG

@kumologica/builder

Version:
135 lines (124 loc) 3.31 kB
function trigger(params) { let templateResources = {}; const name = "Event" + (params.name || process.hrtime.bigint()); // only default event bus supports cron, no need to pass bus name /*templateResources[name] = { Type : "AWS::Events::Rule", Properties : { ScheduleExpression: params.expression, Name: name, Targets : [ { Id: kumohubListenerId, Arn : { "Fn::GetAtt": ["Lambda", "Arn"] }, Input : `{"type": "kumohub-event", "id": "${kumohubListenerId}"}` } ] } }*/ let input; if (params.reference) { input = `{"source": "kumologica", "detail": {"type": "kumologica-event", "reference": "${params.reference}"}}`; }; templateResources.CloudWatchEventRole = { Type: "AWS::IAM::Role", DependsOn: [ "TaskExecutionRole", "Task" ], Properties: { AssumeRolePolicyDocument: { Version: "2012-10-17", Statement: [ { Effect: "Allow", Principal: { Service: "events.amazonaws.com" }, Action: "sts:AssumeRole" } ] }, Policies: [ { PolicyName: "RunECSTask", PolicyDocument: { Version: "2012-10-17", Statement: [ { Effect: "Allow", Action: [ "ecs:RunTask" ], Resource: { "Ref": "Task" } }, { Effect: "Allow", Action: [ "iam:PassRole" ], Resource: [ { "Fn::GetAtt": [ "TaskExecutionRole", "Arn" ] }, { "Fn::GetAtt": [ "DockerRole", "Arn" ] } ] } ] } } ] } }; templateResources.CloudWatchRule = { Type : "AWS::Events::Rule", DependsOn: [ "CloudWatchEventRole", "TaskDefinition" ], Properties : { ScheduleExpression: params.expression, Name: name, State: params.state, Targets : [ { Id: name, Arn : { "Ref": params.ecsClusterArn }, Input: input } ], RoleArn: { "Fn::GetAtt": [ "CloudWatchEventRole", "Arn" ] }, EcsParameters: { TaskDefinitionArn: { Ref: "Task" }, LaunchType: "FARGATE", NetworkConfiguration: { AwsVpcConfiguration: { Subnets: params.vpcConfig.SubnetIds, SecurityGroups: params.vpcConfig.SecurityGroupIds, AssignPublicIp: "DISABLED" } } } } } if (params.deadLetterConfig) { templateResources.CloudWatchRule.DeadLetterConfig = { Arn: params.deadLetterConfig.TargetArn } } if (params.reference) { templateResources.CloudWatchRule.Properties.EventPattern = { "detail": { "type": ["kumologica-event"], "reference": [params.reference], "args": params.args } } } return templateResources; } module.exports = { trigger }