Why AWS EFS Rocks and How to Create it with CDK + TS
Tags: aws, typescript
Published: 2023-06-01 03:34:52

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](https://media1.giphy.com/media/gEvab1ilmJjA82FaSV/giphy.gif?cid=ecf05e47tww91o52zw0rzmywugg759845na10otwua19nek7&ep=v1_gifs_search&rid=giphy.gif&ct=g)

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](https://media3.giphy.com/media/YMpRR36EoUxZjFyEZE/giphy.gif?cid=ecf05e4753z38u3rqsqr7ukrq4o2dibexun7xqyxpwwqphc7&ep=v1_gifs_search&rid=giphy.gif&ct=g)

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](https://media2.giphy.com/media/3o7TKytUCVX0ROGsZq/giphy.gif?cid=ecf05e472qgjxuyipg67baeaz5nw9fh5ai8436jg4f9fnkq2&ep=v1_gifs_search&rid=giphy.gif&ct=g)
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

```typescript
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');
```

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.

```typescript
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
});
```

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

```typescript
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();

```
That finishes our quick overview of EFS and how to develop it with CDK. That's all there is to it, guys!
![thanks](https://media2.giphy.com/media/ZfK4cXKJTTay1Ava29/giphy.gif?cid=ecf05e47lkq99iex8ggnbdoli0zocfr67r9eqkdttd3mim1j&ep=v1_gifs_search&rid=giphy.gif&ct=g)