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.85k stars 9.2k forks source link

aws_lambda_layer_version_permission is not idempotent when more than one permission exist for a layer version #21917

Open Jorge-Rodriguez opened 2 years ago

Jorge-Rodriguez commented 2 years ago

Community Note

Terraform CLI and Terraform AWS Provider Version

Terraform v1.0.8
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v3.67.0

Affected Resource(s)

Terraform Configuration Files

resource "aws_lambda_layer_version_permission" "authorizer" {
  for_each = toset([local.inari_accounts.staging.id, local.inari_accounts.production.id])

  layer_name     = module.authorizer_layer.lambda_layer_layer_arn
  version_number = module.authorizer_layer.lambda_layer_version
  principal      = each.value
  statement_id   = "${module.authorizer_layer.lambda_layer_version}-${each.value}"
  action         = "lambda:GetLayerVersion"
}

Error Output

β•·
β”‚ Error: error adding Lambda Layer Version Permission (layer: arn:aws:lambda:eu-west-1:***:layer:authorizer_layer, version: 52): ResourceConflictException: The statement id (52-728314938062) provided already exists. Please provide a new statement id, or remove the existing statement.
β”‚ {
β”‚   RespMetadata: {
β”‚     StatusCode: 409,
β”‚     RequestID: "988722c4-d407-4244-bd1f-e4b7b6a4207b"
β”‚   },
β”‚   Message_: "The statement id (52-728314938062) provided already exists. Please provide a new statement id, or remove the existing statement.",
β”‚   Type: "User"
β”‚ }
β”‚ 
β”‚   with aws_lambda_layer_version_permission.authorizer["728314938062"],
β”‚   on apigw.tf line 55, in resource "aws_lambda_layer_version_permission" "authorizer":
β”‚   55: resource "aws_lambda_layer_version_permission" "authorizer" {
β”‚ 
β•΅

Expected Behavior

No changes to the infrastructure, infrastructure is up to date

Actual Behavior

The resources are tentatively replaced

  # aws_lambda_layer_version_permission.okta_authorizer["008049879029"] must be replaced
-/+ resource "aws_lambda_layer_version_permission" "okta_authorizer" {
      ~ id             = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer,3" -> (known after apply)
      ~ layer_name     = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer" -> (known after apply) # forces replacement
      ~ policy         = jsonencode(
            {
              - Id        = "default"
              - Statement = [
                  - {
                      - Action    = "lambda:GetLayerVersion"
                      - Effect    = "Allow"
                      - Principal = {
                          - AWS = "arn:aws:iam::008049879029:root"
                        }
                      - Resource  = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer:3"
                      - Sid       = "3-008049879029"
                    },
                  - {
                      - Action    = "lambda:GetLayerVersion"
                      - Effect    = "Allow"
                      - Principal = {
                          - AWS = "arn:aws:iam::728314938062:root"
                        }
                      - Resource  = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer:3"
                      - Sid       = "3-728314938062"
                    },
                ]
              - Version   = "2012-10-17"
            }
        ) -> (known after apply)
      ~ revision_id    = "e86df11f-2890-4a0b-9698-7ecc5c131c24" -> (known after apply)
      ~ statement_id   = "3-008049879029" -> (known after apply) # forces replacement
      ~ version_number = 3 -> (known after apply) # forces replacement
        # (2 unchanged attributes hidden)
    }

  # aws_lambda_layer_version_permission.okta_authorizer["728314938062"] must be replaced
-/+ resource "aws_lambda_layer_version_permission" "okta_authorizer" {
      ~ id             = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer,3" -> (known after apply)
      ~ layer_name     = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer" -> (known after apply) # forces replacement
      ~ policy         = jsonencode(
            {
              - Id        = "default"
              - Statement = [
                  - {
                      - Action    = "lambda:GetLayerVersion"
                      - Effect    = "Allow"
                      - Principal = {
                          - AWS = "arn:aws:iam::008049879029:root"
                        }
                      - Resource  = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer:3"
                      - Sid       = "3-008049879029"
                    },
                  - {
                      - Action    = "lambda:GetLayerVersion"
                      - Effect    = "Allow"
                      - Principal = {
                          - AWS = "arn:aws:iam::728314938062:root"
                        }
                      - Resource  = "arn:aws:lambda:eu-west-1:***:layer:okta_authorizer_layer:3"
                      - Sid       = "3-728314938062"
                    },
                ]
              - Version   = "2012-10-17"
            }
        ) -> (known after apply)
      ~ principal      = "008049879029" -> "728314938062" # forces replacement
      ~ revision_id    = "e86df11f-2890-4a0b-9698-7ecc5c131c24" -> (known after apply)
      ~ statement_id   = "3-008049879029" -> (known after apply) # forces replacement
      ~ version_number = 3 -> (known after apply) # forces replacement
        # (1 unchanged attribute hidden)
    }

Steps to Reproduce

  1. terraform apply

Important Factoids

Note the change in principal on the last planned resource change. It would appear that terraform is comparing the second resource configuration with the first of the permissions applied to the layer version, not the second one.

Jorge-Rodriguez commented 2 years ago

Ping @anGie44 @Avolynsk

fishfacemcgee commented 1 year ago

Not sure what methods, if any, there are to "bump" an issue, but just want to report that this resource is still bugged. Going by the error, it appears the statement does not get deleted. My guess is it's related to whatever's leading the import behavior to be tied to the layer ARN and version, which is not specific enough given how granular the resource is.

vedimuthan commented 1 month ago

I am facing the same problem and surprised that this issue is not being addressed for more than 3 years.