Using Terraform to create Lambda function using code from S3 in different regions

Question:

I have a bucket previously created in the us-east-1 region that contains the Lambda code I want to deploy in other regions. When I try a Terraform deploy of that same code in the us-west-2 region I get the following error:

I see no way of specifying the region of the S3 bucket in Terraform’s aws_lambda_function resource.

Is it possible, using Terraform, to have an S3 bucket in one region provide the Lambda code that can be deployed in other regions?

Answer:

Lambda functions that specify an S3 bucket for the source must reside in the same region as the S3 bucket. This is because behind the scenes they are pulling the code from S3 on demand as the Lambda needs deploying. Pulling across regions would add large amounts of latency and also now create cross region dependencies.

You’ll need to move the Lambda package into the new region which you could potentially do with cross region replication on the S3 bucket or by simply uploading the Lambda function package into an S3 bucket in the other region manually.

Leave a Reply