hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.74k stars 9.1k forks source link

[Bug]: Apply must be run twice to deploy all resources #38634

Open cam-slyp opened 1 month ago

cam-slyp commented 1 month ago

Terraform Core Version

1.9.1, 1.9.3

AWS Provider Version

5.60.0

Affected Resource(s)

aws_lambda_function

Expected Behavior

When an sns topic and lambda environment variable referencing the topic arn are added, both the sns topic and lambda should be modified in a single apply

Actual Behavior

Two runs of apply are required to apply all changes

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.60.0"
    }
  }

  required_version = "1.9.1"
}

provider "aws" {
  region = "ap-southeast-2"
}

resource "aws_iam_role" "iam_for_lambda" {
  name = "testing_tf_iam_role"

  managed_policy_arns = [
    "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
  ]

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Principal = {
          Service = "lambda.amazonaws.com"
        }

      }
    ]
  })
}

resource "aws_lambda_function" "test_tf_lambda" {
  filename      = "lambda_src/lambdazip.zip"
  function_name = "testing_lambda"
  role          = aws_iam_role.iam_for_lambda.arn
  handler       = "index.handler"
  runtime       = "nodejs20.x"
  # environment {
  #   variables = {
  #     SNS_TOPIC_ARN = aws_sns_topic.test_tf_topic.arn
  #   }
  # }
}

# resource "aws_sns_topic" "test_tf_topic" {
#   name = "test_tf_topic"
# }

Steps to Reproduce

  1. Run terraform apply to deploy role and lambda function
  2. Uncomment both the commented sections
  3. Run terraform apply

The diff will only show the topic creation, with no changes to the lambda

  1. Run terraform apply

The diff will now show the changes to the lambda function

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 month ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

justinretzolk commented 2 weeks ago

Similar #38755

justinretzolk commented 2 weeks ago

Whoops! Sorry about the closure!