How to Delete all Resources Except One in Terraform

If you need to delete all resources created by Terraform, except for a single, specific one, then you can do the following: Step 1 – Get the current state list terraform state list Step 2 – Remove the exception resource Remove the specific resource that you don’t want Terraform to track anymore. terraform state rm <resource_to_be_removed> Step 3 – Destroy the resources terraform destroy

October 23, 2022 · 1 min · 64 words · Andrew

How to create an AWS Lambda in Terraform

Step 1 – Lambda Code in Python The following code expects a python file with the contents as follows in a file called python/ def lambda_handler(event, context): return { 'code': 200, 'message': 'Triggered' } Step 2 – Lambda Code in Terraform Now we create the data "archive_file" "zip_python_code_create" { type = "zip" source_file = "python/" output_path = "python/" } resource "aws_lambda_function" "lambda_script1" { filename = "python/" function_name = "LambdaScript1" role = aws_iam_role....

June 27, 2022 · 1 min · 205 words · Andrew

[Solved] Instance Profile already exists in Terraform

If you get the Instance Profile already exists error when running your Terraform apply, then it is either because an instance profile with that name already existed in the account, or because the previous terraform destroy did not remove it. Step 1 – Identify the Instance Profiles in the account aws iam list-instance-profiles Step 2 – Delete the Instance Profile/s aws iam delete-instance-profile --instance-profile-name <name-of-instance-profile>

June 26, 2022 · 1 min · 65 words · Andrew

How to use a Backend-Config File for Terraform S3 State Configuration

If you are using S3 as the backend for your state management in Terraform, then you can create environment specific configuration files where you store the bucket, region among other things to be pulled in automatically when switching environments. Step 1 – Create environment specific configs Create a dev.conf file with the following content: bucket = "your-bucket" region = "your-aws-region" key = "path/to/key" Step 2 – Set the terraform backend In your main....

June 16, 2022 · 1 min · 130 words · Andrew

How to Refresh State in Terraform

Sometimes your infrastructure may be out of sync with your Terraform state because resources may have been amended or deleted manually. For this you can refresh the Terraform state. Using the refresh command terraform refresh [options] Why this command may appear strange at first, not that it is merely an alias for: terraform apply -refresh-only -auto-approve If you don’t want to auto-approve the transaction, then you can perform a refresh as follows:...

June 15, 2022 · 1 min · 106 words · Andrew

How to write If/Else Statements in Terraform

If/Else statements are conditional statements that perform decisions based on some known state, or variable. Terraform allows you to perform these if/else statements using a ternary operation which have become popular as short-form if/else statements in many programming languages. Where a language would write something similar to: let truthy = false if (something == "value") { truthy = true } else { truthy = false } The ternary alternative would be:...

June 13, 2022 · 2 min · 220 words · Andrew

How to Remove a Resource from Terraform State

Terraform state reflects the most up to date reference of the infrastructure. Sometimes you may need to remove a resource from the Terraform state manually. This is helpful if a resource has been deleted or modified manually and the resource definition has been removed in your Terraform code. This way you can re-run your Terraform plan and apply without deleting the remote infra resource that you removed from the state....

June 4, 2022 · 1 min · 143 words · Andrew

How to Delete a Specific Resource in Terraform

Running terraform destroy will tear down the whole stack associated to some terraform code. However, sometimes you might only want to remove a specific piece of your infrastructure. To do this, you can use the terraform destroy -target object. Step 1 – List the State Get a list of all the resources from the state: terraform state list #data.aws_ami.webserver_ami #aws_autoscaling_group.asg-web[0] #random_string.rand3 #... Step 2 – Remove a Specific Resource Run a terraform destroy -target and pass a resource from the state list above:...

June 3, 2022 · 1 min · 107 words · Andrew

How to Create a DynamoDB Lock Table for Terraform

Provision the DynamoDB Table resource "aws_dynamodb_table" "dynamodb-terraform-state-lock" { name = "terraform-state-lock-dynamo" hash_key = "LockID" read_capacity = 20 write_capacity = 20 attribute { name = "LockID" type = "S" } } Configure the DynamoDB table in Terraform Backend terraform { backend "s3" { encrypt = true bucket = "your-unique-bucket-name" dynamodb_table = "terraform-state-lock-dynamo" key = "terraform.tfstate" region = "us-east-1" } }

June 2, 2022 · 1 min · 59 words · Andrew

How to add Comments in Terraform

Terraform support three (3) distinct syntax for adding comments. # – Single line comment # begins a single-line comment, and comments everything that follows on the same line. // – Alternate single line comment The same as a # but added as an alternative. Often more familiar to most programmers in other languages. Purely a preference for some. Also note that many popular formatting tools will replace // with # when found....

June 1, 2022 · 1 min · 95 words · Andrew