Bootstrap FreeKB - Amazon Web Services (AWS) - Create an Elastic File System (EFS) using Terraform
Amazon Web Services (AWS) - Create an Elastic File System (EFS) using Terraform

Updated:   |  Amazon Web Services (AWS) articles

An Elastic File System (EFS) is similar to NFS or CIFS or a Linux Samba share, in that it's basically Network Attached Storage (NAS).

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── elastic_file_systems (directory)
│   ├── elastic_file_systems.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

And elastic_file_systems.tf could have something like this.

resource "aws_efs_file_system" "my-efs" {
  creation_token = "my-efs"
  encrypted      = true
}

 

Optionally, the kms_key_id can be used to apply a Key Management Services (KMS) ID to the EFS Access Point. In this scenario, you will probably want to use the aws_key_key resource to get the arn of the KMS key.

data "aws_kms_key" "my-kms-key" {
  key_id = "alias/default-kms-key"
}

resource "aws_efs_file_system" "my-efs" {
  creation_token = "my-efs"
  encrypted      = true
  kms_key_id     = data.aws_kms_key.my-kms-key.arn
  tags = {
    Name = "my-efs"
    Role = "AWS EFS File Storage"
  }
}

 

You may need to reissue the terraform init command.

~]# terraform init
Initializing the backend...
Initializing modules...
Initializing provider plugins...
Terraform has been successfully initialized!

 

The terraform plan command can be used to see what Terraform will try to do.

By default, the terraform.tfstate file should be found in your root module directory (/usr/local/terraform/aws in this example).

  • If the EFS does not exist and the terraform.tfstate file does not contain the EFS, Terraform will create the EFS.
  • If the EFS exists and the terraform.tfstate file contains the EFS and a difference is found between the efs.tf file and the terraform.tfstate file, Terraform will update the EFS.
  • If the EFS exists and the terraform.tfstate file contains the EFS and the EFS is removed from the efs.tf file, Terraform will destroy (delete) the EFS.

The terraform apply command can be used to create, update or delete the EFS.

module.elastic_file_systems.aws_efs_file_system.my-efs: Creating...
module.elastic_file_systems.aws_efs_file_system.my-efs: Creation complete after 5s [id=fs-056a3b9ea34bd8067 ]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

 

In this example, an Elastic File System with ID fs-056a3b9ea34bd8067 was created.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b2608c in the box below so that we can be sure you are a human.