|
| 1 | +/** |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import * as cdk from 'aws-cdk-lib'; |
| 15 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 16 | +import * as lambda from 'aws-cdk-lib/aws-lambda'; |
| 17 | +import * as sfn from 'aws-cdk-lib/aws-stepfunctions'; |
| 18 | +import { CodeTask } from '@aws-accelerator/cdk-accelerator/src/stepfunction-tasks'; |
| 19 | +import { Construct } from 'constructs'; |
| 20 | + |
| 21 | +export namespace EnableOptinRegionTask { |
| 22 | + export interface Props { |
| 23 | + role: iam.IRole; |
| 24 | + lambdaCode: lambda.Code; |
| 25 | + waitSeconds?: number; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export class EnableOptinRegionTask extends sfn.StateMachineFragment { |
| 30 | + readonly startState: sfn.State; |
| 31 | + readonly endStates: sfn.INextable[]; |
| 32 | + |
| 33 | + constructor(scope: Construct, id: string, props: EnableOptinRegionTask.Props) { |
| 34 | + super(scope, id); |
| 35 | + |
| 36 | + const { role, lambdaCode, waitSeconds = 60 } = props; |
| 37 | + |
| 38 | + role.addToPrincipalPolicy( |
| 39 | + new iam.PolicyStatement({ |
| 40 | + effect: iam.Effect.ALLOW, |
| 41 | + resources: ['*'], |
| 42 | + actions: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'], |
| 43 | + }), |
| 44 | + ); |
| 45 | + role.addToPrincipalPolicy( |
| 46 | + new iam.PolicyStatement({ |
| 47 | + effect: iam.Effect.ALLOW, |
| 48 | + resources: ['*'], |
| 49 | + actions: ['codepipeline:PutJobSuccessResult', 'codepipeline:PutJobFailureResult'], |
| 50 | + }), |
| 51 | + ); |
| 52 | + |
| 53 | + const createTaskResultPath = '$.enableOutput'; |
| 54 | + const createTaskResultLength = `${createTaskResultPath}.outputCount`; |
| 55 | + const createTaskErrorCount = `${createTaskResultPath}.errorCount`; |
| 56 | + |
| 57 | + const enableTask = new CodeTask(scope, `Start Optin Region`, { |
| 58 | + resultPath: createTaskResultPath, |
| 59 | + functionProps: { |
| 60 | + role, |
| 61 | + code: lambdaCode, |
| 62 | + handler: 'index.enableOptinRegions.enable', |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + // Create Map task to iterate |
| 67 | + const mapTask = new sfn.Map(this, `Enable Optin Region Map`, { |
| 68 | + itemsPath: '$.accounts', |
| 69 | + resultPath: sfn.JsonPath.DISCARD, |
| 70 | + maxConcurrency: 15, |
| 71 | + parameters: { |
| 72 | + 'accountId.$': '$$.Map.Item.Value', |
| 73 | + 'assumeRoleName.$': '$.assumeRoleName', |
| 74 | + 'configRepositoryName.$': '$.configRepositoryName', |
| 75 | + 'configFilePath.$': '$.configFilePath', |
| 76 | + 'configCommitId.$': '$.configCommitId', |
| 77 | + 'acceleratorPrefix.$': '$.acceleratorPrefix', |
| 78 | + 'baseline.$': '$.baseline', |
| 79 | + }, |
| 80 | + }); |
| 81 | + mapTask.iterator(enableTask); |
| 82 | + |
| 83 | + const verifyTaskResultPath = '$.verifyOutput'; |
| 84 | + const verifyTask = new CodeTask(scope, 'Verify Optin Region', { |
| 85 | + resultPath: verifyTaskResultPath, |
| 86 | + functionProps: { |
| 87 | + role, |
| 88 | + code: lambdaCode, |
| 89 | + handler: 'index.enableOptinRegions.verify', |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + const waitTask = new sfn.Wait(scope, 'Wait for Optin Region Enabling', { |
| 94 | + time: sfn.WaitTime.duration(cdk.Duration.seconds(waitSeconds)), |
| 95 | + }); |
| 96 | + |
| 97 | + const pass = new sfn.Pass(this, 'Optin Region Enablement Succeeded'); |
| 98 | + |
| 99 | + const fail = new sfn.Fail(this, 'Optin Region Enablement Failed'); |
| 100 | + |
| 101 | + waitTask |
| 102 | + .next(verifyTask) |
| 103 | + .next( |
| 104 | + new sfn.Choice(scope, 'Optin Region Enablement Done?') |
| 105 | + .when(sfn.Condition.stringEquals(verifyTaskResultPath, 'SUCCESS'), pass) |
| 106 | + .when(sfn.Condition.stringEquals(verifyTaskResultPath, 'IN_PROGRESS'), waitTask) |
| 107 | + .otherwise(fail) |
| 108 | + .afterwards(), |
| 109 | + ); |
| 110 | + |
| 111 | + enableTask.next( |
| 112 | + new sfn.Choice(scope, 'Optin Region Enablement Started?') |
| 113 | + .when( |
| 114 | + sfn.Condition.and( |
| 115 | + sfn.Condition.numberEquals(createTaskResultLength, 0), |
| 116 | + sfn.Condition.numberEquals(createTaskErrorCount, 0), |
| 117 | + ), |
| 118 | + pass, |
| 119 | + ) // already enabled or skipped |
| 120 | + .when(sfn.Condition.numberGreaterThan(createTaskResultLength, 0), waitTask) // processing |
| 121 | + .when(sfn.Condition.numberGreaterThan(createTaskErrorCount, 0), fail) |
| 122 | + .otherwise(fail) |
| 123 | + .afterwards(), |
| 124 | + ); |
| 125 | + |
| 126 | + this.startState = mapTask.startState; |
| 127 | + this.endStates = fail.endStates; |
| 128 | + } |
| 129 | +} |
0 commit comments