skip to Main Content

I’m trying to import an existing AWS Lambda function into my Terraform state, but I’m encountering an unexpected behavior with the function name.
When I list my Lambda functions using the AWS CLI, I see the correct function name:
Copyaws lambda list-functions

{
    "Functions": [
        {
            "FunctionName": "my-lambda-function",
            "FunctionArn": "arn:aws:lambda:eu-west-3:123456789012:function:my-lambda-function",
            ...
        }
    ]
}

However, when I import this function into Terraform using:

terraform import aws_lambda_function.my_lambda_function arn:aws:lambda:eu-west-3:123456789012:function:my-lambda-function

And then check the state with terraform show, I see that the function_name is set to the full ARN instead of just "my-lambda-function":

resource "aws_lambda_function" "my_lambda_function" {
    function_name = "arn:aws:lambda:eu-west-3:123456789012:function:my-lambda-function"
    ...
}

This causes issues because Terraform now thinks the function name should be the full ARN, which doesn’t match the actual resource in AWS.
My Terraform configuration file defines the function name correctly:

resource "aws_lambda_function" "my_lambda_function" {
  function_name = "my-lambda-function"
  ...
}

How can I resolve this discrepancy and get Terraform to recognize the correct short name of the Lambda function after import?

i removed the resource from the state file and imported again but the same problem persist

2

Answers


  1. Chosen as BEST ANSWER

    Yes that was exactly what happened and it worked well,thanks


  2. during the import process terraform sometimes misinterprets the resource ID provided (in this case, the ARN) and assigns it directly to the function_name attribute, which should only be the short name of the lambda function.

    remove the lambda from terraform state:

    terraform state rm aws_lambda_function.my_lambda_function
    

    manually import your lambda function:

    terraform import aws_lambda_function.my_lambda_function my-lambda-function
    
    

    run terraform show:

    terraform show
    

    then you can plan and apply

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search