skip to Main Content

I am trying to add python klayer to lambda in terraform (as in snapshot done on console) but its giving error on terraform plan (Error: Import block target does not exist) (The target for the given import block does not exist)

I am not sure what is the correct terraform code for it, I tried to import and terraform plan failed.

enter image description here

Here is my code:

import {
  to =   aws_lambda_function.lambda_function
  #to = aws_lambda_layer_version.test_layer
  id = "arn:aws:lambda:eu-west-1:770693421928:layer:Klayers-p311-requests:3"
}

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for your guidance, I am sure, as you said, if we list the ARN it should work. I will try it out and share result.

    The method however, I used is different as under. Created a klayer, zipped, placed in S3 and referenced it and it worked.

    resource "aws_lambda_layer_version" "klayers" {
      layer_name          = "Lambda-Layer"
      s3_bucket           = "abcd"
      s3_key              = "abcd/requests.zip"
      compatible_runtimes = ["python3.9"]
      description         = "Lambda Layer for Python Requests"
    }
    

  2. (Edited after seeing MarkB’s comment on the question)

    You are trying to import someone else’s layer into your Terraform state, which you can’t do because it’s not in your account.

    To make use of that layer, you don’t need to import it at all, you can just list its ARN directly in the Lambda Function definition:

    locals {
      # We use this layer because ...
      klayers_arn = "arn:aws:lambda:eu-west-1:770693421928:layer:Klayers-p311-requests:3"
    }
    
    resource "aws_lambda_function" "example" {
      # ... other configuration ...
      layers = [local.klayers_arn]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search