Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. AWSTemplateFormatVersion: 2010-09-09
  2. Description: Creates Lambda function framework for managing states of preprod Fargate services (tasks).
  3. Parameters:
  4. StartupCron:
  5. Type: String
  6. Default: 0 10 ? * MON-FRI *
  7. ShutdownCron:
  8. Type: String
  9. Default: 0 22 ? * MON-FRI *
  10. Metadata:
  11. AWS::CloudFormation::Interface:
  12. ParameterGroups:
  13. -
  14. Label:
  15. default: ""
  16. Parameters:
  17. - StartupCron
  18. - ShutdownCron
  19. ParameterLabels:
  20. StartupCron:
  21. default: Time of day, in CRON format, in which Fargate services should start their tasks. For example, the CRON string "0 10 ? MON-FRI *" indicates every weekday at 10:00 AM GMT i.e. 06:00 AM EDT.
  22. ShutdownCron:
  23. default: Time of day, in CRON format, in which Fargate services should shutdown their tasks. For example, the CRON string "0 22 ? MON-FRI *" indicates every weekday at 10:00 PM GMT i.e. 06:00 PM EDT.
  24. Resources:
  25. ManagePreprodFargatePolicy:
  26. Type: AWS::IAM::ManagedPolicy
  27. Properties:
  28. ManagedPolicyName: manage-preprod-fargate-policy
  29. Description: Allow Lambda function to call CloudWatch logging, ECS all, and SNS write service actions.
  30. PolicyDocument:
  31. Version: "2012-10-17"
  32. Statement:
  33. - Effect: Allow
  34. Action:
  35. - logs:CreateLogGroup
  36. - logs:CreateLogStream
  37. - logs:PutLogEvents
  38. Resource: "arn:aws:logs:*:*:*"
  39. - Effect: Allow
  40. Action:
  41. - ecs:updateService
  42. Resource: "*"
  43. - Effect: Allow
  44. Action:
  45. - sns:Publish
  46. Resource: "*"
  47. ManagePreprodFargateRole:
  48. Type: AWS::IAM::Role
  49. DependsOn: ManagePreprodFargatePolicy
  50. Properties:
  51. RoleName: manage-preprod-fargate-role
  52. # Description: Allow Lambda function to call CloudWatch logging, ECS all, and SNS write service actions.
  53. ManagedPolicyArns:
  54. - Ref: ManagePreprodFargatePolicy
  55. AssumeRolePolicyDocument:
  56. Version: '2012-10-17'
  57. Statement:
  58. - Effect: Allow
  59. Principal:
  60. Service:
  61. - lambda.amazonaws.com
  62. Action:
  63. - sts:AssumeRole
  64. Path: "/"
  65. ManagePreprodFargateProfile:
  66. Type: AWS::IAM::InstanceProfile
  67. Properties:
  68. InstanceProfileName: manage-preprod-fargate-role
  69. Path: "/"
  70. Roles:
  71. - Ref: ManagePreprodFargateRole
  72. CloudwatchScheduleStartup:
  73. Type: AWS::Events::Rule
  74. DependsOn: ManagePreprodFargateFunction
  75. Properties:
  76. Description: Trigger to Lambda function to startup ECS tasks in the morning (EDT).
  77. ScheduleExpression: !Sub 'cron(${StartupCron})'
  78. Name: manage-preprod-fargate-startup-event
  79. State: 'ENABLED'
  80. Targets:
  81. - Arn: !GetAtt ManagePreprodFargateFunction.Arn
  82. Id: manage-preprod-fargate
  83. Input: '{"action":"startup"}'
  84. PermissionForStartupEventToInvokeLambda:
  85. Type: AWS::Lambda::Permission
  86. Properties:
  87. FunctionName: !GetAtt ManagePreprodFargateFunction.Arn
  88. Action: lambda:InvokeFunction
  89. Principal: events.amazonaws.com
  90. SourceArn: !GetAtt CloudwatchScheduleStartup.Arn
  91. CloudwatchScheduleShutdown:
  92. Type: AWS::Events::Rule
  93. DependsOn: ManagePreprodFargateFunction
  94. Properties:
  95. Description: Trigger to Lambda function to shutdown ECS tasks in the evening (EDT).
  96. ScheduleExpression: !Sub 'cron(${ShutdownCron})'
  97. Name: manage-preprod-fargate-shutdown-event
  98. State: 'ENABLED'
  99. Targets:
  100. - Arn: !GetAtt ManagePreprodFargateFunction.Arn
  101. Id: manage-preprod-fargate
  102. Input: '{"action":"shutdown"}'
  103. PermissionForShutdownEventToInvokeLambda:
  104. Type: AWS::Lambda::Permission
  105. Properties:
  106. FunctionName: !GetAtt ManagePreprodFargateFunction.Arn
  107. Action: lambda:InvokeFunction
  108. Principal: events.amazonaws.com
  109. SourceArn: !GetAtt CloudwatchScheduleShutdown.Arn
  110. ManagePreprodFargateFunction:
  111. Type: AWS::Lambda::Function
  112. DependsOn: ManagePreprodFargateRole
  113. Properties:
  114. Description: Function for shutting down Fargate pre-production tasks during non-business hours.
  115. Code:
  116. ZipFile: >
  117. exports.handler = function(event) {
  118. // Paste the *real* function from Git
  119. };
  120. FunctionName: manage-preprod-fargate
  121. Handler: 'index.handler'
  122. Role: !GetAtt ManagePreprodFargateRole.Arn
  123. Runtime: 'nodejs8.10'
  124. Timeout: 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement