Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provision existing AWS lambda using Terraform

I was learning terraform and was asked to provision it for CI/CD pipeline at gitlab. My doubt is that ,

Let's say a lambda function is already running/live. How can I provision it using terraform ? Should I use data block to consume the running aws lambda? Or this isn't how it works ! I am not sure how can we do this. I searched the docs which isn't supporting this use case.

like image 741
That Coder Avatar asked Oct 24 '25 15:10

That Coder


1 Answers

So with the Lambda function that is already running, basically here you have two use cases:

  • whether you want to add further changes/updates to that Lambda later on using Terraform. In this case, you need to import it to your terraform code, and all the changes you add to that Lambda can be deployed via your CI/CD pipeline, e.g.:
    terraform import aws_lambda_function.my_lambda existing_lambda_function_name

Note: Please note that the my_lambda function is your terraform block of code that is defining the exact Lambda that is already running, this is to match the existing resource with your code Terraform, to then be added to the state. I hope that it is clear

  • or you simply just need some outputs of that Lambda to be used as inputs to other services, here you can simply just keep the Lambda up and running and use Terraform data source, e.g.:
    data "aws_lambda_function" "existing_lambda" {
      function_name = var.function_name
    }

And somewhere else in your code you can use it as follows:

    function_name = data.aws_lambda_function.existing_lambda

I hope this was helpful

like image 108
Ouma Avatar answered Oct 26 '25 12:10

Ouma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!