Terraform Backend Using S3 and DynamoDB With State Locking

Terraform Backend Using S3 and DynamoDB With State Locking

Terraform is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. This includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. Terraform can manage both existing service providers and custom in-house solutions.

You must have installed the following things

  • AWS CLI Install and Configure -?url
  • Terraform Install -?url

We can use the following easy steps

Step 1 -?Create an Empty Directory and open it via the code editor.

VS-Code View

Step 2 -?Inside the empty folder create a file called provider.tf and add the following content.

terraform {
? required_providers {
? ? aws = {
? ? ? source ?= "hashicorp/aws"
? ? ? version = "~> 4.16"
? ? }
? }
}        

Step 3 -?Create backend.tf file and add S3 bucket and DynamoDb table details.

  • In the AWS management console and create an S3 bucket in your preferred region with the enable bucket versioning and encryption.

S3-created

  • Go to DynamoDb and create a table. use the Partition key as LockID and keep the rest of the settings as default.

DynamoDB Table

  • Create backend.tf and add the following code. Please add your S3 bucket name, DynamoDB table name, and AWS region.

terraform {
? backend "s3" {
? ? bucket? ? ? ? ?= "{{s3-bucket-name}}"
? ? key? ? ? ? ? ? = "terraform.tfstate"
? ? region? ? ? ? ?= "{{aws-region}}"
? ? dynamodb_table = "{{dynamodb-table-name}}"
? }
}        

Step 4 -?Firstly Configure AWS using AWS CLI keys(Use aws configure command). next run terraform init command for initialize terraform project.

terraform init        
Terraform init

Step 5 -?Create the following files in your repository for EC2 instance creation.

main.tf

resource "aws_instance" "app_server" {
? ami ? ? ? ? ? = "{{ami-id}}"
? instance_type = "t2.micro"


? tags = {
? ? Name = var.instance_name
? }
}        

variables.tf

variable "instance_name" {
? description = "Value of the Name tag for EC2"
? type ? ? ? ?= string
? default ? ? = "Example EC2 Instance"
}        

outputs.tf

output "instance_id" {
? description = "Id EC2"
? value ? ? ? = aws_instance.app_server.id
}


output "instance_public_ip" {
? description = "Public ip EC2"
? value ? ? ? = aws_instance.app_server.public_ip
}        

Step 6 - Type terraform apply to creating resources. In the creation process enter yes for approval.

Terraform-apply
Terraform-apply-result

Step 6 - You can see the DynamoDB table like this.

Dynamodb-created-table

Also, see terraform.tfstate file inside S3 bucket.

s3-bucket-view

Step 7 - Finally, run terraform destroy command to remove all created resources. Because it helps to reduce AWS cost.

terraform-destroy-view

Thanks for reading the Article.

Git repository: https://github.com/sanju2/tf-state-mgt

good one but its better you create the bucket and table using IaC too. can give a try this https://github.com/devopswithzack/aws-terraform-s3-backend

要查看或添加评论,请登录

Lasantha Sanjeewa Silva的更多文章

  • Free resources to get started AWS DevOps

    Free resources to get started AWS DevOps

    1.Cloud Quest: Cloud Practitioner AWS Cloud Quest: Cloud Practitioner is a role-playing learning game that helps you…

社区洞察

其他会员也浏览了