Terraform Stepfunctions
#AWS_Step_Functions #terraform
公式モジュールから作成する
https://registry.terraform.io/modules/terraform-aws-modules/step-functions/aws/latest
便利ポイントは、StepFunctionが各サービスを呼び出すために必要になるポリシーとロールを自動で作成してくれる
自分でポリシー書くのだるいから実際便利
Service integration policies
locals.tfにdefault_resoucesが定義されているものはtrueするだけで良い
aws_batchとかはこっち
default_resourcesが定義されていない物については、具体的にsfがトリガーするarnを配列で渡す必要がある
lambdaとかはこっち
AWS Batchのサービスインテグレーションでエラー
Error: error creating Step Function State Machine (dlsite_scraper_batch_sf): AccessDeniedException: 'arn:aws:iam::677203041315:role/dlsite_scraper_batch_sf' is not authorized to create managed-rule.
IAMロールのポリシー権限でevent関係が作られないことが原因
正しく作られた場合はStepFunctionsGetEventsForBatchJobsRuleというリソースにイベントの許可がある文言があるがこのモジュールから作られたポリシーにはこの部分がなかった
code:json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"batch:SubmitJob",
"batch:DescribeJobs",
"batch:TerminateJob"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"events:PutTargets",
"events:PutRule",
"events:DescribeRule"
],
"Resource": [
"arn:aws:events:ap-northeast-1:677203041315:rule/StepFunctionsGetEventsForBatchJobsRule"
]
}
]
}
原因はこれ(Make it easier to use service integrations that require events permissions)
default_resourcesという定数を定義してmoduleの使用者はevents = trueという風に指定すれば解決できる。
はずだった...
events = trueこれを指定してもうまくいかない
該当箇所のコードを見てみると
code:text
batch = {
actions = [
"batch:SubmitJob",
"batch:DescribeJobs",
"batch:TerminateJob"
]
default_resources = "*"
}
events = {
actions = [
"events:PutTargets",
"events:PutRule",
"events:DescribeRule"
]
}
default_resources = "arn:aws:events:${local.aws_region}:${data.aws_caller_identity.current.account_id}:rule/StepFunctionsGetEventsForBatchJobsRule"
}
と定義されていたがdefault_resoucesを定義する箇所はeventsの中に無いといけないのではないか?
現にecs_Syncの方ではeventsの中のブロックにdefault_resourcesが定義されている
https://gyazo.com/9babdb10cf005ae5121f65a343ff1d24
ただ他のサービスでもeventsの外に定義されているものは多くあってこれが正常なのか、間違いなのか断定はできない気もする
外にある例
https://gyazo.com/489fdaef411c7a8f8982557445b5a3e2
saemaker
https://gyazo.com/d0968910f10c7973f53b0d40560e7057
batch
このeventsの対処法としてevents = trueではなく、自分でリソースを指定する書き方(多くのサービスでリソースを指定するのに書く方法)でやってみた
code:text
module "step_function" {
source = "terraform-aws-modules/step-functions/aws"
name = "dlsite_scraper_batch_sf"
type = "STANDARD"
definition = templatefile("./stepfunctions_template/dlsite_scraper_batch_sf_json.tpl", {
job_definition_arn = aws_batch_job_definition.dlsite_scraping_job_fargate_definition.arn
job_queue_arn = aws_batch_job_queue.dlsite_shuffle_recommender_batch_fargate_queue.arn
}
)
service_integrations = {
batch_Sync = {
batch = true
events = "arn:aws:events:${data.aws_region.current_rg.name}:${data.aws_caller_identity.current_id.account_id}:rule/StepFunctionsGetEventsForECSTaskRule"
# events = true
}
}
}
data "aws_caller_identity" "current_id" {}
data "aws_region" "current_rg" {}
しかしこの書き方でもダメだった/icons/うーん.icon/icons/うーん.icon/icons/うーん.icon/icons/やれやれ.icon/icons/やれやれ.icon/icons/やれやれ.icon
このissueを見つけたのだけど
iam:PassRoleが何か関係しているのかな
解決!!!!
/diary-hiroki/2022/8/7