DEV Community

Marcos Henrique for AWS Community Builders

Posted on

Why AWS EFS Rocks and How to Create it with CDK + TS

Hello there, technologists! As we solve the puzzle of AWS Elastic File System (EFS), buckle up for an exhilarating voyage into the AWS cloud. Get set to throw the best cloud storage party ever! We'll give a quick overview of AWS EFS in this cool post, employing slang and a touch of extraversion. Let's start now!

What's AWS EFS?

someone thinking

Consider AWS EFS to be the ultimate cloud storage jukebox. It's like having a virtual boombox that allows you to easily store and retrieve your files and data. AWS EFS is a fully managed file storage solution that provides your cloud applications with scalable and shared storage. It's like your own personal cloud DJ, playing files for your apps to rock 'n' roll too.

Jamming with AWS EFS:

people jamming

AWS EFS throws a wild party for your apps by offering a file system that can be mounted by several EC2 instances at the same time. It's like having a huge dance floor where all of your applications can jam together. You don't have to be concerned about size restrictions or fitting into a small storage closet. No matter how many instances are ripping up the dance floor, AWS EFS has your back.

Why AWS EFS Rocks?

Here's the real kicker, people. AWS EFS is highly available and durable, which means your files will be protected even if a major disaster occurs. It duplicates your data across different Availability Zones automatically, ensuring that your files are backed up and ready to go.

What's more, guess what? You only pay for the services you use! AWS EFS continues the celebration by providing a pay-as-you-go pricing mechanism. There's no need to hire out the entire club; simply pay for the songs you're playing. It's a low-cost and hassle-free method to jam with your online files.

Hands-On

A child hands-on
Firstly we need to create our security group and add an inbound rule to allow instances to connect to the file system over port 2049



const securityGroup = new SecurityGroup(this, 'MySecurityGroup', {
      vpc: <your VPC here>,
      allowAllOutbound: true, // Allow outbound traffic on all ports
      securityGroupName: 'MySecurityGroup',
});

    // Add an inbound rule to allow connections on port 2049
securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(2049), 'Allow NFS Connections');


Enter fullscreen mode Exit fullscreen mode

Finally we are going to create the EFS using this new SG to mount targets.

Mount targets are endpoints within a Virtual Private Cloud (VPC) that allow instances to connect and access an Amazon Elastic File System (EFS), providing file system access to multiple instances simultaneously.



const fileSystem = new FileSystem(this, 'MyEfsFileSystem', {
  vpc: <your VPC here>,
  lifecyclePolicy: LifecyclePolicy.AFTER_14_DAYS, // Example lifecycle policy
  securityGroup: securityGroup, // Associate the security group with the file system
});

// Create mount targets for the file system
fileSystem.addAccessPoint('MyAccessPoint', {
  createAcl: {
    ownerGid: '1000',
    ownerUid: '1000',
    permissions: '755',
  },
  path: '/my-mount-point', // Example mount point path
  posixUser: {
    gid: '1000',
    uid: '1000',
  },
  securityGroups: [securityGroup], // Associate the security group with the mount target
});


Enter fullscreen mode Exit fullscreen mode

When you merge everything, you'll have a file like this:



import * as cdk from 'aws-cdk-lib';
import { Stack, Construct, StackProps } from 'aws-cdk-lib';
import { SecurityGroup, Peer, Port } from 'aws-cdk-lib/aws-ec2';
import { FileSystem, LifecyclePolicy } from 'aws-cdk-lib/aws-efs';

class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // Create a new security group
    const securityGroup = new SecurityGroup(this, 'MySecurityGroup', {
      vpc: <your VPC here>,
      allowAllOutbound: true, // Allow outbound traffic on all ports
      securityGroupName: 'MySecurityGroup',
    });

    // Add an inbound rule to allow connections on port 2049
    securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(2049), 'Allow NFS Connections');

    // Create a new Amazon EFS file system
    const fileSystem = new FileSystem(this, 'MyEfsFileSystem', {
      vpc: <your VPC here>,
      lifecyclePolicy: LifecyclePolicy.AFTER_14_DAYS, // Example lifecycle policy
      securityGroup: securityGroup, // Associate the security group with the file system
    });

    // Create mount targets for the file system
    fileSystem.addAccessPoint('MyAccessPoint', {
      createAcl: {
        ownerGid: '1000',
        ownerUid: '1000',
        permissions: '755',
      },
      path: '/my-mount-point', // Example mount point path
      posixUser: {
        gid: '1000',
        uid: '1000',
      },
      securityGroups: [securityGroup], // Associate the security group with the mount target
    });
  }
}

const app = new cdk.App();
new MyStack(app, 'MyStack');
app.synth();



Enter fullscreen mode Exit fullscreen mode

That finishes our quick overview of EFS and how to develop it with CDK. That's all there is to it, guys!
thanks

Top comments (0)