AWS CDK EFS Construct Library (v1)
The AWS CDK Construct Library for AWS EFS (Amazon Elastic File System) provides constructs to create and manage EFS resources as infrastructure-as-code. This package is part of the AWS CDK v1 ecosystem (version 1.204.0), which is currently in maintenance mode. AWS CDK v1 typically sees infrequent updates, primarily for critical bug fixes or security patches.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_efs'
cause The `aws-cdk.aws-efs` Python package is not installed or the `from aws_cdk import aws_efs` import path is incorrect for the installed CDK version.fixEnsure `pip install aws-cdk.aws-efs aws-cdk.core` has been run for CDK v1. If using CDK v2, install `aws-cdk-lib` instead and use `from aws_cdk import aws_efs` (without the separate package). -
TypeError: Argument 'scope' must be instance of Construct, not <class 'aws_cdk.aws_ec2.Vpc'>
cause This often occurs when mixing AWS CDK v1 and v2 constructs or core libraries. A v1 construct might be expecting a v1 `cdk.Construct` but receives a v2 equivalent (or vice-versa).fixVerify that all your `aws-cdk.*` packages are consistently either v1 or v2. If using v1, ensure all libraries are installed separately (e.g., `aws-cdk.core`, `aws-cdk.aws-ec2`, `aws-cdk.aws-efs`). If using v2, install only `aws-cdk-lib`. -
Resource handler returned message: "EFS file system mount target could not be created" (RequestToken: ..., HandlerErrorCode: InvalidRequest)
cause This typically indicates an issue with the VPC configuration for the EFS mount targets, often related to security groups, network ACLs, or insufficient IP addresses in subnets.fixCheck the security group attached to the EFS mount target (automatically created or custom) allows inbound port 2049 from the requesting resources. Also, confirm the subnets associated with the VPC have enough available IP addresses and are correctly routed.
Warnings
- breaking This package is for AWS CDK v1. Using it with an AWS CDK v2 application (which uses the `aws-cdk-lib` package) will cause `TypeError` or `AttributeError` due to incompatible types and API differences. The v2 equivalent constructs are part of the `aws-cdk-lib` package.
- deprecated AWS CDK v1 is in maintenance mode and no longer receives new feature development. Users are strongly encouraged to migrate to AWS CDK v2 for new projects to benefit from active development, consolidated packages, and improved features.
- gotcha EFS File Systems require a VPC and correctly configured security groups for EC2 instances to mount them. Failing to specify a VPC or providing inadequate security group rules will prevent instances from accessing the EFS share.
Install
-
pip install aws-cdk.aws-efs aws-cdk.core
Imports
- FileSystem
from aws_cdk.aws_efs import FileSystem
from aws_cdk import aws_efs
- AccessPoint
from aws_cdk import efs
from aws_cdk import aws_efs
Quickstart
import os
from aws_cdk import (
core as cdk,
aws_ec2 as ec2,
aws_efs as efs,
)
class EfsStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create a VPC for the EFS File System
vpc = ec2.Vpc(self, "EfsVpc", max_azs=2)
# Create an EFS File System
file_system = efs.FileSystem(self, "MyEfsFileSystem",
vpc=vpc,
performance_mode=efs.PerformanceMode.GENERAL_PURPOSE,
throughput_mode=efs.ThroughputMode.BURSTING,
encrypted=True
)
# Optional: Create an Access Point for granular access control
efs.AccessPoint(self, "MyEfsAccessPoint",
file_system=file_system,
path="/mydata",
create_acl=efs.Acl(owner_uid="1001", owner_gid="1001", permissions="755"),
posix_user=efs.PosixUser(uid="1001", gid="1001")
)
cdk.CfnOutput(self, "FileSystemId", value=file_system.file_system_id)
app = cdk.App()
EfsStack(app, "EfsQuickstartStack",
env=cdk.Environment(account=os.environ.get("CDK_DEFAULT_ACCOUNT", ""),
region=os.environ.get("CDK_DEFAULT_REGION", ""))
)
app.synth()