From 8ce02838ba5033a6b35123a34e444c4f35350975 Mon Sep 17 00:00:00 2001 From: Tatsuya Mori Date: Fri, 8 Dec 2023 07:00:32 +0900 Subject: [PATCH] fix(stepfunctions-tasks): state machine role is missing sagemaker:AddTags permission for SageMakerCreateTransformJob task (#27264) If we specified RUN_JOB as IntegrationPattern prop for SageMakerCreateTransformJob construct, StepFunctions executes SageMaker batch transform job [synchronously](https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html#connect-sync). In this case, StepFunctions add a tag (key: MANAGED_BY_AWS, value: STARTED_BY_STEP_FUNCTIONS) to the job, so state machine role needs the permission to do that. However, currently CDK does not add the permission automatically. This PR solves the issue by adding `sagemaker:AddTags` permission to state machine role when RUN_JOB is specified as IntegrationPattern prop. Closes #26012 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- ...efaultTestDeployAssert659366A6.assets.json | 19 + ...aultTestDeployAssert659366A6.template.json | 36 ++ ...ask-create-transform-job-integ.assets.json | 19 + ...k-create-transform-job-integ.template.json | 208 ++++++++++ .../cdk.out | 1 + .../integ.json | 12 + .../manifest.json | 131 +++++++ .../tree.json | 363 ++++++++++++++++++ .../sagemaker/integ.create-transform-job.ts | 45 +++ .../lib/sagemaker/create-transform-job.ts | 10 + .../sagemaker/create-transform-job.test.ts | 214 +++++++++++ 11 files changed, 1058 insertions(+) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.assets.json new file mode 100644 index 0000000000000..8ada2490da17a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.assets.json @@ -0,0 +1,19 @@ +{ + "version": "35.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "SqsTestDefaultTestDeployAssert659366A6.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/SqsTestDefaultTestDeployAssert659366A6.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.assets.json new file mode 100644 index 0000000000000..ba72b23ddf8b4 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.assets.json @@ -0,0 +1,19 @@ +{ + "version": "35.0.0", + "files": { + "17277a722928df6f2a0ba8c8d5225ee786b3be9598d636e6411609b626e5a099": { + "source": { + "path": "aws-cdk-step-functions-task-create-transform-job-integ.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "17277a722928df6f2a0ba8c8d5225ee786b3be9598d636e6411609b626e5a099.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.template.json new file mode 100644 index 0000000000000..c8c96b7ac24f6 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/aws-cdk-step-functions-task-create-transform-job-integ.template.json @@ -0,0 +1,208 @@ +{ + "Resources": { + "BatchInferenceTaskSagemakerTransformRoleEABCCE1A": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "sagemaker.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/AmazonSageMakerFullAccess" + ] + ] + } + ] + } + }, + "SimpleStateMachineRole0CBC135A": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "states.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "SimpleStateMachineRoleDefaultPolicyF5E1CB8E": { + "Type": "AWS::IAM::Policy", + "Properties": { + "PolicyDocument": { + "Statement": [ + { + "Action": [ + "sagemaker:AddTags", + "sagemaker:CreateTransformJob", + "sagemaker:DescribeTransformJob", + "sagemaker:StopTransformJob" + ], + "Effect": "Allow", + "Resource": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":sagemaker:", + { + "Ref": "AWS::Region" + }, + ":", + { + "Ref": "AWS::AccountId" + }, + ":transform-job/*" + ] + ] + } + }, + { + "Action": "sagemaker:ListTags", + "Effect": "Allow", + "Resource": "*" + }, + { + "Action": "iam:PassRole", + "Condition": { + "StringEquals": { + "iam:PassedToService": "sagemaker.amazonaws.com" + } + }, + "Effect": "Allow", + "Resource": { + "Fn::GetAtt": [ + "BatchInferenceTaskSagemakerTransformRoleEABCCE1A", + "Arn" + ] + } + }, + { + "Action": [ + "events:DescribeRule", + "events:PutRule", + "events:PutTargets" + ], + "Effect": "Allow", + "Resource": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":events:", + { + "Ref": "AWS::Region" + }, + ":", + { + "Ref": "AWS::AccountId" + }, + ":rule/StepFunctionsGetEventsForSageMakerTransformJobsRule" + ] + ] + } + } + ], + "Version": "2012-10-17" + }, + "PolicyName": "SimpleStateMachineRoleDefaultPolicyF5E1CB8E", + "Roles": [ + { + "Ref": "SimpleStateMachineRole0CBC135A" + } + ] + } + }, + "SimpleStateMachineE8E2CF40": { + "Type": "AWS::StepFunctions::StateMachine", + "Properties": { + "DefinitionString": { + "Fn::Join": [ + "", + [ + "{\"StartAt\":\"BatchInferenceTask\",\"States\":{\"BatchInferenceTask\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:", + { + "Ref": "AWS::Partition" + }, + ":states:::sagemaker:createTransformJob.sync\",\"Parameters\":{\"ModelName\":\"MyModelName\",\"TransformInput\":{\"DataSource\":{\"S3DataSource\":{\"S3Uri\":\"s3://inputbucket/prefix\",\"S3DataType\":\"S3Prefix\"}}},\"TransformJobName\":\"MyTransformJob\",\"TransformOutput\":{\"S3OutputPath\":\"s3://outputbucket/result\"},\"TransformResources\":{\"InstanceCount\":1,\"InstanceType\":\"ml.m4.xlarge\"}}}}}" + ] + ] + }, + "RoleArn": { + "Fn::GetAtt": [ + "SimpleStateMachineRole0CBC135A", + "Arn" + ] + } + }, + "DependsOn": [ + "SimpleStateMachineRoleDefaultPolicyF5E1CB8E", + "SimpleStateMachineRole0CBC135A" + ], + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Delete" + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/cdk.out new file mode 100644 index 0000000000000..c5cb2e5de6344 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"35.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/integ.json new file mode 100644 index 0000000000000..2d6728a63d38c --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "35.0.0", + "testCases": { + "SqsTest/DefaultTest": { + "stacks": [ + "aws-cdk-step-functions-task-create-transform-job-integ" + ], + "assertionStack": "SqsTest/DefaultTest/DeployAssert", + "assertionStackName": "SqsTestDefaultTestDeployAssert659366A6" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/manifest.json new file mode 100644 index 0000000000000..27092adfc374f --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/manifest.json @@ -0,0 +1,131 @@ +{ + "version": "35.0.0", + "artifacts": { + "aws-cdk-step-functions-task-create-transform-job-integ.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "aws-cdk-step-functions-task-create-transform-job-integ.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "aws-cdk-step-functions-task-create-transform-job-integ": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "aws-cdk-step-functions-task-create-transform-job-integ.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/17277a722928df6f2a0ba8c8d5225ee786b3be9598d636e6411609b626e5a099.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "aws-cdk-step-functions-task-create-transform-job-integ.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "aws-cdk-step-functions-task-create-transform-job-integ.assets" + ], + "metadata": { + "/aws-cdk-step-functions-task-create-transform-job-integ/BatchInferenceTask/SagemakerTransformRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "BatchInferenceTaskSagemakerTransformRoleEABCCE1A" + } + ], + "/aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Role/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "SimpleStateMachineRole0CBC135A" + } + ], + "/aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Role/DefaultPolicy/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "SimpleStateMachineRoleDefaultPolicyF5E1CB8E" + } + ], + "/aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "SimpleStateMachineE8E2CF40" + } + ], + "/aws-cdk-step-functions-task-create-transform-job-integ/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-cdk-step-functions-task-create-transform-job-integ/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "aws-cdk-step-functions-task-create-transform-job-integ" + }, + "SqsTestDefaultTestDeployAssert659366A6.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "SqsTestDefaultTestDeployAssert659366A6.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "SqsTestDefaultTestDeployAssert659366A6": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "SqsTestDefaultTestDeployAssert659366A6.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "SqsTestDefaultTestDeployAssert659366A6.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "SqsTestDefaultTestDeployAssert659366A6.assets" + ], + "metadata": { + "/SqsTest/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/SqsTest/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "SqsTest/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/tree.json new file mode 100644 index 0000000000000..a6cc8e5f15382 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.js.snapshot/tree.json @@ -0,0 +1,363 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "aws-cdk-step-functions-task-create-transform-job-integ": { + "id": "aws-cdk-step-functions-task-create-transform-job-integ", + "path": "aws-cdk-step-functions-task-create-transform-job-integ", + "children": { + "BatchInferenceTask": { + "id": "BatchInferenceTask", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/BatchInferenceTask", + "children": { + "SagemakerTransformRole": { + "id": "SagemakerTransformRole", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/BatchInferenceTask/SagemakerTransformRole", + "children": { + "ImportSagemakerTransformRole": { + "id": "ImportSagemakerTransformRole", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/BatchInferenceTask/SagemakerTransformRole/ImportSagemakerTransformRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/BatchInferenceTask/SagemakerTransformRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "sagemaker.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/AmazonSageMakerFullAccess" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.SageMakerCreateTransformJob", + "version": "0.0.0" + } + }, + "SimpleStateMachine": { + "id": "SimpleStateMachine", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine", + "children": { + "Role": { + "id": "Role", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Role", + "children": { + "ImportRole": { + "id": "ImportRole", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Role/ImportRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Role/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "states.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + }, + "DefaultPolicy": { + "id": "DefaultPolicy", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Role/DefaultPolicy", + "children": { + "Resource": { + "id": "Resource", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Role/DefaultPolicy/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Policy", + "aws:cdk:cloudformation:props": { + "policyDocument": { + "Statement": [ + { + "Action": [ + "sagemaker:AddTags", + "sagemaker:CreateTransformJob", + "sagemaker:DescribeTransformJob", + "sagemaker:StopTransformJob" + ], + "Effect": "Allow", + "Resource": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":sagemaker:", + { + "Ref": "AWS::Region" + }, + ":", + { + "Ref": "AWS::AccountId" + }, + ":transform-job/*" + ] + ] + } + }, + { + "Action": "sagemaker:ListTags", + "Effect": "Allow", + "Resource": "*" + }, + { + "Action": "iam:PassRole", + "Condition": { + "StringEquals": { + "iam:PassedToService": "sagemaker.amazonaws.com" + } + }, + "Effect": "Allow", + "Resource": { + "Fn::GetAtt": [ + "BatchInferenceTaskSagemakerTransformRoleEABCCE1A", + "Arn" + ] + } + }, + { + "Action": [ + "events:DescribeRule", + "events:PutRule", + "events:PutTargets" + ], + "Effect": "Allow", + "Resource": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":events:", + { + "Ref": "AWS::Region" + }, + ":", + { + "Ref": "AWS::AccountId" + }, + ":rule/StepFunctionsGetEventsForSageMakerTransformJobsRule" + ] + ] + } + } + ], + "Version": "2012-10-17" + }, + "policyName": "SimpleStateMachineRoleDefaultPolicyF5E1CB8E", + "roles": [ + { + "Ref": "SimpleStateMachineRole0CBC135A" + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnPolicy", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Policy", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/SimpleStateMachine/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::StepFunctions::StateMachine", + "aws:cdk:cloudformation:props": { + "definitionString": { + "Fn::Join": [ + "", + [ + "{\"StartAt\":\"BatchInferenceTask\",\"States\":{\"BatchInferenceTask\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:", + { + "Ref": "AWS::Partition" + }, + ":states:::sagemaker:createTransformJob.sync\",\"Parameters\":{\"ModelName\":\"MyModelName\",\"TransformInput\":{\"DataSource\":{\"S3DataSource\":{\"S3Uri\":\"s3://inputbucket/prefix\",\"S3DataType\":\"S3Prefix\"}}},\"TransformJobName\":\"MyTransformJob\",\"TransformOutput\":{\"S3OutputPath\":\"s3://outputbucket/result\"},\"TransformResources\":{\"InstanceCount\":1,\"InstanceType\":\"ml.m4.xlarge\"}}}}}" + ] + ] + }, + "roleArn": { + "Fn::GetAtt": [ + "SimpleStateMachineRole0CBC135A", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_stepfunctions.CfnStateMachine", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_stepfunctions.StateMachine", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-cdk-step-functions-task-create-transform-job-integ/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "SqsTest": { + "id": "SqsTest", + "path": "SqsTest", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "SqsTest/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "SqsTest/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "SqsTest/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "SqsTest/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "SqsTest/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.ts new file mode 100644 index 0000000000000..508a69167d519 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/sagemaker/integ.create-transform-job.ts @@ -0,0 +1,45 @@ +import { App, Stack, StackProps } from 'aws-cdk-lib'; +import { InstanceType, InstanceClass, InstanceSize } from 'aws-cdk-lib/aws-ec2'; +import { SageMakerCreateTransformJob, S3DataType } from 'aws-cdk-lib/aws-stepfunctions-tasks'; +import { StateMachine, IntegrationPattern, DefinitionBody } from 'aws-cdk-lib/aws-stepfunctions'; +import * as integ from '@aws-cdk/integ-tests-alpha'; + +class StepFunctionsTaskCreateTransformJobIntegStack extends Stack { + constructor(scope: App, id: string, props?: StackProps) { + super(scope, id, props); + + const task = new SageMakerCreateTransformJob(this, 'BatchInferenceTask', { + transformJobName: 'MyTransformJob', + modelName: 'MyModelName', + transformInput: { + transformDataSource: { + s3DataSource: { + s3Uri: 's3://inputbucket/prefix', + s3DataType: S3DataType.S3_PREFIX, + }, + }, + }, + transformOutput: { + s3OutputPath: 's3://outputbucket/result', + }, + transformResources: { + instanceCount: 1, + instanceType: InstanceType.of(InstanceClass.M4, InstanceSize.XLARGE), + }, + integrationPattern: IntegrationPattern.RUN_JOB, + }); + + new StateMachine(this, 'SimpleStateMachine', { + definitionBody: DefinitionBody.fromChainable(task), + }); + } +} + +const app = new App(); +const stack = new StepFunctionsTaskCreateTransformJobIntegStack(app, 'aws-cdk-step-functions-task-create-transform-job-integ'); + +new integ.IntegTest(app, 'SqsTest', { + testCases: [stack], +}); + +app.synth(); \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/sagemaker/create-transform-job.ts b/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/sagemaker/create-transform-job.ts index 9503c6e432339..324c3a1728483 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/sagemaker/create-transform-job.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/sagemaker/create-transform-job.ts @@ -282,6 +282,16 @@ export class SageMakerCreateTransformJob extends sfn.TaskStateBase { }), ], }), + new iam.PolicyStatement({ + actions: ['sagemaker:AddTags'], + resources: [ + stack.formatArn({ + service: 'sagemaker', + resource: 'transform-job', + resourceName: '*', + }), + ], + }), ); } diff --git a/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/sagemaker/create-transform-job.test.ts b/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/sagemaker/create-transform-job.test.ts index 116da381909b2..0ae4168103386 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/sagemaker/create-transform-job.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/sagemaker/create-transform-job.test.ts @@ -1,3 +1,4 @@ +import { Template } from '../../../assertions'; import * as ec2 from '../../../aws-ec2'; import * as iam from '../../../aws-iam'; import * as kms from '../../../aws-kms'; @@ -248,6 +249,7 @@ test('pass param to transform job', () => { }, }); }); + test('create transform job with instance type supplied as JsonPath', () => { // WHEN const task = new SageMakerCreateTransformJob(stack, 'TransformTask', { @@ -306,3 +308,215 @@ test('create transform job with instance type supplied as JsonPath', () => { }, }); }); + +test('required permissions are granted to service role if RUN_JOB is supplied as service integration pattern', () => { + // WHEN + const task = new SageMakerCreateTransformJob(stack, 'TransformTask', { + transformJobName: 'MyTransformJob', + modelName: 'MyModelName', + transformInput: { + transformDataSource: { + s3DataSource: { + s3Uri: 's3://inputbucket/prefix', + }, + }, + }, + transformOutput: { + s3OutputPath: 's3://outputbucket/prefix', + }, + integrationPattern: sfn.IntegrationPattern.RUN_JOB, + }); + + new sfn.StateMachine(stack, 'MyStateMachine', { + definitionBody: sfn.DefinitionBody.fromChainable(task), + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { + PolicyDocument: { + Statement: [ + { + Action: [ + 'sagemaker:CreateTransformJob', + 'sagemaker:DescribeTransformJob', + 'sagemaker:StopTransformJob', + ], + Effect: 'Allow', + Resource: { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':sagemaker:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':transform-job/*', + ], + ], + }, + }, + { + Action: 'sagemaker:ListTags', + Effect: 'Allow', + Resource: '*', + }, + { + Action: 'iam:PassRole', + Condition: { + StringEquals: { + 'iam:PassedToService': 'sagemaker.amazonaws.com', + }, + }, + Effect: 'Allow', + Resource: { + 'Fn::GetAtt': [ + 'TransformTaskSagemakerTransformRoleEB12FAC2', + 'Arn', + ], + }, + }, + { + Action: [ + 'events:PutTargets', + 'events:PutRule', + 'events:DescribeRule', + ], + Effect: 'Allow', + Resource: { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':events:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':rule/StepFunctionsGetEventsForSageMakerTransformJobsRule', + ], + ], + }, + }, + { + Action: 'sagemaker:AddTags', + Effect: 'Allow', + Resource: { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':sagemaker:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':transform-job/*', + ], + ], + }, + }, + ], + Version: '2012-10-17', + }, + }); +}); + +test('required permissions are granted to service role if REQUEST_RESPONSE is supplied as service integration pattern', () => { + // WHEN + const task = new SageMakerCreateTransformJob(stack, 'TransformTask', { + transformJobName: 'MyTransformJob', + modelName: 'MyModelName', + transformInput: { + transformDataSource: { + s3DataSource: { + s3Uri: 's3://inputbucket/prefix', + }, + }, + }, + transformOutput: { + s3OutputPath: 's3://outputbucket/prefix', + }, + integrationPattern: sfn.IntegrationPattern.REQUEST_RESPONSE, + }); + + new sfn.StateMachine(stack, 'MyStateMachine', { + definitionBody: sfn.DefinitionBody.fromChainable(task), + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { + PolicyDocument: { + Statement: [ + { + Action: [ + 'sagemaker:CreateTransformJob', + 'sagemaker:DescribeTransformJob', + 'sagemaker:StopTransformJob', + ], + Effect: 'Allow', + Resource: { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':sagemaker:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':transform-job/*', + ], + ], + }, + }, + { + Action: 'sagemaker:ListTags', + Effect: 'Allow', + Resource: '*', + }, + { + Action: 'iam:PassRole', + Condition: { + StringEquals: { + 'iam:PassedToService': 'sagemaker.amazonaws.com', + }, + }, + Effect: 'Allow', + Resource: { + 'Fn::GetAtt': [ + 'TransformTaskSagemakerTransformRoleEB12FAC2', + 'Arn', + ], + }, + }, + ], + Version: '2012-10-17', + }, + }); +}); \ No newline at end of file