简体   繁体   中英

Create Resources via terraform

I created an AWS environment using TERRAFORM.

After that, some resources were created by console (SES, SNS, LAMBDA) they did not was provisioned by TERRAFORM.

I'm writing the TERRAFORM code for these resources (SES, SNS, LAMBDA) that were created by the console.

If I already have these resources running in my account, is it possible to generate this code via TERRAFORM for these resources without removing them?

Or even, how do I have to proceed in this case?

Welcome to the world of IaC, you're in for a treat. :)

You can import all resources that were created without terraform (using a CLI or manually provisioned - resources which are not part of the tf state) to your terraform state. Once these resources are imported you can then start managing their lifecycle using terraform.

  1. Define the resource in your.tf files
  2. Import existing resources

As an example:

In order to import an existing non terraform managed lambda, you first define the resource for it in your.tf files:

main.tf:

resource "aws_lambda_function" "test_lambda" {
  filename      = "lambda_function_payload.zip"
  function_name = "lambda_function_name"
  role          = "${aws_iam_role.iam_for_lambda.arn}"
  handler       = "exports.test"

  # The filebase64sha256() function is available in Terraform 0.11.12 and later
  # For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
  # source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
  source_code_hash = "${filebase64sha256("lambda_function_payload.zip")}"

  runtime = "nodejs12.x"

  environment {
    variables = {
      foo = "bar"
    }
  }
}

Then you can execute terraform import, in order to import the existing lambda:

terraform import aws_lambda_function.test_lambda my_test_lambda_function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM