How to import an external lambda layer into terraform?

I’m trying to use datadog lambda layer in my lambda function to send datadog metrics:


I can set this up manually from console. However, I’m not sure how to get it working in terraform. So far I have the following resource:

resource "aws_lambda_layer_version" "datadog_layer" {
  layer_name = "${local.datadog_layer_name}"

  compatible_runtimes = ["python2.7"]
}

And I tried to import it with:

terraform import ...<my_module>.aws_lambda_layer_version.datadog_layer "arn:aws:lambda:us-west-2:464622532012:layer:Datadog-Python27:9"

However, I’m getting the following error:

Error: Reference to undeclared resource

  on 
.terraform/modules/mymodule/modules/lambda_function/main.tf line 24, in resource "aws_lambda_function" "lambda_function":
  24:   layers        = ["${aws_lambda_layer_version.datadog.arn}"]

A managed resource "aws_lambda_layer_version" "datadog" has not been declared

in mymodule.my_lambda.lambda_function.

The reason I used the resource instead of the data source is because in terraform doc for the resource it shows that we can import it:

Any suggestion on how I can make this work?

Never mind. stupid mistake. I misquoted the resource name.

Hello there,

I am having a similar issue as you. When I run

terraform import aws_lambda_layer_version.datadog_layer arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Python38:52

I am getting

Error: Cannot import non-existent remote object
│
│ While attempting to import an existing object to "aws_lambda_layer_version.datadog_layer", the provider detected that no object exists with the given id. Only pre-existing objects can be imported; check that
│ the id is correct and that it is associated with the provider's configured region or endpoint, or use "terraform apply" to create a new remote object for this resource.

Is this because the lambda layer resides in a different account than mine?