Terraform 1.4.3: Facing Dependency Issues When Creating Multiple AWS Lambda Functions with Shared IAM Role
I'm sure I'm missing something obvious here, but I've looked through the documentation and I'm still confused about I'm encountering a dependency issue while trying to deploy multiple AWS Lambda functions that share the same IAM role using Terraform 1.4.3. My intention is to create a single IAM role and then reference it in several Lambda function resources. However, I'm facing the following error during the `terraform apply` phase: ``` Error: Error creating Lambda function: InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda. ``` I've tried restructuring my code to ensure that the IAM role is defined before the Lambda functions, but I'm still getting the same error. Hereβs the relevant part of my Terraform configuration: ```hcl resource "aws_iam_role" "lambda_role" { name = "my_lambda_role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Principal = { Service = "lambda.amazonaws.com" } Effect = "Allow" Sid = "" } ] }) } resource "aws_lambda_function" "function1" { function_name = "my_first_function" role = aws_iam_role.lambda_role.arn handler = "index.handler" runtime = "nodejs14.x" source_code_hash = filebase64sha256("path/to/your/code.zip") } resource "aws_lambda_function" "function2" { function_name = "my_second_function" role = aws_iam_role.lambda_role.arn handler = "index.handler" runtime = "nodejs14.x" source_code_hash = filebase64sha256("path/to/your/other_code.zip") } ``` I verified that the IAM role is being created before the Lambda functions, and I also double-checked the trust relationship for the IAM role. The Lambda function code structures are correct, and the paths to the zip files are valid. Additionally, I've ensured that I have the necessary permissions to create IAM roles and Lambda functions in my AWS account. However, the error persists which makes me suspect there might be an issue with the timing of the role being available to the Lambda functions during the execution. Anyone experienced similar issues or have suggestions on how to resolve this dependency error? Am I missing something obvious? For context: I'm using Hcl on Debian. Any ideas what could be causing this?